1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Forbid :: in object-like key name

Ref #3399
This commit is contained in:
Brown 2020-05-19 14:38:38 -04:00
parent dc82243edc
commit 8becefda04
3 changed files with 26 additions and 3 deletions

View File

@ -635,6 +635,12 @@ class ParseTree
case '::':
$nexter_token = $i + 2 < $c ? $type_tokens[$i + 2] : null;
if ($current_leaf instanceof ParseTree\ObjectLikeTree) {
throw new TypeParseTreeException(
'Unexpected :: in array key'
);
}
if (!$nexter_token
|| (!preg_match('/^([a-zA-Z_][a-zA-Z_0-9]*\*?|\*)$/', $nexter_token[0])
&& strtolower($nexter_token[0]) !== 'class')

View File

@ -80,7 +80,7 @@ class ObjectLike extends \Psalm\Type\Atomic
return (string) $type;
}
if (\is_string($name) && \preg_match('/[ "\'\\\\.\n]/', $name)) {
if (\is_string($name) && \preg_match('/[ "\'\\\\.\n:]/', $name)) {
$name = '\'' . \str_replace("\n", '\n', \addslashes($name)) . '\'';
}
@ -106,7 +106,7 @@ class ObjectLike extends \Psalm\Type\Atomic
return $type->getId();
}
if (\is_string($name) && \preg_match('/[ "\'\\\\.\n]/', $name)) {
if (\is_string($name) && \preg_match('/[ "\'\\\\.\n:]/', $name)) {
$name = '\'' . \str_replace("\n", '\n', \addslashes($name)) . '\'';
}
@ -164,7 +164,7 @@ class ObjectLike extends \Psalm\Type\Atomic
$this_class,
$use_phpdoc_format
) {
if (\is_string($name) && \preg_match('/[ "\'\\\\.\n]/', $name)) {
if (\is_string($name) && \preg_match('/[ "\'\\\\.\n:]/', $name)) {
$name = '\'' . \str_replace("\n", '\n', \addslashes($name)) . '\'';
}

View File

@ -426,6 +426,23 @@ class TypeParseTest extends TestCase
$this->assertSame('array{\'\\"\': int, \'\\\'\': string}', (string) Type:: parseString('array{"\\"": int, "\\\'": string}'));
}
/**
* @return void
*/
public function testObjectLikeWithClassConstantKey()
{
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
Type::parseString('array{self::FOO: string}');
}
/**
* @return void
*/
public function testObjectLikeWithQuotedClassConstantKey()
{
$this->assertSame('array{\'self::FOO\': string}', (string) Type:: parseString('array{"self::FOO": string}'));
}
/**
* @return void
*/