Skip to content

Commit 77cad7b

Browse files
ondrejmirtesherndlm
authored andcommitted
Restructure and improve the hack
1 parent e302961 commit 77cad7b

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

+30-27
Original file line numberDiff line numberDiff line change
@@ -177,34 +177,12 @@ public function findSpecifiedType(
177177
$sureTypes = $specifiedTypes->getSureTypes();
178178
$sureNotTypes = $specifiedTypes->getSureNotTypes();
179179

180-
$isSpecified = static function (Expr $expr) use ($scope, $node): bool {
181-
if ($expr === $node) {
182-
return true;
183-
}
184-
185-
if ($expr instanceof Expr\Variable && is_string($expr->name) && !$scope->hasVariableType($expr->name)->yes()) {
186-
return true;
187-
}
188-
189-
return (
190-
$node instanceof FuncCall
191-
|| $node instanceof MethodCall
192-
|| $node instanceof Expr\StaticCall
193-
) && $scope->isSpecified($expr);
194-
};
195-
196180
$rootExpr = $specifiedTypes->getRootExpr();
197181
if ($rootExpr !== null) {
198-
if ($isSpecified($rootExpr)) {
182+
if (self::isSpecified($scope, $node, $rootExpr)) {
199183
return null;
200184
}
201185

202-
if ($rootExpr instanceof Expr\BinaryOp) {
203-
if ($isSpecified($rootExpr->left) || $isSpecified($rootExpr->right)) {
204-
return null;
205-
}
206-
}
207-
208186
$rootExprType = $scope->getType($rootExpr);
209187
if ($rootExprType instanceof ConstantBooleanType) {
210188
return $rootExprType->getValue();
@@ -215,7 +193,7 @@ public function findSpecifiedType(
215193

216194
if (count($sureTypes) === 1 && count($sureNotTypes) === 0) {
217195
$sureType = reset($sureTypes);
218-
if ($isSpecified($sureType[0])) {
196+
if (self::isSpecified($scope, $node, $sureType[0])) {
219197
return null;
220198
}
221199

@@ -238,7 +216,7 @@ public function findSpecifiedType(
238216
return null;
239217
} elseif (count($sureNotTypes) === 1 && count($sureTypes) === 0) {
240218
$sureNotType = reset($sureNotTypes);
241-
if ($isSpecified($sureNotType[0])) {
219+
if (self::isSpecified($scope, $node, $sureNotType[0])) {
242220
return null;
243221
}
244222

@@ -263,7 +241,7 @@ public function findSpecifiedType(
263241

264242
if (count($sureTypes) > 0) {
265243
foreach ($sureTypes as $sureType) {
266-
if ($isSpecified($sureType[0])) {
244+
if (self::isSpecified($scope, $node, $sureType[0])) {
267245
return null;
268246
}
269247
}
@@ -277,7 +255,7 @@ public function findSpecifiedType(
277255

278256
if (count($sureNotTypes) > 0) {
279257
foreach ($sureNotTypes as $sureNotType) {
280-
if ($isSpecified($sureNotType[0])) {
258+
if (self::isSpecified($scope, $node, $sureNotType[0])) {
281259
return null;
282260
}
283261
}
@@ -292,6 +270,31 @@ public function findSpecifiedType(
292270
return null;
293271
}
294272

273+
private static function isSpecified(Scope $scope, Expr $node, Expr $expr): bool
274+
{
275+
if ($expr === $node) {
276+
return true;
277+
}
278+
279+
if ($expr instanceof Expr\Variable && is_string($expr->name) && !$scope->hasVariableType($expr->name)->yes()) {
280+
return true;
281+
}
282+
283+
if ($expr instanceof Expr\BooleanNot) {
284+
return self::isSpecified($scope, $node, $expr->expr);
285+
}
286+
287+
if ($expr instanceof Expr\BinaryOp) {
288+
return self::isSpecified($scope, $node, $expr->left) || self::isSpecified($scope, $node, $expr->right);
289+
}
290+
291+
return (
292+
$node instanceof FuncCall
293+
|| $node instanceof MethodCall
294+
|| $node instanceof Expr\StaticCall
295+
) && $scope->isSpecified($expr);
296+
}
297+
295298
/**
296299
* @param Node\Arg[] $args
297300
*/

0 commit comments

Comments
 (0)