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
Use isArray, isConstantArray instead of instanceof in TypeCombinartor::union #2118
Conversation
ab253c8
to
b39adf9
Compare
@@ -239,7 +239,7 @@ public function getLastIterableValueType(): Type | |||
|
|||
public function isArray(): TrinaryLogic | |||
{ | |||
return TrinaryLogic::createYes(); | |||
return TrinaryLogic::createMaybe(); |
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.
I haven't checked the rest of this PR but this is certainly wrong. If something is a list then we know it's an array...
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.
I understand you're changing this so that some other algorithm results in an outcome we want, but the typesystem layer always has to tell the truth, otherwise something else breaks...
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.
I haven't checked the rest of this PR but this is certainly wrong. If something is a list then we know it's an array...
I thought that AccessoryType themselves are not isArray, they have to be always intersected with an array, but looking at the failing test, this assumption seems false as you are saying...
I can solve this PR without this change, I just wanted to check what fails with this :) (at least the tests didn't fail, maybe we need some regression tests)
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.
I'm likely to accept whatever you come up with, but make sure to do some benchmarks too :) It's really easy to think you've sped things up, but in reality you might have slowed them down :) Thanks.
This reverts commit 984a5d3.
…scribe(VerbosityLevel::cache())` and they should be same (except HasOffsetValueType)
8900cef
to
99ec877
Compare
99ec877
to
b4719b8
Compare
I tried to just refactor and keep the original behavior, but there seems to be a slight difference somewhere... |
I believe this refactor should have a slight performance improvement as I expected, because number of union calls has been decreased. |
3ed21be
to
d0626fb
Compare
This is the final performance comparison. |
This pull request has been marked as ready for review. |
$arrayTypes = $arrayTypes; | ||
|
||
$commonArrayAccessoryTypesKeys = []; | ||
if (count($arrayAccessoryTypes) > 1) { | ||
$commonArrayAccessoryTypesKeys = array_keys(array_intersect_key(...$arrayAccessoryTypes)); | ||
} elseif (count($arrayAccessoryTypes) > 0) { | ||
$commonArrayAccessoryTypesKeys = array_keys($arrayAccessoryTypes[0]); | ||
} | ||
|
||
$arrayAccessoryTypesToProcess = []; | ||
foreach ($commonArrayAccessoryTypesKeys as $commonKey) { | ||
$typesToUnion = []; | ||
foreach ($arrayAccessoryTypes as $array) { | ||
foreach ($array[$commonKey] as $arrayAccessoryType) { | ||
$typesToUnion[] = $arrayAccessoryType; | ||
} | ||
} | ||
$arrayAccessoryTypesToProcess[] = self::union(...$typesToUnion); | ||
} |
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.
this logic is replaced by count($arrayType) === count($accessoryType[$key])
in
https://github.com/phpstan/phpstan-src/pull/2118/files#diff-fef252fe8071200a31b3d282d1442135d62297098c9b20fd731f3888398ee973R549-R562
also, there is no need to union accessoryTypes except HasOffsetValueType
, because the types are all the same.
Thank you! |
Refactored union of ArrayTypes by extracting
processArrayAccessoryTypes
as a private function.Also, changed to use
isArray
isConstantArray
instead ofinstanceof ArrayType
.I think we can apply the bucket idea of phpstan/phpstan#8499 if we filter ArrayTypes, ConstantIntegerTypes|IntegerRangeType and union them before creating buckets.