Skip to content

Handle condition with always true/false sub-condition #3313

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

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
75 changes: 63 additions & 12 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,21 +575,49 @@ public function specifyTypesInCondition(
if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}
$leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context, $rootExpr);

if ($context->falsey() && $scope->getType($expr->left)->isTrue()->yes()) {
$leftTypes = null;
} else {
$leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context, $rootExpr);
}

$rightScope = $scope->filterByTruthyValue($expr->left);
$rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context, $rootExpr);
$types = $context->true() ? $leftTypes->unionWith($rightTypes) : $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope));
if ($context->falsey() && $scope->getType($expr->right)->isTrue()->yes()) {
$rightTypes = null;
} else {
$rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context, $rootExpr);
}

if ($leftTypes === null && $rightTypes === null) {
$types = new SpecifiedTypes([], [], false, [], $expr);
} elseif ($leftTypes === null) {
$types = $context->true() ? $rightTypes : $rightTypes->normalize($rightScope);
} elseif ($rightTypes === null) {
$types = $context->true() ? $leftTypes : $leftTypes->normalize($scope);
} else {
$types = $context->true()
? $leftTypes->unionWith($rightTypes)
: $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope));
}

if ($context->false()) {
return new SpecifiedTypes(
$types->getSureTypes(),
$types->getSureNotTypes(),
false,
array_merge(
if ($leftTypes === null || $rightTypes === null) {
$conditionalTypes = [];
} else {
$conditionalTypes = array_merge(
$this->processBooleanNotSureConditionalTypes($scope, $leftTypes, $rightTypes),
$this->processBooleanNotSureConditionalTypes($scope, $rightTypes, $leftTypes),
$this->processBooleanSureConditionalTypes($scope, $leftTypes, $rightTypes),
$this->processBooleanSureConditionalTypes($scope, $rightTypes, $leftTypes),
),
);
}

return new SpecifiedTypes(
$types->getSureTypes(),
$types->getSureNotTypes(),
false,
$conditionalTypes,
$rootExpr,
);
}
Expand All @@ -599,10 +627,33 @@ public function specifyTypesInCondition(
if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}
$leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context, $rootExpr);

if ($context->truthy() && $scope->getType($expr->left)->isFalse()->yes()) {
$leftTypes = null;
} else {
$leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context, $rootExpr);
}

$rightScope = $scope->filterByFalseyValue($expr->left);
$rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context, $rootExpr);
$types = $context->true() ? $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope)) : $leftTypes->unionWith($rightTypes);
if ($context->truthy() && $scope->getType($expr->right)->isFalse()->yes()) {
$rightTypes = null;
} else {
$rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context, $rootExpr);
}

if ($leftTypes === null && $rightTypes === null) {
return new SpecifiedTypes([], [], false, [], $expr);
}
if ($leftTypes === null) {
return $context->true() ? $rightTypes->normalize($rightScope) : $rightTypes;
}
if ($rightTypes === null) {
return $context->true() ? $leftTypes->normalize($scope) : $leftTypes;
}

$types = $context->true()
? $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope))
: $leftTypes->unionWith($rightTypes);
if ($context->true()) {
return new SpecifiedTypes(
$types->getSureTypes(),
Expand Down
34 changes: 31 additions & 3 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ protected function setUp(): void
$this->scope = $this->scope->assignVariable('barOrFalse', new UnionType([new ObjectType('Bar'), new ConstantBooleanType(false)]), new UnionType([new ObjectType('Bar'), new ConstantBooleanType(false)]));
$this->scope = $this->scope->assignVariable('stringOrFalse', new UnionType([new StringType(), new ConstantBooleanType(false)]), new UnionType([new StringType(), new ConstantBooleanType(false)]));
$this->scope = $this->scope->assignVariable('array', new ArrayType(new MixedType(), new MixedType()), new ArrayType(new MixedType(), new MixedType()));
$this->scope = $this->scope->assignVariable('arrayOrNull', new UnionType([new ArrayType(new MixedType(), new MixedType()), new NullType()]), new UnionType([new ArrayType(new MixedType(), new MixedType()), new NullType()]));
$this->scope = $this->scope->assignVariable('foo', new MixedType(), new MixedType());
$this->scope = $this->scope->assignVariable('mixed', new MixedType(), new MixedType());
$this->scope = $this->scope->assignVariable('classString', new ClassStringType(), new ClassStringType());
$this->scope = $this->scope->assignVariable('genericClassString', new GenericClassStringType(new ObjectType('Bar')), new GenericClassStringType(new ObjectType('Bar')));
$this->scope = $this->scope->assignVariable('object', new ObjectWithoutClassType(), new ObjectWithoutClassType());
Expand Down Expand Up @@ -349,9 +351,17 @@ public function dataCondition(): iterable
$this->createFunctionCall('is_int', 'foo'),
$this->createFunctionCall('is_string', 'bar'),
),
[],
['$foo' => 'int'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since bar is an object

$this->scope = $this->scope->assignVariable('bar', new ObjectType('Bar'), new ObjectType('Bar'));

we can be sure foo is an int.

I added the same test with is_int($foo) || is_string($mixed) to reproduce the old behavior.

['$foo' => '~int', '$bar' => '~string'],
],
[
new Expr\BinaryOp\BooleanOr(
$this->createFunctionCall('is_int', 'foo'),
$this->createFunctionCall('is_string', 'mixed'),
),
[],
['$foo' => '~int', '$mixed' => '~string'],
],
[
new Expr\BinaryOp\BooleanAnd(
new Expr\BinaryOp\BooleanOr(
Expand Down Expand Up @@ -648,19 +658,37 @@ public function dataCondition(): iterable
[
new Expr\Empty_(new Variable('array')),
[
'$array' => 'array{}|null',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since array is defined as an non nullable array

$this->scope = $this->scope->assignVariable('array', new ArrayType(new MixedType(), new MixedType()), new ArrayType(new MixedType(), new MixedType()));

we can be sure it's an array.

'$array' => 'array{}',
],
[
'$array' => '~0|0.0|\'\'|\'0\'|array{}|false|null',
],
],
[
new Expr\Empty_(new Variable('arrayOrNull')),
[
'$arrayOrNull' => 'array{}|null',
],
[
'$arrayOrNull' => '~0|0.0|\'\'|\'0\'|array{}|false|null',
],
],
[
new BooleanNot(new Expr\Empty_(new Variable('array'))),
[
'$array' => '~0|0.0|\'\'|\'0\'|array{}|false|null',
],
[
'$array' => 'array{}|null',
'$array' => 'array{}',
],
],
[
new BooleanNot(new Expr\Empty_(new Variable('arrayOrNull'))),
[
'$arrayOrNull' => '~0|0.0|\'\'|\'0\'|array{}|false|null',
],
[
'$arrayOrNull' => 'array{}|null',
],
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,4 +864,10 @@ public function testBug10997(): void
]);
}

public function testBug11276(): void
{
$this->reportPossiblyNonexistentConstantArrayOffset = true;
$this->analyse([__DIR__ . '/data/bug-11276.php'], []);
}

}
40 changes: 40 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-11276.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace Bug11276;

class HelloWorld
{
/**
* @param array{from: string, to: string} $expected
*/
public function testLanguagesMatchingRegex(string $url, ?array $expected): void
{
preg_match('#\/(?<from>[a-z]{2})-(?<to>[a-z]{1}[a-z0-9]{1})\/#', $url, $matches);

foreach ($expected as $key => $value) {
if ($matches instanceof ArrayAccess || \array_key_exists($key, $matches)) {
$matches[$key];
}
}

foreach ($expected as $key => $value) {
if (\array_key_exists($key, $matches) || $matches instanceof ArrayAccess) {
$matches[$key];
}
}

foreach ($expected as $key => $value) {
if (!$matches instanceof ArrayAccess && !\array_key_exists($key, $matches)) {
} else {
$matches[$key];
}
}

foreach ($expected as $key => $value) {
if (!\array_key_exists($key, $matches) && !$matches instanceof ArrayAccess) {
} else {
$matches[$key];
}
}
}
}
8 changes: 0 additions & 8 deletions tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ public function testInstanceof(): void
232,
$tipText,
],
[
'Instanceof between *NEVER* and ImpossibleInstanceOf\Foo will always evaluate to false.',
234,
],
[
'Instanceof between ImpossibleInstanceOf\Bar&ImpossibleInstanceOf\Foo and ImpossibleInstanceOf\Foo will always evaluate to true.',
238,
Expand Down Expand Up @@ -232,10 +228,6 @@ public function testInstanceofWithoutAlwaysTrue(): void
'Instanceof between *NEVER* and ImpossibleInstanceOf\Lorem will always evaluate to false.',
228,
],
[
'Instanceof between *NEVER* and ImpossibleInstanceOf\Foo will always evaluate to false.',
234,
],
[
'Instanceof between *NEVER* and ImpossibleInstanceOf\Bar will always evaluate to false.',
240,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ public function testRule(): void
64,
//$tipText,
],
[
'Result of && is always false.',
66,
//$tipText,
],
[
'Result of && is always false.',
125,
Expand Down Expand Up @@ -190,11 +185,6 @@ public function testRuleLogicalAnd(): void
64,
//$tipText,
],
[
'Result of && is always false.',
66,
//$tipText,
],
[
'Result of && is always false.',
125,
Expand Down Expand Up @@ -274,11 +264,6 @@ public function testRuleLogicalAndBleedingEdge(): void
64,
//$tipText,
],
[
'Result of and is always false.',
66,
//$tipText,
],
[
'Result of and is always false.',
125,
Expand Down
Loading