Skip to content

Commit 7279769

Browse files
committed
Key preg_match_all() pattern-order lists by int<0, max>
RegexArrayShapeMatcher built the list of PREG_PATTERN_ORDER matches as an IntersectionType of an int-keyed array and AccessoryArrayListType, which is not normalized. It used to be normalized by the union rules, but since single arrays are left as they are there, list<string> did not accept it. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4a-ca6e-7246-a572-578487f178c3
1 parent f936452 commit 7279769

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎src/Type/Php/RegexArrayShapeMatcher.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPStan\Type\Constant\ConstantIntegerType;
1515
use PHPStan\Type\Constant\ConstantStringType;
1616
use PHPStan\Type\IntegerRangeType;
17-
use PHPStan\Type\IntegerType;
1817
use PHPStan\Type\IntersectionType;
1918
use PHPStan\Type\NullType;
2019
use PHPStan\Type\Regex\RegexCapturingGroup;
@@ -387,7 +386,7 @@ private function createSubjectValueType(Type $baseType, int $flags, bool $matche
387386

388387
if ($this->containsPatternOrder($flags)) {
389388
$accessoryTypes = [
390-
new ArrayType(new IntegerType(), $subjectValueType),
389+
new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $subjectValueType),
391390
new AccessoryArrayListType(),
392391
];
393392
if ($wasMatched->yes()) {

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,6 +3703,11 @@ public function testBug15843(): void
37033703
$this->analyse([__DIR__ . '/data/bug-15843.php'], []);
37043704
}
37053705

3706+
public function testBug15856(): void
3707+
{
3708+
$this->analyse([__DIR__ . '/data/bug-15856.php'], []);
3709+
}
3710+
37063711
public function testBug13164(): void
37073712
{
37083713
$this->checkExplicitMixed = true;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15856;
4+
5+
/** @param list<string> $matches */
6+
function takeList(array $matches): void {}
7+
8+
function f(string $content): void
9+
{
10+
if (preg_match_all('/abc[^#]*#/', $content, $matches)) {
11+
takeList($matches[0]);
12+
}
13+
14+
if (preg_match_all('/abc/', $content, $m2) > 0) {
15+
takeList($m2[0]);
16+
}
17+
}

0 commit comments

Comments
 (0)