-
-
Notifications
You must be signed in to change notification settings - Fork 15
Trigger error on unsafe StrictGroups usages #29
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4875f9f
Trigger error on unsafe StrictGroups usages
Seldaek eca48c7
Implement new rule to warn about unsafe strict groups calls
Seldaek 7de8962
Apply feedback
Seldaek 088f752
Fix 7.2/7.3 builds
Seldaek 5cdc186
Bump minimum PHPStan version
Seldaek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Composer\Pcre\PHPStan; | ||
|
||
use Composer\Pcre\Preg; | ||
use Composer\Pcre\Regex; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Analyser\SpecifiedTypes; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<StaticCall> | ||
*/ | ||
final class UnsafeStrictGroupsCallRule implements Rule | ||
{ | ||
/** | ||
* @var RegexArrayShapeMatcher | ||
*/ | ||
private $regexShapeMatcher; | ||
|
||
public function __construct(RegexArrayShapeMatcher $regexShapeMatcher) | ||
{ | ||
$this->regexShapeMatcher = $regexShapeMatcher; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return StaticCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->class instanceof FullyQualified) { | ||
return []; | ||
} | ||
$isRegex = $node->class->toString() === Regex::class; | ||
$isPreg = $node->class->toString() === Preg::class; | ||
if (!$isRegex && !$isPreg) { | ||
return []; | ||
} | ||
if (!$node->name instanceof Node\Identifier || !in_array($node->name->name, ['matchStrictGroups', 'isMatchStrictGroups', 'matchAllStrictGroups', 'isMatchAllStrictGroups'], true)) { | ||
return []; | ||
} | ||
|
||
$args = $node->getArgs(); | ||
if (!isset($args[0])) { | ||
return []; | ||
} | ||
|
||
$patternArg = $args[0] ?? null; | ||
if ($isPreg) { | ||
if (!isset($args[2])) { // no matches set, skip as the matches won't be used anyway | ||
return []; | ||
} | ||
$flagsArg = $args[3] ?? null; | ||
} else { | ||
$flagsArg = $args[2] ?? null; | ||
} | ||
|
||
if ($patternArg === null) { | ||
return []; | ||
} | ||
|
||
$flagsType = PregMatchFlags::getType($flagsArg, $scope); | ||
if ($flagsType === null) { | ||
return []; | ||
} | ||
$patternType = $scope->getType($patternArg->value); | ||
|
||
$matchedType = $this->regexShapeMatcher->matchType($patternType, $flagsType, TrinaryLogic::createFromBoolean(true)); | ||
if ($matchedType === null) { | ||
return [ | ||
RuleErrorBuilder::message(sprintf('The %s call is potentially unsafe as $matches\' type could not be inferred.', $node->name->name)) | ||
->identifier('composerPcre.maybeUnsafeStrictGroups') | ||
->build(), | ||
]; | ||
} | ||
|
||
if (count($matchedType->getConstantArrays()) === 1) { | ||
$matchedType = $matchedType->getConstantArrays()[0]; | ||
$nullableGroups = []; | ||
foreach ($matchedType->getValueTypes() as $index => $type) { | ||
if (TypeCombinator::containsNull($type)) { | ||
$nullableGroups[] = $matchedType->getKeyTypes()[$index]->getValue(); | ||
} | ||
} | ||
|
||
if (\count($nullableGroups) > 0) { | ||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'The %s call is unsafe as match group%s "%s" %s optional and may be null.', | ||
$node->name->name, | ||
\count($nullableGroups) > 1 ? 's' : '', | ||
implode('", "', $nullableGroups), | ||
\count($nullableGroups) > 1 ? 'are' : 'is' | ||
))->identifier('composerPcre.unsafeStrictGroups')->build(), | ||
]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of composer/pcre. | ||
* | ||
* (c) Composer <https://github.com/composer> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Composer\Pcre\PHPStanTests; | ||
|
||
use PHPStan\Testing\RuleTestCase; | ||
use Composer\Pcre\PHPStan\UnsafeStrictGroupsCallRule; | ||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
use PHPStan\Php\PhpVersion; | ||
|
||
/** | ||
* Run with "vendor/bin/phpunit --testsuite phpstan" | ||
* | ||
* This is excluded by default to avoid side effects with the library tests | ||
* | ||
* @extends RuleTestCase<UnsafeStrictGroupsCallRule> | ||
*/ | ||
class UnsafeStrictGruopsCallRuleTest extends RuleTestCase | ||
{ | ||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
// @phpstan-ignore phpstanApi.constructor,phpstanApi.constructor | ||
Seldaek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new UnsafeStrictGroupsCallRule(new RegexArrayShapeMatcher(new PhpVersion(PHP_VERSION_ID, PhpVersion::SOURCE_RUNTIME))); | ||
Seldaek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/nsrt/preg-match.php'], [ | ||
[ | ||
'The matchStrictGroups call is unsafe as match group "1" is optional and may be null.', | ||
80, | ||
], | ||
[ | ||
'The matchAllStrictGroups call is unsafe as match groups "foo", "2" are optional and may be null.', | ||
82, | ||
], | ||
[ | ||
'The isMatchStrictGroups call is potentially unsafe as $matches\' type could not be inferred.', | ||
86, | ||
], | ||
]); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
'phar://' . __DIR__ . '/../../vendor/phpstan/phpstan/phpstan.phar/conf/bleedingEdge.neon', | ||
__DIR__ . '/../../extension.neon', | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.