Skip to content

Commit

Permalink
Fix null-removal on nested captured-arrays (#37)
Browse files Browse the repository at this point in the history
* Fix null-removeal on nested captured-arrays

* Update phpstan-baseline.neon

* Update phpstan-baseline.neon
  • Loading branch information
staabm authored Aug 23, 2024
1 parent 0ec200e commit c8bfe21
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ parameters:
count: 2
path: tests/PregTests/ReplaceCallbackArrayTest.php

-
message: "#^Parameter \\#1 \\$str(ing)? of function strtoupper expects string, string\\|null given\\.$#"
count: 2
path: tests/PregTests/ReplaceCallbackTest.php

-
message: "#^Regex pattern is invalid\\: No ending matching delimiter '\\}' found$#"
count: 2
Expand Down
12 changes: 12 additions & 0 deletions src/PHPStan/PregReplaceCallbackClosureTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
$matchesType = new ConstantArrayType(
$matchesType->getKeyTypes(),
array_map(static function (Type $valueType): Type {
if (count($valueType->getConstantArrays()) === 1) {
$valueTypeArray = $valueType->getConstantArrays()[0];
return new ConstantArrayType(
$valueTypeArray->getKeyTypes(),
array_map(static function (Type $valueType): Type {
return TypeCombinator::removeNull($valueType);
}, $valueTypeArray->getValueTypes()),
$valueTypeArray->getNextAutoIndexes(),
[],
$valueTypeArray->isList()
);
}
return TypeCombinator::removeNull($valueType);
}, $matchesType->getValueTypes()),
$matchesType->getNextAutoIndexes(),
Expand Down
3 changes: 1 addition & 2 deletions tests/PHPStanTests/nsrt/preg-replace-callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ function (string $s): void {
Preg::replaceCallbackStrictGroups(
'/(foo)?(bar)?(baz)?/',
function ($matches) {
// should be array{array{string, int<-1, max>}, array{'foo', int<-1, max>}, array{'bar', int<-1, max>}, array{'baz', int<-1, max>}}
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);
assertType("array{array{string, int<-1, max>}, array{'foo', int<-1, max>}, array{'bar', int<-1, max>}, array{'baz', int<-1, max>}}", $matches);
return '';
},
$s,
Expand Down

0 comments on commit c8bfe21

Please sign in to comment.