Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ jobs:
run: |
php build-infection/bin/infection-config.php \
--source-directory='build/PHPStan/Build' \
--timeout=180 \
Copy link
Contributor Author

@staabm staabm Oct 29, 2025

Choose a reason for hiding this comment

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

in the worst case infection needs enough time to run the full test-suite.
otherwise mutants are considered 'skipped', which we saw in CI:

grafik

this setting means: "how many seconds can infection spent on a single mutant"

Copy link
Contributor Author

@staabm staabm Oct 29, 2025

Choose a reason for hiding this comment

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

opened a new ticket, as we need more information to proceed.
infection/infection#2510

even after raising the timeout to 300 it still times out.
so at phpstan-src repo we are so slow, that we can't even check 3 mutants in 15minutes CI runtime.

we can merge for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lets fix this in a separate PR

> infection.json5
cat infection.json5 | jq

Expand Down
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