Skip to content
Permalink
Browse files

Fix #2362 - support single value type in docblocks

  • Loading branch information
muglug committed Nov 21, 2019
1 parent 4c2f7ad commit 06653f6322c6ef3f32c9c4509cd4f0da7b5f0c7f
Showing with 57 additions and 9 deletions.
  1. +14 −9 src/Psalm/Type.php
  2. +33 −0 tests/TypeParseTest.php
  3. +10 −0 tests/ValueTest.php
@@ -135,7 +135,7 @@ public static function parseString(
/**
* Parses a string type representation
*
* @param array<int, array{0: string, 1: int}> $type_tokens
* @param list<array{0: string, 1: int}> $type_tokens
* @param array{int,int}|null $php_version
* @param array<string, array<string, array{Type\Union}>> $template_type_map
*
@@ -151,16 +151,21 @@ public static function parseTokens(
// Note: valid identifiers can include class names or $this
if (!preg_match('@^(\$this|\\\\?[a-zA-Z_\x7f-\xff][\\\\\-0-9a-zA-Z_\x7f-\xff]*)$@', $only_token[0])) {
throw new TypeParseTreeException("Invalid type '$only_token[0]'");
}
$only_token[0] = self::fixScalarTerms($only_token[0], $php_version);
if (!is_numeric($only_token)
&& strpos($only_token[0], '\'') !== false
&& strpos($only_token[0], '"') !== false
) {
throw new TypeParseTreeException("Invalid type '$only_token[0]'");
}
} else {
$only_token[0] = self::fixScalarTerms($only_token[0], $php_version);
$atomic = Atomic::create($only_token[0], $php_version, $template_type_map);
$atomic->offset_start = 0;
$atomic->offset_end = strlen($only_token[0]);
$atomic = Atomic::create($only_token[0], $php_version, $template_type_map);
$atomic->offset_start = 0;
$atomic->offset_end = strlen($only_token[0]);
return new Union([$atomic]);
return new Union([$atomic]);
}
}
$parse_tree = ParseTree::createFromTokens($type_tokens);
@@ -856,6 +856,39 @@ public function testEnumWithoutSpaces()
$this->assertSame($resolved_type->getId(), $docblock_type->getId());
}
/**
* @return void
*/
public function testSingleLiteralString()
{
$this->assertSame(
'string',
(string)Type::parseString('"var"')
);
}
/**
* @return void
*/
public function testSingleLiteralInt()
{
$this->assertSame(
'int',
(string)Type::parseString('6')
);
}
/**
* @return void
*/
public function testSingleLiteralFloat()
{
$this->assertSame(
'float',
(string)Type::parseString('6.315')
);
}
/**
* @return void
*/
@@ -646,6 +646,16 @@ class A {
public $foo = "a";
}',
],
'supportSingleLiteralType' => [
'<?php
class A {
/**
* @var string
* @psalm-var "easy"
*/
private $type = "easy";
}'
],
];
}

0 comments on commit 06653f6

Please sign in to comment.
You can’t perform that action at this time.