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

Fix PHPStan extension for matchAllStrictGroups #38

Merged
merged 4 commits into from
Aug 27, 2024
Merged

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Aug 26, 2024

closes #34

@@ -82,19 +82,9 @@ public function specifyTypes(MethodReflection $methodReflection, StaticCall $nod
}

if (
in_array($methodReflection->getName(), ['matchStrictGroups', 'isMatchStrictGroups'], true)
&& count($matchedType->getConstantArrays()) === 1
in_array($methodReflection->getName(), ['matchStrictGroups', 'isMatchStrictGroups', 'matchAllStrictGroups', 'isMatchAllStrictGroups'], true)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this IF case was missing 'matchAllStrictGroups', 'isMatchAllStrictGroups'

Copy link
Member

@Seldaek Seldaek Aug 27, 2024

Choose a reason for hiding this comment

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

Yeah I tried that without success tho. But now with the traverser looks much better, thanks!

@@ -34,4 +39,32 @@ static public function getType(?Arg $flagsArg, Scope $scope): ?Type
}
return TypeCombinator::union(...$internalFlagsTypes);
}

static public function removeNullFromMatches(Type $matchesType): Type
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved this out into the utility class to ease re-use without duplication

@@ -44,6 +44,10 @@ public function testRule(): void
'The isMatchStrictGroups call is potentially unsafe as $matches\' type could not be inferred.',
86,
],
[
'The isMatchAllStrictGroups call is unsafe as match groups "test", "1" are optional and may be null.',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not 100% sure this error is expected, as I don't got the use-case of the rule

Copy link
Member

Choose a reason for hiding this comment

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

This new warning is correct/fine.

Copy link
Member

@Seldaek Seldaek Aug 27, 2024

Choose a reason for hiding this comment

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

The point of StrictGroups methods is, the method returns only non-null matches, but if one is null it throws, so it isn't safe to use with nullable match groups, which is what this warns about. In the test though you want nullable match groups to ensure they appear not-null in $matches.

@staabm
Copy link
Contributor Author

staabm commented Aug 27, 2024

//cc @Seldaek

@Seldaek Seldaek merged commit 26859a8 into composer:2.x Aug 27, 2024
13 checks passed
@Seldaek
Copy link
Member

Seldaek commented Aug 27, 2024

Great thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants