Repository navigation
Conversation
| $node->class, | ||
| '', // We don't care about the error message | ||
| static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasInstanceProperty($propertyName)->yes(), | ||
| static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasStaticProperty($propertyName)->yes(), |
There was a problem hiding this comment.
undoing this change does not make any test fail
There was a problem hiding this comment.
Good catch, the existing test runs with checkUnionTypes: true, so the callback never filters anything. Added testBug15681WithoutCheckingUnionTypes, which runs with checkUnionTypes: false on StaticRequest|Request and $x::$foo. With hasInstanceProperty only Request survives the filter, and it has no static $foo, so the error disappears.
| } | ||
|
|
||
| foreach ($member->getObjectClassNames() as $className) { | ||
| $classNames[$className] = $className; |
There was a problem hiding this comment.
removing the de-duplication of class names breaks no test
There was a problem hiding this comment.
Added static access on StaticRequest|class-string<StaticRequest>. Both members resolve to the same class, so without de-duplication all 4 static rules report the error twice.
|
|
||
| $classNames = []; | ||
| foreach ($members as $member) { | ||
| if ($acceptClassStrings) { |
There was a problem hiding this comment.
forcing if ($acceptClassStrings) to true breaks no test
There was a problem hiding this comment.
Added instance access on class-string<StaticRequest>|Request. With the flag forced on, the class-string member resolves to StaticRequest and its doFoo is reported as well, so the method and method-callable tests fail.
The restricted usage rules (method, property, static, class constant,
callable, string cast) missed the class of a union member in several cases:
- Static access on Foo|false, Foo|string, Foo|int|null was lost at level 7+,
because the whole union was converted at once.
- Instance access on callable|Foo, iterable<int>|Foo, Foo|object{a: int}
and TObject|Foo found no class name, because the intersection with object
left a member without a class.
- class-string<Foo>|false static access was never checked.
- RestrictedStaticPropertyUsageRule looked for an instance property instead
of a static one when picking the type to check, so $orFalse::$prop was
missed.
Class names are now collected per union member.
Follow-up to phpstan#7006 (phpstan/phpstan#15681)
b091695 to
7337f68
Compare
bbrala
left a comment
There was a problem hiding this comment.
Rebased onto 2.3.x to resolve the conflict with 1de281ab1. The instance rules now use RestrictedUsageObjectClassNames::get() instead of the isObject() shortcut. For a type that is already an object, the helper only reads its class names, so it does no more work than the shortcut.
| $node->class, | ||
| '', // We don't care about the error message | ||
| static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasInstanceProperty($propertyName)->yes(), | ||
| static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasStaticProperty($propertyName)->yes(), |
There was a problem hiding this comment.
Good catch, the existing test runs with checkUnionTypes: true, so the callback never filters anything. Added testBug15681WithoutCheckingUnionTypes, which runs with checkUnionTypes: false on StaticRequest|Request and $x::$foo. With hasInstanceProperty only Request survives the filter, and it has no static $foo, so the error disappears.
| } | ||
|
|
||
| foreach ($member->getObjectClassNames() as $className) { | ||
| $classNames[$className] = $className; |
There was a problem hiding this comment.
Added static access on StaticRequest|class-string<StaticRequest>. Both members resolve to the same class, so without de-duplication all 4 static rules report the error twice.
|
|
||
| $classNames = []; | ||
| foreach ($members as $member) { | ||
| if ($acceptClassStrings) { |
There was a problem hiding this comment.
Added instance access on class-string<StaticRequest>|Request. With the flag forced on, the class-string member resolves to StaticRequest and its doFoo is reported as well, so the method and method-callable tests fail.
|
Hmm, seems main is broken. Not gonna chase the CI right now |
I made the same PR as the referenced fix, was too late with opening so you fixed it already. But while walking though my work i also notices some extra ways the related issue might appear. Not sure if you want this, if this is overkill, but it seems the bug is still present in some combinations.
Follow-up to #7006 (phpstan/phpstan#15681).
#7006 fixed restricted usage on
?Foo,Foo|falseandclass-string<Foo>. Some cases are still not reported:Foo|false,Foo|stringandFoo|int|nullis reported at levels 2–6 but not at level 7 and above, so a higher level reports fewer errors:class-string<Foo>|falseused statically is missed at every level.findTypeToCheck()callback inRestrictedStaticPropertyUsageRulecheckshasInstanceProperty()and should checkhasStaticProperty(). This now matchesAccessStaticPropertiesCheck.Root cause
UnionType::getObjectClassNames()returns[]as soon as one member has no class names. AfterTypeCombinator::intersect($type, new ObjectWithoutClassType()), members likecallable-object,iterable&object, object shapes and object templates are still in the union, so the whole result is empty.getObjectTypeOrClassStringObjectType()on the whole union turnsfalse/intintoErrorTypeandstringintoobject.Foo|falsetherefore becomesErrorTypeand yields no class names. Below level 7,findTypeToCheck()happens to filter those members out first, which is why the result depends on the level.Fix
A small helper,
RestrictedUsageObjectClassNames, collects class names member by member:get(Type)is used by the 4 instance rules in place of the intersect.getFromClassExpression(Type)is used by the 4 static rules. It appliesgetObjectTypeOrClassStringObjectType()per member. The static rules still go throughRuleLevelHelper::findTypeToCheck(); only how class names are read from its result changes.Why not reuse something existing:
intersect(..., object)isn't enough, for the reason above: it leaves classless object members in the union, so cases 1 and 3 stay unreported.TypeUtils::flattenTypes()expands constant arrays (2^N optional keys). These rules run for every method call and property fetch on arbitrary types, so that's the wrong cost profile.AllowedArrayKeysTypesandPhpDocLineHelper.The loop is one
instanceofcheck plusgetObjectClassNames()per member, so it costs less than the intersect it replaces. On Drupal core with phpstan-deprecation-rules the difference in analysis time was within noise.Behaviour changes
findTypeToCheck()can't filter, such as'Foo'|falseorclass-string<Foo>|false, is now reported. The class really is a possible receiver there.checkThisOnlystill skips$expr::….mixed,object,object|Foo(which collapses toobject), non-class strings or plainclass-string.Tests
The new cases are appended to
tests/PHPStan/Rules/RestrictedUsage/data/bug-15681.php, so existing line numbers don't change. Each of the 8testBug15681()methods has new expectations. All 8 fail on 2.3.x without thesrc/changes. The phpstan-deprecation-rules test suite passes unchanged.Open question, not addressed here: at levels 0–1 the static rules don't check
$var::deprecatedStatic()at all, not even for a plainFoo $var. Instance access ($var->deprecated()) is checked at every level. Projects that run deprecation-rules at a low level only to find deprecations will miss these calls. Is that intended?