Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support matchAll* variants in PHPStan extensions #32

Merged
merged 1 commit into from
Aug 19, 2024
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"require-dev": {
"phpunit/phpunit": "^8 || ^9",
"phpstan/phpstan": "^1.11.8",
"phpstan/phpstan": "^1.11.9",
"phpstan/phpstan-strict-rules": "^1.1"
},
"conflict": {
Expand Down
9 changes: 8 additions & 1 deletion src/PHPStan/PregMatchParameterOutTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function isStaticMethodSupported(MethodReflection $methodReflection, Para
{
return
$methodReflection->getDeclaringClass()->getName() === Preg::class
&& in_array($methodReflection->getName(), ['match', 'isMatch', 'matchStrictGroups', 'isMatchStrictGroups'], true)
&& in_array($methodReflection->getName(), [
'match', 'isMatch', 'matchStrictGroups', 'isMatchStrictGroups',
'matchAll', 'isMatchAll', 'matchAllStrictGroups', 'isMatchAllStrictGroups'
], true)
&& $parameter->getName() === 'matches';
}

Expand All @@ -52,6 +55,10 @@ public function getParameterOutTypeFromStaticMethodCall(MethodReflection $method
return null;
}

if (stripos($methodReflection->getName(), 'matchAll') !== false) {
return $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope);
Copy link
Member

Choose a reason for hiding this comment

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

This probably should be conditional on the matchAllExpr method existing, otherwise older phpstan version will break.. So either that or we need to bump the phpstan/phpstan conflict rule?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree.

I am on vacation atm - cannot work on it now

}

return $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope);
}

Expand Down
13 changes: 11 additions & 2 deletions src/PHPStan/PregMatchTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public function getClass(): string

public function isStaticMethodSupported(MethodReflection $methodReflection, StaticCall $node, TypeSpecifierContext $context): bool
{
return in_array($methodReflection->getName(), ['match', 'isMatch', 'matchStrictGroups', 'isMatchStrictGroups'], true) && !$context->null();
return in_array($methodReflection->getName(), [
'match', 'isMatch', 'matchStrictGroups', 'isMatchStrictGroups',
'matchAll', 'isMatchAll', 'matchAllStrictGroups', 'isMatchAllStrictGroups'
], true)
&& !$context->null();
}

public function specifyTypes(MethodReflection $methodReflection, StaticCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
Expand All @@ -67,7 +71,12 @@ public function specifyTypes(MethodReflection $methodReflection, StaticCall $nod
return new SpecifiedTypes();
}

$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope);
if (stripos($methodReflection->getName(), 'matchAll') !== false) {
$matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope);
Copy link
Member

Choose a reason for hiding this comment

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

Same here.. Probably easiest to bump the conflict rule tbh, not worth having ugly code here.

Copy link
Member

Choose a reason for hiding this comment

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

Ok I did this myself in a follow-up commit. Thanks for the PR! I guess preg_replace_callback could also be done now with phpstan 1.11.10?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

} else {
$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope);
}

if ($matchedType === null) {
return new SpecifiedTypes();
}
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStanTests/nsrt/preg-match.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ function doMatchStrictGroupsUnsafe(string $s): void
}
}

function doMatchAllStrictGroups(string $s): void
{
if (Preg::matchAllStrictGroups('/Price: /i', $s, $matches)) {
assertType('array{list<string>}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{list<string>}', $matches);

if (Preg::matchAllStrictGroups('/Price: (£|€)\d+/', $s, $matches)) {
assertType('array{list<string>, list<non-empty-string>}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{list<string>, list<non-empty-string>}', $matches);

if (Preg::isMatchAllStrictGroups('/Price: (?<test>£|€)\d+/', $s, $matches)) {
assertType('array{0: list<string>, test: list<non-empty-string>, 1: list<non-empty-string>}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{0: list<string>, test: list<non-empty-string>, 1: list<non-empty-string>}', $matches);
}

// disabled until https://github.com/phpstan/phpstan-src/pull/3185 can be resolved
//
//function identicalMatch(string $s): void
Expand Down
Loading