Skip to content
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

TypeSpecifier - understand \Composer\Pcre\Preg::match() == 1 same way as === 1 #3178

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions build/composer-dependency-analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
)
->ignoreErrorsOnPackage('phpunit/phpunit', [ErrorType::DEV_DEPENDENCY_IN_PROD]) // prepared test tooling
->ignoreErrorsOnPackage('jetbrains/phpstorm-stubs', [ErrorType::PROD_DEPENDENCY_ONLY_IN_DEV]) // there is no direct usage, but we need newer version then required by ondrejmirtes/BetterReflection
->ignoreErrorsOnPackage('composer/pcre', [ErrorType::SHADOW_DEPENDENCY]) // reverse dependency on composer/pcre to support https://github.com/composer/pcre/pull/24
Copy link
Contributor Author

@staabm staabm Jun 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janedbal is this the correct way to handle a optional runtime dependency?

->ignoreErrorsOnPath(__DIR__ . '/../tests', [ErrorType::UNKNOWN_CLASS, ErrorType::UNKNOWN_FUNCTION]) // to be able to test invalid symbols
->ignoreUnknownClasses([
'JetBrains\PhpStorm\Pure', // not present on composer's classmap
Expand Down
30 changes: 30 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Analyser;

use Composer\Pcre\Preg;
use Countable;
use PhpParser\Node;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -1911,6 +1912,18 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
) {
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
}

if (
$context->true()
&& $exprNode instanceof StaticCall
&& $exprNode->class instanceof Name
&& $exprNode->class->toString() === Preg::class
&& $exprNode->name instanceof Node\Identifier
&& $exprNode->name->toLowerString() === 'match'
&& (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes()
) {
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
}
}

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

if (
$context->true()
&& $unwrappedLeftExpr instanceof StaticCall
&& $unwrappedLeftExpr->class instanceof Name
&& $unwrappedLeftExpr->class->toString() === Preg::class
&& $unwrappedLeftExpr->name instanceof Node\Identifier
&& $unwrappedLeftExpr->name->toLowerString() === 'match'
&& (new ConstantIntegerType(1))->isSuperTypeOf($rightType)->yes()
) {
return $this->specifyTypesInCondition(
$scope,
$leftExpr,
$context,
$rootExpr,
);
}

if (
$context->true()
&& $unwrappedLeftExpr instanceof FuncCall
Expand Down
Loading