Repository navigation
Add NameNode, FullyQualifiedNameNode and RelativeNameNode - #339
Merged
Merged
Conversation
A class-like name in a PHPDoc type, as opposed to a keyword like int or non-falsy-string, which stays an IdentifierTypeNode. The parser never produces these nodes because it does not know which identifiers are keywords; tools that do, like PHPStan's Type::toPhpDocNode(), create them, and the Printer prints them, also when format-preserving. They deliberately do not extend IdentifierTypeNode, so code that matches keywords by IdentifierTypeNode::$name cannot mistake a class named Int for int. GenericTypeNode::$type, CallableTypeNode::$identifier and TypeAliasImportTagValueNode::$importedFrom therefore widen from IdentifierTypeNode to TypeNode (IdentifierTypeNode|NameNode in PHPDoc; PHP 7.4 has no native union types). Parsed PHPDocs are unchanged. Amp-Thread-ID: https://ampcode.com/threads/T-01a179f2-995f-7745-8d8c-4c174ba5c0ce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds AST nodes for class-like names in PHPDoc types, modelled on php-parser's
Name\FullyQualified/Name\Relative:NameNode(interface, extendsTypeNode):toString()(no leading backslash),toCodeString(),getParts(),getFirst(),getLast(),isFullyQualified().FullyQualifiedNameNode: stores the name without the backslash, prints\Foo\Bar.RelativeNameNode:BarorFoo\Bar; rejects a leading backslash.The parser does not produce them. It doesn't know which identifiers are keywords, so parsed PHPDocs are unchanged. Tools that do know create them, e.g. PHPStan's
Type::toPhpDocNode()(phpstan/phpstan-src#6875 will be reworked on top of this), and Rector when it builds and imports names. The Printer prints them, including in format-preserving mode.They don't extend
IdentifierTypeNode, on purpose. Code that matches keywords byIdentifierTypeNode::$namemust not mistake a class namedIntforint. Consequently:GenericTypeNode::$type,CallableTypeNode::$identifierandTypeAliasImportTagValueNode::$importedFrom(and the constructor parameters) widen fromIdentifierTypeNodetoTypeNode, typedIdentifierTypeNode|NameNodein PHPDoc (PHP 7.4 has no native union types).->type->nameneeds aninstanceofcheck under static analysis, and subclasses redeclaring these properties break. Hence 2.4.x.