Skip to content

Commit c8bfe21

Browse files
authored
Fix null-removal on nested captured-arrays (#37)
* Fix null-removeal on nested captured-arrays * Update phpstan-baseline.neon * Update phpstan-baseline.neon
1 parent 0ec200e commit c8bfe21

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ parameters:
5555
count: 2
5656
path: tests/PregTests/ReplaceCallbackArrayTest.php
5757

58-
-
59-
message: "#^Parameter \\#1 \\$str(ing)? of function strtoupper expects string, string\\|null given\\.$#"
60-
count: 2
61-
path: tests/PregTests/ReplaceCallbackTest.php
62-
6358
-
6459
message: "#^Regex pattern is invalid\\: No ending matching delimiter '\\}' found$#"
6560
count: 2

src/PHPStan/PregReplaceCallbackClosureTypeExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
6161
$matchesType = new ConstantArrayType(
6262
$matchesType->getKeyTypes(),
6363
array_map(static function (Type $valueType): Type {
64+
if (count($valueType->getConstantArrays()) === 1) {
65+
$valueTypeArray = $valueType->getConstantArrays()[0];
66+
return new ConstantArrayType(
67+
$valueTypeArray->getKeyTypes(),
68+
array_map(static function (Type $valueType): Type {
69+
return TypeCombinator::removeNull($valueType);
70+
}, $valueTypeArray->getValueTypes()),
71+
$valueTypeArray->getNextAutoIndexes(),
72+
[],
73+
$valueTypeArray->isList()
74+
);
75+
}
6476
return TypeCombinator::removeNull($valueType);
6577
}, $matchesType->getValueTypes()),
6678
$matchesType->getNextAutoIndexes(),

tests/PHPStanTests/nsrt/preg-replace-callback.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ function (string $s): void {
9696
Preg::replaceCallbackStrictGroups(
9797
'/(foo)?(bar)?(baz)?/',
9898
function ($matches) {
99-
// should be array{array{string, int<-1, max>}, array{'foo', int<-1, max>}, array{'bar', int<-1, max>}, array{'baz', int<-1, max>}}
100-
assertType("array{array{string|null, int<-1, max>}, array{'foo'|null, int<-1, max>}, array{'bar'|null, int<-1, max>}, array{'baz'|null, int<-1, max>}}", $matches);
99+
assertType("array{array{string, int<-1, max>}, array{'foo', int<-1, max>}, array{'bar', int<-1, max>}, array{'baz', int<-1, max>}}", $matches);
101100
return '';
102101
},
103102
$s,

0 commit comments

Comments
 (0)