Repository navigation
fix(assertion): instanceof parent narrows to the parent class - #14318
Conversation
…4246 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
✅ Performance: unchanged · Types: decreased
How to read this table
✅ symfony: issues +4 −8Memory 3 596 → 3 596 MB (<0.1 %) · Wall 277.9 → 270.7 s (−2.6 %) Weighted 98 386 → 98 370 (−16, −0.02 %): L1 −2 · L2 +2 · L3 −2 · L6 −2
Changed in place (2) @@ 2 places MixedArgument @@
src/Symfony/Component/String/AbstractUnicodeString.php:499
src/Symfony/Component/String/AbstractUnicodeString.php:538
- Argument 1 of preg_quote cannot be mixed, expecting string
+ Argument 1 of preg_quote cannot be mixed|string, expecting stringAdded (2)
Removed (6)
head 6d0cf66 · base 28e192b (merge-base) · workflow run |
|
Behaviour change
|
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
orklah
left a comment
There was a problem hiding this comment.
Thanks, the fix is in the right place. Two things before merging:
getParentFQCLN()returnsnullinside a trait method, soinstanceof parentstill doesn't narrow there, whileselfandparent::foo()are resolved against the using class:
trait T {
public function viaTrait(Shape $s): int {
if ($s instanceof parent) {
return $s->getSize(); // still UndefinedInterfaceMethod
}
return 0;
}
}
final class UsesTrait extends Box { use T; }Resolving the parent from the storage of $this_class_name (as StaticCallAnalyzer does for parent:: through $context->self) fixes it and keeps every other case the same:
&& $this_class_name !== null
&& $source instanceof StatementsAnalyzer
&& ($parent_class_name = $source->getCodebase()->classlike_storage_provider->get($this_class_name)->parent_class) !== null- The same gap exists next door:
is_a($s, parent::class)(the// do nothingbranch in theis_ahandling) andget_class($s) === parent::class/$s::class === parent::class(getGetclassComparisonAssertions()setsparenttonull). All three still reportUndefinedInterfaceMethodon$s->getSize(). Could you handle them with the same resolution, with a test case each?
Minor: the test doesn't need php_version => '8.3' (and then MissingOverrideAttribute doesn't need ignoring), and a /** @psalm-check-type-exact $shape = Box */ in the narrowed branch would pin the type.
… for is_a() and class comparisons instanceof parent, is_a($x, parent::class), get_class($x) === parent::class and $x::class === parent::class resolve parent from the storage of the class the code runs for, as self does. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Pushed 5122297.
Not touched: |
…4246 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks, the three points are addressed and this is ready to merge, but it now conflicts with master after today's merges. Could you merge master (keeping both sides)? I'll merge it once CI is green. |
# Conflicts: # tests/TypeReconciliation/ConditionalTest.php
$x instanceof parentdoes not narrow$x, althoughinstanceof selfandinstanceof Boxdo.Before
After
Fixes #14246