TypeParser: add support for $this type

This commit is contained in:
Jan Tvrdik 2017-11-18 16:01:37 +01:00
parent 85464aa76e
commit 0b3dbb212f
4 changed files with 26 additions and 3 deletions

View File

@ -13,6 +13,7 @@ Nullable
Atomic
::= Identifier (Generic | Array)?
| ThisType
| '(' Type ')' Array?
Generic
@ -21,8 +22,11 @@ Generic
Array
::= ('[' ']')+
ThisType
::= '$this'
Identifier
::= ('\'? Word)+
::= ('\\'? Word)+
Word
::= [a-zA-Z_#x7F-#x10FFFF][0-9a-zA-Z_#x7F-#x10FFFF]*

View File

@ -13,6 +13,7 @@ Nullable
Atomic
= Identifier (Generic / Array)?
/ ThisType
/ '(' Type ')' Array?
Generic
@ -21,8 +22,11 @@ Generic
Array
= ('[' ']')+
ThisType
= '$this'
Identifier
= ('\'? Word)+
= ('\\'? Word)+
Word
= [a-zA-Z_\127-\255\\][a-zA-Z0-9_\127-\255\\]*
= [a-zA-Z_\\127-\\255][a-zA-Z0-9_\\127-\\255]*

View File

@ -0,0 +1,12 @@
<?php declare(strict_types = 1);
namespace PHPStan\PhpDocParser\Ast\Type;
class ThisTypeNode implements TypeNode
{
public function __toString(): string
{
return '$this';
}
}

View File

@ -38,6 +38,9 @@ class TypeParser
$type = $this->parseArray($tokens, $type);
}
} elseif ($tokens->tryConsumeTokenType(Lexer::TOKEN_THIS_VARIABLE)) {
return new Ast\Type\ThisTypeNode();
} else {
$type = new Ast\Type\IdentifierTypeNode($tokens->currentTokenValue());
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);