Skip to content

Commit

Permalink
Apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jul 12, 2024
1 parent eca48c7 commit 7de8962
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/PHPStan/UnsafeStrictGroupsCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function processNode(Node $node, Scope $scope): array
}
$patternType = $scope->getType($patternArg->value);

$matchedType = $this->regexShapeMatcher->matchType($patternType, $flagsType, TrinaryLogic::createFromBoolean(true));
$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))
Expand Down
4 changes: 1 addition & 3 deletions tests/PHPStanTests/UnsafeStrictGroupsCallRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
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"
Expand All @@ -27,8 +26,7 @@ class UnsafeStrictGruopsCallRuleTest extends RuleTestCase
{
protected function getRule(): \PHPStan\Rules\Rule
{
// @phpstan-ignore phpstanApi.constructor,phpstanApi.constructor
return new UnsafeStrictGroupsCallRule(new RegexArrayShapeMatcher(new PhpVersion(PHP_VERSION_ID, PhpVersion::SOURCE_RUNTIME)));
return new UnsafeStrictGroupsCallRule(self::getContainer()->getByType(RegexArrayShapeMatcher::class));
}

public function testRule(): void
Expand Down
28 changes: 14 additions & 14 deletions tests/PHPStanTests/nsrt/preg-match.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ function doMatch(string $s): void
}
assertType('array{}|array{string, string}', $matches);

if (Preg::match('/Price: (£|€)?\d+/', $s, $matches)) {
assertType('array{0: string, 1: string|null}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{0: string, 1: string|null}', $matches);

// passing the PREG_UNMATCHED_AS_NULL should change nothing compared to above as it is always set
if (Preg::match('/Price: (£|€)?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: string, 1: string|null}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{0: string, 1: string|null}', $matches);
// if (Preg::match('/Price: (£|€)?\d+/', $s, $matches)) { // temporarily disabled until https://github.com/phpstan/phpstan-src/pull/3229 is released
// assertType('array{0: string, 1: string|null}', $matches);
// } else {
// assertType('array{}', $matches);
// }
// assertType('array{}|array{0: string, 1: string|null}', $matches);
//
// // passing the PREG_UNMATCHED_AS_NULL should change nothing compared to above as it is always set
// if (Preg::match('/Price: (£|€)?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
// assertType('array{0: string, 1: string|null}', $matches);
// } else {
// assertType('array{}', $matches);
// }
// assertType('array{}|array{0: string, 1: string|null}', $matches);

if (Preg::isMatch('/Price: (?<currency>£|€)\d+/', $s, $matches)) {
assertType('array{0: string, currency: string, 1: string}', $matches);
Expand Down

0 comments on commit 7de8962

Please sign in to comment.