Skip to content

Commit 8b7c801

Browse files
committed
TypeSpecifier - understand \Composer\Pcre\Preg::match() == 1 same way as === 1
1 parent 794b203 commit 8b7c801

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: src/Analyser/TypeSpecifier.php

+30
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Analyser;
44

5+
use Composer\Pcre\Preg;
56
use Countable;
67
use PhpParser\Node;
78
use PhpParser\Node\Expr;
@@ -1911,6 +1912,18 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
19111912
) {
19121913
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
19131914
}
1915+
1916+
if (
1917+
$context->true()
1918+
&& $exprNode instanceof StaticCall
1919+
&& $exprNode->class instanceof Name
1920+
&& $exprNode->class->toString() === Preg::class
1921+
&& $exprNode->name instanceof Node\Identifier
1922+
&& $exprNode->name->toLowerString() === 'match'
1923+
&& (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes()
1924+
) {
1925+
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
1926+
}
19141927
}
19151928

19161929
$leftType = $scope->getType($expr->left);
@@ -2015,6 +2028,23 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
20152028
);
20162029
}
20172030

2031+
if (
2032+
$context->true()
2033+
&& $unwrappedLeftExpr instanceof StaticCall
2034+
&& $unwrappedLeftExpr->class instanceof Name
2035+
&& $unwrappedLeftExpr->class->toString() === Preg::class
2036+
&& $unwrappedLeftExpr->name instanceof Node\Identifier
2037+
&& $unwrappedLeftExpr->name->toLowerString() === 'match'
2038+
&& (new ConstantIntegerType(1))->isSuperTypeOf($rightType)->yes()
2039+
) {
2040+
return $this->specifyTypesInCondition(
2041+
$scope,
2042+
$leftExpr,
2043+
$context,
2044+
$rootExpr,
2045+
);
2046+
}
2047+
20182048
if (
20192049
$context->true()
20202050
&& $unwrappedLeftExpr instanceof FuncCall

0 commit comments

Comments
 (0)