-
Notifications
You must be signed in to change notification settings - Fork 574
Augment BooleanAnd falsey and BooleanOr truthy type narrowing when left and right conditions narrow different expression keys
#5595
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
base: 2.1.x
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -734,7 +734,16 @@ | |
| $leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context)->setRootExpr($expr); | ||
| $rightScope = $scope->filterByTruthyValue($expr->left); | ||
| $rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context)->setRootExpr($expr); | ||
| $types = $context->true() ? $leftTypes->unionWith($rightTypes) : $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope)); | ||
| if ($context->true()) { | ||
| $types = $leftTypes->unionWith($rightTypes); | ||
| } else { | ||
| $leftNormalized = $leftTypes->normalize($scope); | ||
| $rightNormalized = $rightTypes->normalize($rightScope); | ||
| $types = $leftNormalized->intersectWith($rightNormalized); | ||
| $leftFalseyScope = $scope->filterByFalseyValue($expr->left); | ||
| $rightFalseyScope = $rightScope->filterByFalseyValue($expr->right); | ||
| $types = $this->augmentDisjunctionTypes($scope, $leftNormalized, $rightNormalized, $leftFalseyScope, $rightFalseyScope, $types); | ||
| } | ||
| if ($context->false()) { | ||
| $leftTypesForHolders = $leftTypes; | ||
| $rightTypesForHolders = $rightTypes; | ||
|
|
@@ -788,8 +797,13 @@ | |
| ) { | ||
| $types = $leftTypes->normalize($scope); | ||
| } else { | ||
| $types = $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope)); | ||
| $leftNormalized = $leftTypes->normalize($scope); | ||
| $rightNormalized = $rightTypes->normalize($rightScope); | ||
| $types = $leftNormalized->intersectWith($rightNormalized); | ||
| $types = $this->augmentBooleanOrTruthyWithConditionalHolders($scope, $rightScope, $expr, $types); | ||
| $leftTruthyScopeForAugment = $scope->filterByTruthyValue($expr->left); | ||
| $rightTruthyScopeForAugment = $rightScope->filterByTruthyValue($expr->right); | ||
| $types = $this->augmentDisjunctionTypes($scope, $leftNormalized, $rightNormalized, $leftTruthyScopeForAugment, $rightTruthyScopeForAugment, $types); | ||
| } | ||
| } else { | ||
| $types = $leftTypes->unionWith($rightTypes); | ||
|
|
@@ -2076,6 +2090,65 @@ | |
| return $types; | ||
| } | ||
|
|
||
| private function augmentDisjunctionTypes( | ||
| MutatingScope $scope, | ||
| SpecifiedTypes $leftNormalized, | ||
| SpecifiedTypes $rightNormalized, | ||
| MutatingScope $leftFilteredScope, | ||
| MutatingScope $rightFilteredScope, | ||
| SpecifiedTypes $types, | ||
| ): SpecifiedTypes | ||
| { | ||
| $candidateExprs = []; | ||
| foreach ($leftNormalized->getSureTypes() as $exprString => [$exprNode, $type]) { | ||
| $candidateExprs[$exprString] = $exprNode; | ||
| } | ||
| foreach ($rightNormalized->getSureTypes() as $exprString => [$exprNode, $type]) { | ||
| $candidateExprs[$exprString] = $exprNode; | ||
| } | ||
|
|
||
| $existingSureTypes = $types->getSureTypes(); | ||
|
|
||
| foreach ($candidateExprs as $exprString => $targetExpr) { | ||
| if (isset($existingSureTypes[$exprString])) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!$scope->hasExpressionType($targetExpr)->yes()) { | ||
|
Check warning on line 2117 in src/Analyser/TypeSpecifier.php
|
||
| continue; | ||
| } | ||
| if (!$leftFilteredScope->hasExpressionType($targetExpr)->yes()) { | ||
|
Check warning on line 2120 in src/Analyser/TypeSpecifier.php
|
||
| continue; | ||
| } | ||
| if (!$rightFilteredScope->hasExpressionType($targetExpr)->yes()) { | ||
|
Check warning on line 2123 in src/Analyser/TypeSpecifier.php
|
||
| continue; | ||
| } | ||
|
|
||
| $originalType = $scope->getType($targetExpr); | ||
| $leftType = $leftFilteredScope->getType($targetExpr); | ||
| $rightType = $rightFilteredScope->getType($targetExpr); | ||
|
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. You could already continue if |
||
|
|
||
| if ($leftType->equals($originalType) || !$originalType->isSuperTypeOf($leftType)->yes()) { | ||
|
Check warning on line 2131 in src/Analyser/TypeSpecifier.php
|
||
| continue; | ||
| } | ||
|
|
||
| if ($rightType->equals($originalType) || !$originalType->isSuperTypeOf($rightType)->yes()) { | ||
|
Check warning on line 2135 in src/Analyser/TypeSpecifier.php
|
||
| continue; | ||
| } | ||
|
|
||
| $unionType = TypeCombinator::union($leftType, $rightType); | ||
| if ($unionType->equals($originalType)) { | ||
| continue; | ||
| } | ||
|
|
||
| $types = $types->unionWith( | ||
| $this->create($targetExpr, $unionType, TypeSpecifierContext::createTrue(), $scope), | ||
| ); | ||
| } | ||
|
|
||
| return $types; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, ConditionalExpressionHolder[]> | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14566; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test | ||
| */ | ||
| function fooNestedIfs(array $test): void { | ||
| if (isset($test['hi'])) { | ||
| if (is_string($test['hi'])) { | ||
| return; | ||
| } | ||
| } | ||
| assertType("array{}|array{hi: array{0: 42, 1?: 42}}", $test); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test | ||
| */ | ||
| function fooCombinedAnd(array $test): void { | ||
| if (isset($test['hi']) && is_string($test['hi'])) { | ||
| return; | ||
| } | ||
| assertType("array{}|array{hi: array{0: 42, 1?: 42}}", $test); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test | ||
| */ | ||
| function fooCombinedAndAssign(array $test): void { | ||
| if (isset($test['hi']) && is_string($test['hi'])) { | ||
| return; | ||
| } | ||
| $test['hi'][] = 42; | ||
| } | ||
|
|
||
| /** | ||
| * @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test | ||
| */ | ||
| function fooBooleanOrDual(array $test): void { | ||
| if (!isset($test['hi']) || !is_string($test['hi'])) { | ||
| assertType("array{}|array{hi: array{0: 42, 1?: 42}}", $test); | ||
| return; | ||
| } | ||
| assertType("array{hi: 'hello'}", $test); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{}|array{hi: 42}|array{hi: array{0: 42, 1?: 42}} $testIsArray | ||
| */ | ||
| function fooIsArray(array $testIsArray): void { | ||
| if (isset($testIsArray['hi']) && is_array($testIsArray['hi'])) { | ||
| return; | ||
| } | ||
| assertType("array{}|array{hi: 42}", $testIsArray); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{}|array{hi: 42}|array{hi: 'hello'} $testIsInt | ||
| */ | ||
| function fooIsInt(array $testIsInt): void { | ||
| if (isset($testIsInt['hi']) && is_int($testIsInt['hi'])) { | ||
| return; | ||
| } | ||
| assertType("array{}|array{hi: 'hello'}", $testIsInt); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{}|array{hi: 42}|array{hi: 1.5} $testIsFloat | ||
| */ | ||
| function fooIsFloat(array $testIsFloat): void { | ||
| if (isset($testIsFloat['hi']) && is_float($testIsFloat['hi'])) { | ||
| return; | ||
| } | ||
| assertType("array{}|array{hi: 42}", $testIsFloat); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{}|array{hi: true}|array{hi: 'hello'} $testIsBool | ||
| */ | ||
| function fooIsBool(array $testIsBool): void { | ||
| if (isset($testIsBool['hi']) && is_bool($testIsBool['hi'])) { | ||
| return; | ||
| } | ||
| assertType("array{}|array{hi: 'hello'}", $testIsBool); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{}|array{val: 'hello'}|array{val: array{0: 42}} $testArrayKeyExists | ||
| */ | ||
| function fooArrayKeyExists(array $testArrayKeyExists): void { | ||
| if (array_key_exists('val', $testArrayKeyExists) && is_string($testArrayKeyExists['val'])) { | ||
| return; | ||
| } | ||
| assertType("array{}|array{val: array{42}}", $testArrayKeyExists); | ||
| } |
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.
Isn't an issue to risk to override a sureType for the leftNormalized foreach loop ?