Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions src/Type/Php/PregMatchTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
Expand Down Expand Up @@ -41,13 +43,34 @@
{
$args = $node->getArgs();
$patternArg = $args[0] ?? null;
$subjectArg = $args[1] ?? null;
$matchesArg = $args[2] ?? null;
$flagsArg = $args[3] ?? null;

if ($patternArg === null) {
return new SpecifiedTypes();
}

$subjectTypes = new SpecifiedTypes();
if (
$patternArg === null || $matchesArg === null
$subjectArg !== null
&& $context->true()
&& $scope->getType($subjectArg->value)->isString()->yes()

Check warning on line 58 in src/Type/Php/PregMatchTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ( $subjectArg !== null && $context->true() - && $scope->getType($subjectArg->value)->isString()->yes() + && !$scope->getType($subjectArg->value)->isString()->no() && !$this->isSubExprOfMatchesArg($subjectArg->value, $matchesArg !== null ? $matchesArg->value : null) ) { $subjectType = $this->regexShapeMatcher->matchSubjectExpr($patternArg->value, $scope);

Check warning on line 58 in src/Type/Php/PregMatchTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ( $subjectArg !== null && $context->true() - && $scope->getType($subjectArg->value)->isString()->yes() + && !$scope->getType($subjectArg->value)->isString()->no() && !$this->isSubExprOfMatchesArg($subjectArg->value, $matchesArg !== null ? $matchesArg->value : null) ) { $subjectType = $this->regexShapeMatcher->matchSubjectExpr($patternArg->value, $scope);
&& !$this->isSubExprOfMatchesArg($subjectArg->value, $matchesArg !== null ? $matchesArg->value : null)
) {
return new SpecifiedTypes();
$subjectType = $this->regexShapeMatcher->matchSubjectExpr($patternArg->value, $scope);
if ($subjectType !== null) {
$subjectTypes = $this->typeSpecifier->create(
$subjectArg->value,
$subjectType,
$context,
$scope,
)->setRootExpr($node);
}
}

if ($matchesArg === null) {
return $subjectTypes;
}

$flagsType = null;
Expand All @@ -69,7 +92,7 @@
$matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, $wasMatched, $scope);
}
if ($matchedType === null) {
return new SpecifiedTypes();
return $subjectTypes;
}

$overwrite = false;
Expand All @@ -88,7 +111,21 @@
$types = $types->setAlwaysOverwriteTypes();
}

return $types;
return $types->unionWith($subjectTypes);
}

private function isSubExprOfMatchesArg(Expr $subject, ?Expr $matchesVar): bool
{
if ($matchesVar === null) {
return false;
}
$rootVar = $subject;
while ($rootVar instanceof ArrayDimFetch) {
$rootVar = $rootVar->var;
}
return $rootVar instanceof Expr\Variable
&& $matchesVar instanceof Expr\Variable
&& $rootVar->name === $matchesVar->name;
}

}
21 changes: 21 additions & 0 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ public function matchExpr(Expr $patternExpr, ?Type $flagsType, TrinaryLogic $was
return $this->matchPatternType($this->getPatternType($patternExpr, $scope), $flagsType, $wasMatched, false);
}

public function matchSubjectExpr(Expr $patternExpr, Scope $scope): ?Type
{
$patternType = $this->getPatternType($patternExpr, $scope);
$constantStrings = $patternType->getConstantStrings();
if (count($constantStrings) === 0) {
return null;
}

$subjectTypes = [];
foreach ($constantStrings as $constantString) {
$astWalkResult = $this->regexGroupParser->parseGroups($constantString->getValue());
if ($astWalkResult === null) {
return null;
}

$subjectTypes[] = $astWalkResult->getSubjectBaseType();
}

return TypeCombinator::union(...$subjectTypes);
}

private function matchPatternType(Type $patternType, ?Type $flagsType, TrinaryLogic $wasMatched, bool $matchesAll): ?Type
{
if ($wasMatched->no()) {
Expand Down
110 changes: 110 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14710.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php declare(strict_types = 1);

namespace Bug14710;

use function PHPStan\Testing\assertType;

function ternaryPregMatch(string $x): void {
(preg_match('/^(a|b|c)$/', $x)) ?
assertType('non-falsy-string', $x)
: assertType('string', $x);
}

function ifPregMatch(string $x): void {
if (preg_match('/^(a|b|c)$/', $x)) {
assertType('non-falsy-string', $x);
} else {
assertType('string', $x);
}
}

function ternaryPregMatchWithMatches(string $x): void {
(preg_match('/^(a|b|c)$/', $x, $matches)) ?
assertType('non-falsy-string', $x)
: assertType('string', $x);
}

function ifPregMatchWithMatches(string $x): void {
if (preg_match('/^(a|b|c)$/', $x, $matches)) {
assertType('non-falsy-string', $x);
} else {
assertType('string', $x);
}
}

function pregMatchNonEmpty(string $x): void {
if (preg_match('/foo/', $x)) {
assertType('non-falsy-string', $x);
}
}

function pregMatchNoNarrow(string $x): void {
if (preg_match('/^$/', $x)) {
assertType('string', $x);
}
}

function pregMatchAll(string $x): void {
if (preg_match_all('/^(a|b|c)$/', $x)) {
assertType('non-falsy-string', $x);
} else {
assertType('string', $x);
}
}

function negatedPregMatch(string $x): void {
if (!preg_match('/^(a|b|c)$/', $x)) {
assertType('string', $x);
} else {
assertType('non-falsy-string', $x);
}
}

function pregMatchCompare(string $x): void {
if (preg_match('/^(a|b|c)$/', $x) === 1) {
assertType('non-falsy-string', $x);
}
}

function pregMatchNotIdentical(string $x): void {
if (preg_match('#ExtensionInterface$#', $x) !== 1) {
return;
}
assertType('non-falsy-string', $x);
}

function pregMatchNotEqual(string $x): void {
if (preg_match('#ExtensionInterface$#', $x) != 1) {
return;
}
assertType('non-falsy-string', $x);
}

function pregMatchWithNonConstantPattern(string $pattern, string $x): void {
if (preg_match($pattern, $x)) {
assertType('string', $x);
}
}

function pregMatchSubjectSharesVarWithMatches(): void {
$matches = ['', '', 'foo'];
if (preg_match('/^(a|b|c)$/', $matches[2], $matches)) {
assertType("array{non-falsy-string, 'a'|'b'|'c'}", $matches);
}
}

function pregMatchNullableSubject(?string $x): void {
// a null subject is coerced to '' which cannot match a non-empty pattern, so null is removed
if (preg_match('/^(a|b|c)$/', $x)) {
assertType('string|null', $x); // could be non-falsy-string
} else {
assertType('string|null', $x);
}
}

function pregMatchIntStringSubject(int|string $x): void {
// an int subject can be coerced to a matching string, so narrowing it away would be unsound
if (preg_match('/^(a|b|c)$/', $x)) {
assertType('int|string', $x);
}
}
Loading