-
Notifications
You must be signed in to change notification settings - Fork 585
Collapse ObjectShapeType against HasPropertyType regardless of member order #6184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SanderMuller
wants to merge
3
commits into
phpstan:2.2.x
Choose a base branch
from
SanderMuller:fix-intersection-hasproperty-order
base: 2.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug15047; | ||
|
|
||
| use stdClass; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| final class MissalYearLimits | ||
| { | ||
|
|
||
| private ?int $until_year; | ||
|
|
||
| private function __construct(?int $until_year) | ||
| { | ||
| $this->until_year = $until_year; | ||
| } | ||
|
|
||
| /** | ||
| * @param stdClass&object{since_year:int,until_year?:int} $data | ||
| */ | ||
| protected static function fromObjectInternal(stdClass $data): self | ||
| { | ||
| assertType('object{since_year: int, until_year?: int}&stdClass', $data); | ||
|
staabm marked this conversation as resolved.
|
||
| assertType('int', $data->since_year); | ||
| assertType('int|null', isset($data->until_year) ? $data->until_year : null); | ||
| assertType('int|null', $data->until_year ?? null); | ||
|
|
||
| // isset() adds a hasProperty() member to the intersection, and resolving the | ||
| // optional key against it must not depend on where that member lands in the | ||
| // member list - which is what this used to be sensitive to. | ||
| if (isset($data->until_year)) { | ||
| assertType('object{since_year: int, until_year: int}&stdClass', $data); | ||
| assertType('int', $data->until_year); | ||
| } | ||
|
|
||
| return new self(isset($data->until_year) ? $data->until_year : null); | ||
| } | ||
|
|
||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug15047Instantiation; | ||
|
|
||
| use stdClass; | ||
|
|
||
| final class MissalYearLimits | ||
| { | ||
|
|
||
| private ?int $until_year; | ||
|
|
||
| private function __construct(?int $until_year) | ||
| { | ||
| $this->until_year = $until_year; | ||
| } | ||
|
|
||
| /** | ||
| * @param stdClass&object{since_year:int,until_year?:int} $data | ||
| */ | ||
| protected static function fromObjectInternal(stdClass $data): self | ||
| { | ||
| // the optional key must keep its declared type through the isset() narrowing, | ||
| // otherwise this reports "expects int|null, mixed given" | ||
| return new self(isset($data->until_year) ? $data->until_year : null); | ||
| } | ||
|
|
||
| public function getUntilYear(): ?int | ||
| { | ||
| return $this->until_year; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we not have the same problem for
if ($types[$i] instanceof ConstantArrayType && $types[$j] instanceof HasOffsetValueType) {?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked it rather than reasoned about it: the array pair does not have the problem, in any combination I could build.
I brute-forced order independence — for each member set, run
TypeCombinator::intersect()over every permutation and compare the results. On2.2.x:With this PR all of them are order-independent.
The reason is not the position of the pairing — both pairs sit in the same
$isSuperTypeB->maybe()block. It is that the object side has a third member that makes the accessory look redundant to the generic dedup above it, and the array side has no counterpart:stdClassreports every property as present, sohasProperty(a)is a definite supertype of it and$isSuperTypeA->yes()splices it out before it can be paired with the object shape. "Has offsetawith valueint" is not implied by any general array type, so nothing makesHasOffsetValueTypedefinitely redundant, and the only type that would — a constant array that already has the offset — has already absorbed the information anyway.One thing that did fall out of the sweep: the same bug fires with an ordinary interface, not just
stdClass.object{a?: int} & hasProperty(a) & Traversableresolves three ways depending on order on2.2.x, and in some of them the object shape is dropped entirely (Traversable&hasProperty(a)). This PR fixes that too. Say the word if you want that case added as a second fixture.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please add a test