Skip to content

Commit

Permalink
Restructure and improve the hack
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes authored and herndlm committed Apr 27, 2022
1 parent e36ffc7 commit 2b10c8b
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Expand Up @@ -177,34 +177,12 @@ public function findSpecifiedType(
$sureTypes = $specifiedTypes->getSureTypes();
$sureNotTypes = $specifiedTypes->getSureNotTypes();

$isSpecified = static function (Expr $expr) use ($scope, $node): bool {
if ($expr === $node) {
return true;
}

if ($expr instanceof Expr\Variable && is_string($expr->name) && !$scope->hasVariableType($expr->name)->yes()) {
return true;
}

return (
$node instanceof FuncCall
|| $node instanceof MethodCall
|| $node instanceof Expr\StaticCall
) && $scope->isSpecified($expr);
};

$rootExpr = $specifiedTypes->getRootExpr();
if ($rootExpr !== null) {
if ($isSpecified($rootExpr)) {
if (self::isSpecified($scope, $node, $rootExpr)) {
return null;
}

if ($rootExpr instanceof Expr\BinaryOp) {
if ($isSpecified($rootExpr->left) || $isSpecified($rootExpr->right)) {
return null;
}
}

$rootExprType = $scope->getType($rootExpr);
if ($rootExprType instanceof ConstantBooleanType) {
return $rootExprType->getValue();
Expand All @@ -215,7 +193,7 @@ public function findSpecifiedType(

if (count($sureTypes) === 1 && count($sureNotTypes) === 0) {
$sureType = reset($sureTypes);
if ($isSpecified($sureType[0])) {
if (self::isSpecified($scope, $node, $sureType[0])) {
return null;
}

Expand All @@ -238,7 +216,7 @@ public function findSpecifiedType(
return null;
} elseif (count($sureNotTypes) === 1 && count($sureTypes) === 0) {
$sureNotType = reset($sureNotTypes);
if ($isSpecified($sureNotType[0])) {
if (self::isSpecified($scope, $node, $sureNotType[0])) {
return null;
}

Expand All @@ -263,7 +241,7 @@ public function findSpecifiedType(

if (count($sureTypes) > 0) {
foreach ($sureTypes as $sureType) {
if ($isSpecified($sureType[0])) {
if (self::isSpecified($scope, $node, $sureType[0])) {
return null;
}
}
Expand All @@ -277,7 +255,7 @@ public function findSpecifiedType(

if (count($sureNotTypes) > 0) {
foreach ($sureNotTypes as $sureNotType) {
if ($isSpecified($sureNotType[0])) {
if (self::isSpecified($scope, $node, $sureNotType[0])) {
return null;
}
}
Expand All @@ -292,6 +270,31 @@ public function findSpecifiedType(
return null;
}

private static function isSpecified(Scope $scope, Expr $node, Expr $expr): bool
{
if ($expr === $node) {
return true;
}

if ($expr instanceof Expr\Variable && is_string($expr->name) && !$scope->hasVariableType($expr->name)->yes()) {
return true;
}

if ($expr instanceof Expr\BooleanNot) {
return self::isSpecified($scope, $node, $expr->expr);
}

if ($expr instanceof Expr\BinaryOp) {
return self::isSpecified($scope, $node, $expr->left) || self::isSpecified($scope, $node, $expr->right);
}

return (
$node instanceof FuncCall
|| $node instanceof MethodCall
|| $node instanceof Expr\StaticCall
) && $scope->isSpecified($expr);
}

/**
* @param Node\Arg[] $args
*/
Expand Down

0 comments on commit 2b10c8b

Please sign in to comment.