-
Notifications
You must be signed in to change notification settings - Fork 572
Fix isset() falsey branch not narrowing array type for dim fetches #4983
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
Changes from 2 commits
3b14a35
fed2702
432d18a
24c603c
cf3bf63
c5a4140
06ae0bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -920,6 +920,52 @@ | |
| return $exprType; | ||
| } | ||
|
|
||
| if ( | ||
| $issetExpr instanceof ArrayDimFetch | ||
| && $issetExpr->dim !== null | ||
| ) { | ||
| $varType = $scope->getType($issetExpr->var); | ||
| if (!$varType instanceof MixedType) { | ||
| $dimType = $scope->getType($issetExpr->dim); | ||
|
|
||
| if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) { | ||
| $types = $varType instanceof UnionType ? $varType->getTypes() : [$varType]; | ||
| $typesToRemove = []; | ||
| foreach ($types as $innerType) { | ||
| $hasOffset = $innerType->hasOffsetValueType($dimType); | ||
| if (!$hasOffset->yes() || !$innerType->getOffsetValueType($dimType)->isNull()->no()) { | ||
|
Check warning on line 936 in src/Analyser/TypeSpecifier.php
|
||
| continue; | ||
| } | ||
|
|
||
| $typesToRemove[] = $innerType; | ||
| } | ||
|
|
||
| if ($typesToRemove !== []) { | ||
| $typeToRemove = TypeCombinator::union(...$typesToRemove); | ||
| $result = $this->create( | ||
| $issetExpr->var, | ||
| $typeToRemove, | ||
| TypeSpecifierContext::createFalse(), | ||
| $scope, | ||
| )->setRootExpr($expr); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This need to be reworked cf the test it would be great to refining the constant array without totally removing them Do you see an easy way to do such thing @staabm ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it work to iterate over and re-union the results?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried lot of different thing without success. I used |
||
|
|
||
| if ($scope->hasExpressionType($issetExpr->var)->maybe()) { | ||
| $result = $result->unionWith( | ||
| $this->create( | ||
| new IssetExpr($issetExpr->var), | ||
| new NullType(), | ||
| TypeSpecifierContext::createTruthy(), | ||
| $scope, | ||
| )->setRootExpr($expr), | ||
| ); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return new SpecifiedTypes(); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug9908; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public function test(): void | ||
| { | ||
| $a = []; | ||
| if (rand() % 2) { | ||
| $a = ['bar' => 'string']; | ||
| } | ||
|
|
||
| if (isset($a['bar'])) { | ||
| $a['bar'] = 1; | ||
| } | ||
|
|
||
| assertType("array{}|array{bar: 1}", $a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{bar?: int} $foo | ||
| */ | ||
| public function sayHello(array $foo): void | ||
| { | ||
| echo 'Hello' . print_r($foo, true); | ||
| } | ||
|
|
||
| public function test2(): void | ||
| { | ||
| $a = []; | ||
| if (rand() % 2) { | ||
| $a = ['bar' => 'string']; | ||
| } | ||
|
|
||
| if (isset($a['bar'])) { | ||
| $a['bar'] = 1; | ||
| } | ||
|
|
||
| $this->sayHello($a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{a: string, b: string}|array{a: string|null, c: string}|array{a?: string, d: string} $a | ||
| */ | ||
| public function moreTests($a): void | ||
| { | ||
| if (isset($a['a'])) { | ||
| assertType("array{a: string, b: string}|array{a: string, c: string}|array{a: string, d: string}", $a); | ||
| } else { | ||
| // Could be "array{a: null, c: string}|array{d: string}" | ||
| assertType("array{a: string|null, c: string}|array{a?: string, d: string}", $a); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.