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 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
36 changes: 34 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,26 @@ public function specifyTypesInCondition(
if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}

$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()) {
$leftIsAlwaysTrue = $scope->getType($expr->left)->isTrue()->yes();
$rightIsAlwaysTrue = $scope->getType($expr->right)->isTrue()->yes();

if ($leftIsAlwaysTrue && !$rightIsAlwaysTrue) {
return $context->true() ? $rightTypes : $rightTypes->normalize($rightScope);
}
if ($rightIsAlwaysTrue && !$leftIsAlwaysTrue) {
return $context->true() ? $leftTypes : $leftTypes->normalize($scope);
}
}

$types = $context->true()
? $leftTypes->unionWith($rightTypes)
: $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope));
if ($context->false()) {
return new SpecifiedTypes(
$types->getSureTypes(),
Expand All @@ -599,10 +615,26 @@ public function specifyTypesInCondition(
if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}

$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()) {
$leftIsAlwaysFalse = $scope->getType($expr->left)->isFalse()->yes();
$rightIsAlwaysFalse = $scope->getType($expr->right)->isFalse()->yes();

if ($leftIsAlwaysFalse && !$rightIsAlwaysFalse) {
return $context->true() ? $rightTypes->normalize($rightScope) : $rightTypes;
}
if ($rightIsAlwaysFalse && !$leftIsAlwaysFalse) {
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];
}
}
}
}
Loading