-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Strict groups support #27
Conversation
4b2e643
to
9918fa8
Compare
wdyt about $ git diff
diff --git a/src/PHPStan/PregMatchTypeSpecifyingExtension.php b/src/PHPStan/PregMatchTypeSpecifyingExtension.php
index b83ed12..7ee7ec5 100644
--- a/src/PHPStan/PregMatchTypeSpecifyingExtension.php
+++ b/src/PHPStan/PregMatchTypeSpecifyingExtension.php
@@ -11,8 +11,11 @@
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\TrinaryLogic;
+use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Php\RegexArrayShapeMatcher;
use PHPStan\Type\StaticMethodTypeSpecifyingExtension;
+use PHPStan\Type\TypeCombinator;
+use PHPStan\Type\Type;
final class PregMatchTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
@@ -70,6 +73,22 @@ public function specifyTypes(MethodReflection $methodReflection, StaticCall $nod
return new SpecifiedTypes();
}
+ if (
+ in_array($methodReflection->getName(), ['matchStrictGroups', 'isMatchStrictGroups'], true)
+ && $matchedType instanceof ConstantArrayType
+ ) {
+ $matchedType = new ConstantArrayType(
+ $matchedType->getKeyTypes(),
+ array_map(static function (Type $valueType): Type {
+ return TypeCombinator::removeNull($valueType);
+
+ }, $matchedType->getValueTypes()),
+ [0],
+ [],
+ $matchedType->isList()
+ );
+ }
+
$overwrite = false;
if ($context->false()) {
$overwrite = true; I am not 100% sure, I got the difference of the above patch removes all nullable types and also turns all groups into mandatory groups. |
I wonder whether this even means |
Yeah that looks great thanks!
Hmm if it matches it cannot, but it is still allowed to NOT match. It only throws if it matches but some groups are null, thus improving the type safety within the code handling the matches as we know we don't have to check for nulls.. Now with better support for optional groups etc in PHPStan this whole thing might be less needed or not at all anymore tbh. I'll have to see. |
BTW there was a deprecation warning on |
Woops, that does not go so well on the composer repo:
|
how to reproduce? |
in the composer project:
And add Then |
@staabm
I am not sure how to do the non-null part. Maybe it's doable by modifying the result of
$this->regexShapeMatcher->matchType()
to ensurestring|null
becomesstring
.. but I suck at phpstan internals :DIf you have a sec to help any time it'd be great, I believe the tests are correct.