Repository navigation
Add Type::toNativePhpTypeNode() and NativeTypePosition - #6873
Merged
Merged
Conversation
ondrejmirtes
force-pushed
the
to-native-php-type-node
branch
2 times, most recently
from
October 28, 2026 13:03
7b01a11 to
2e7f04a
Compare
ondrejmirtes
force-pushed
the
to-native-php-type-node
branch
2 times, most recently
from
October 28, 2026 13:18
f1d95cf to
2fb3e8f
Compare
ondrejmirtes
commented
Oct 28, 2026
| return new IdentifierTypeNode('list'); | ||
| } | ||
|
|
||
| /** @return Identifier|Name|ComplexType|null */ |
Member
Author
There was a problem hiding this comment.
The phpdoc doesn't have to be there. It's implicitly inherited.In case PHPStan complains about too-wide type, you can put a narrower type here.
Go through all similar code and remove extraneous PHPDocs like that.
Member
Author
There was a problem hiding this comment.
Removed the redundant @return PHPDocs from all 40 implementations in 594c472, along with the imports they left unused. PHPStan doesn't need a narrower type anywhere. The only PHPDocs left are on Type::toNativePhpTypeNode() itself and the two static methods of NativePhpTypeNodeHelper, which don't inherit one.
Exports a type as a native PHP type declaration node (Identifier, Name or ComplexType), or null when the type cannot be expressed in the given position. NativeTypePosition is a value object like VerbosityLevel. It carries the PHP version and answers feature questions (isCallableAllowed(), isIntersectionAllowed(), isNullableAllowed(), ...) rather than naming the position. UnionType hands its members a derived position, so an intersection inside a union is only emitted from PHP 8.2 (DNF) while a standalone intersection is emitted from PHP 8.1. Foo|null is preferred as ?Foo wherever the nullable syntax is allowed. The compound halves live in NativePhpTypeNodeHelper so the turbo extension calls the same code; the other shadowed types implement the method natively. Amp-Thread-ID: https://ampcode.com/threads/T-01a179f2-995f-7745-8d8c-4c174ba5c0ce
ondrejmirtes
force-pushed
the
to-native-php-type-node
branch
from
October 28, 2026 14:15
2fb3e8f to
594c472
Compare
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
Type::toNativePhpTypeNode(NativeTypePosition $position), the native-declaration counterpart oftoPhpDocNode(). It returnsIdentifier|Name|ComplexType|null(PHPDoc only, so the PHP 7.4 downgrade keeps the interface and implementations compatible). It returns null when the type cannot be written in that position.The motivation is Rector: its
PHPStanStaticTypeMapperkeeps about 30 type mappers whose only job is turning PHPStan types back into php-parser type nodes. A prototype that replaces them with this method passes Rector's test suite.NativeTypePosition
A value object like
VerbosityLevel, built from a PHP version:NativeTypePosition::parameter($phpVersion),returnType(),property(),classConstant().isCallableAllowed(),isVoidAllowed(),isStaticAllowed(),isNullableAllowed(),isUnionAllowed(),isIntersectionAllowed(), and so on. A property, for example, does not allowcallable.toUnionMember(),toNullableInner(),toIntersectionMember().IntersectionTypeonly asksisIntersectionAllowed(), so a standaloneA&Bis emitted from PHP 8.1, while inside a union (DNF) it needs 8.2.Behaviour
Foo|nullbecomes?Foowherever the nullable syntax is allowed; otherwisenullgoes last in the union.Foo|object,array|iterable,A|(A&B).non-empty-string→string,list→array).object&hasMethod(foo)becomesobject.mixed,resourceand error types give null.TypeToNativePhpTypeNodeTestround-trips every case throughParserNodeTypeToPHPStanTypeand asserts that the result is a supertype of the original.Turbo
The shadowed types implement the method natively. The union and intersection logic lives in
NativePhpTypeNodeHelper, which both the PHP classes and the extension call.type-family.phpcompares native and PHP results for 48 types across 20 positions.