Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Copy link
Copy Markdown
Contributor

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 ?

}

$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

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } - if (!$scope->hasExpressionType($targetExpr)->yes()) { + if ($scope->hasExpressionType($targetExpr)->no()) { continue; } if (!$leftFilteredScope->hasExpressionType($targetExpr)->yes()) {

Check warning on line 2117 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } - if (!$scope->hasExpressionType($targetExpr)->yes()) { + if ($scope->hasExpressionType($targetExpr)->no()) { continue; } if (!$leftFilteredScope->hasExpressionType($targetExpr)->yes()) {
continue;
}
if (!$leftFilteredScope->hasExpressionType($targetExpr)->yes()) {

Check warning on line 2120 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if (!$scope->hasExpressionType($targetExpr)->yes()) { continue; } - if (!$leftFilteredScope->hasExpressionType($targetExpr)->yes()) { + if ($leftFilteredScope->hasExpressionType($targetExpr)->no()) { continue; } if (!$rightFilteredScope->hasExpressionType($targetExpr)->yes()) {

Check warning on line 2120 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if (!$scope->hasExpressionType($targetExpr)->yes()) { continue; } - if (!$leftFilteredScope->hasExpressionType($targetExpr)->yes()) { + if ($leftFilteredScope->hasExpressionType($targetExpr)->no()) { continue; } if (!$rightFilteredScope->hasExpressionType($targetExpr)->yes()) {
continue;
}
if (!$rightFilteredScope->hasExpressionType($targetExpr)->yes()) {

Check warning on line 2123 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if (!$leftFilteredScope->hasExpressionType($targetExpr)->yes()) { continue; } - if (!$rightFilteredScope->hasExpressionType($targetExpr)->yes()) { + if ($rightFilteredScope->hasExpressionType($targetExpr)->no()) { continue; }

Check warning on line 2123 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if (!$leftFilteredScope->hasExpressionType($targetExpr)->yes()) { continue; } - if (!$rightFilteredScope->hasExpressionType($targetExpr)->yes()) { + if ($rightFilteredScope->hasExpressionType($targetExpr)->no()) { continue; }
continue;
}

$originalType = $scope->getType($targetExpr);
$leftType = $leftFilteredScope->getType($targetExpr);
$rightType = $rightFilteredScope->getType($targetExpr);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could already continue if !$leftNarrowed without having to compue rightNarrowed


if ($leftType->equals($originalType) || !$originalType->isSuperTypeOf($leftType)->yes()) {

Check warning on line 2131 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $leftType = $leftFilteredScope->getType($targetExpr); $rightType = $rightFilteredScope->getType($targetExpr); - if ($leftType->equals($originalType) || !$originalType->isSuperTypeOf($leftType)->yes()) { + if ($leftType->equals($originalType) || $originalType->isSuperTypeOf($leftType)->no()) { continue; }

Check warning on line 2131 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $leftType = $leftFilteredScope->getType($targetExpr); $rightType = $rightFilteredScope->getType($targetExpr); - if ($leftType->equals($originalType) || !$originalType->isSuperTypeOf($leftType)->yes()) { + if ($leftType->equals($originalType) || $originalType->isSuperTypeOf($leftType)->no()) { continue; }
continue;
}

if ($rightType->equals($originalType) || !$originalType->isSuperTypeOf($rightType)->yes()) {

Check warning on line 2135 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } - if ($rightType->equals($originalType) || !$originalType->isSuperTypeOf($rightType)->yes()) { + if ($rightType->equals($originalType) || $originalType->isSuperTypeOf($rightType)->no()) { continue; }

Check warning on line 2135 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } - if ($rightType->equals($originalType) || !$originalType->isSuperTypeOf($rightType)->yes()) { + if ($rightType->equals($originalType) || $originalType->isSuperTypeOf($rightType)->no()) { continue; }
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[]>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
if ((new \PHPStan\Tests\AssertionClass())->assertString($foo) && \PHPStan\Tests\AssertionClass::assertInt($bar)) {
}

assertType('string|null', $foo);
assertType('string', $foo);
assertType('int|null', $bar);
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
if ((new \PHPStan\Tests\AssertionClass())->assertString($foo) && \PHPStan\Tests\AssertionClass::assertInt($bar)) {
}

assertType('string|null', $foo);
assertType('string', $foo);
assertType('int|null', $bar);
98 changes: 98 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14566.php
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);
}
5 changes: 4 additions & 1 deletion tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,10 @@ public function testBug3632(): void
[
'Instanceof between Bug3632\NiceClass and Bug3632\NiceClass will always evaluate to true.',
36,
$tipText,
],
[
'Instanceof between null and Bug3632\NiceClass will always evaluate to false.',
36,
],
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ public function testBug11903(): void
[
'Negated boolean expression is always true.',
21,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
}
Expand Down
Loading