-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trigger error on unsafe StrictGroups usages (#29)
Also fix issues on php 7.2/7.3
- Loading branch information
Showing
11 changed files
with
227 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 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::createYes()); | ||
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 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 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 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,57 @@ | ||
<?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; | ||
|
||
/** | ||
* 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 | ||
{ | ||
return new UnsafeStrictGroupsCallRule(self::getContainer()->getByType(RegexArrayShapeMatcher::class)); | ||
} | ||
|
||
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 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 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 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 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