Skip to content
Merged
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
19 changes: 18 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,24 @@ public function specifyTypesInCondition(
$leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context)->setRootExpr($expr);
$rightScope = $scope->filterByFalseyValue($expr->left);
$rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context)->setRootExpr($expr);
$types = $context->true() ? $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope)) : $leftTypes->unionWith($rightTypes);

if ($context->true()) {
if (
$scope->getType($expr->left)->toBoolean()->isFalse()->yes()
) {
$types = $rightTypes->normalize($rightScope);
} elseif (
$scope->getType($expr->left)->toBoolean()->isTrue()->yes()
|| $scope->getType($expr->right)->toBoolean()->isFalse()->yes()
) {
$types = $leftTypes->normalize($scope);
} else {
$types = $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope));
}
} else {
$types = $leftTypes->unionWith($rightTypes);
}

if ($context->true()) {
return (new SpecifiedTypes(
$types->getSureTypes(),
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public static function dataCondition(): iterable
self::createFunctionCall('is_int', 'foo'),
self::createFunctionCall('is_string', 'bar'),
),
[],
['$foo' => 'int'],
['$foo' => '~int', '$bar' => '~string'],
],
[
Expand Down Expand Up @@ -652,7 +652,7 @@ public static function dataCondition(): iterable
[
new Expr\Empty_(new Variable('array')),
[
'$array' => 'array{}|null',
'$array' => 'array{}',
],
[
'$array' => '~0|0.0|\'\'|\'0\'|array{}|false|null',
Expand All @@ -664,7 +664,7 @@ public static function dataCondition(): iterable
'$array' => '~0|0.0|\'\'|\'0\'|array{}|false|null',
],
[
'$array' => 'array{}|null',
'$array' => 'array{}',
],
],
[
Expand Down
59 changes: 59 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11276.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Bug11276;

use function PHPStan\Testing\assertType;

function doFoo(int $i, int $j, $arr): void
{
if (false || array_key_exists($i, $arr)) {
assertType('non-empty-array', $arr);
}

if (array_key_exists($i, $arr) || false) {
assertType('non-empty-array', $arr);
}

if ("0" || array_key_exists($i, $arr)) {
assertType('non-empty-array', $arr);
}

if (array_key_exists($i, $arr) || "0") {
assertType('non-empty-array', $arr);
}

if (true || array_key_exists($i, $arr)) {
assertType('mixed', $arr);
}

if (array_key_exists($i, $arr) || true) {
assertType('mixed', $arr);
}

if ("1" || array_key_exists($i, $arr)) {
assertType('mixed', $arr);
}

if (array_key_exists($i, $arr) || "1") {
assertType('mixed', $arr);
}

if (array_key_exists($i, $arr) || array_key_exists($j, $arr)) {
assertType('non-empty-array', $arr);
}

if (!array_key_exists($j, $arr)) {
if (array_key_exists($i, $arr) || array_key_exists($j, $arr)) {
assertType('non-empty-array', $arr);
}
}
if (!array_key_exists($i, $arr)) {
if (array_key_exists($i, $arr) || array_key_exists($j, $arr)) {
assertType('non-empty-array', $arr);
}
}

if (array_key_exists($i, $arr)) {
assertType('non-empty-array', $arr);
}
}
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11276b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php // lint >= 8.0

namespace Bug11276b;

use function PHPStan\Testing\assertType;

function doBar($j, array $arr) {
$i = 1;
if (!array_key_exists($i, $arr)) {
if (array_key_exists($i, $arr) || array_key_exists($j, $arr)) {
assertType('non-empty-array<mixed~1, mixed>', $arr);
}
}

if (!array_key_exists($i, $arr)) {
if (array_key_exists($j, $arr) || array_key_exists($i, $arr)) {
assertType('non-empty-array<mixed~1, mixed>', $arr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1132,4 +1132,10 @@ public function testBug6209(): void
$this->analyse([__DIR__ . '/data/bug-6209.php'], []);
}

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

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-11276.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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];
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public function testReportPhpDoc(): void
[
'Right side of || is always true.',
33,
$tipText,
],
]);
}
Expand Down
Loading