Skip to content

Commit cb69e3e

Browse files
Rework
1 parent 8f559bc commit cb69e3e

7 files changed

Lines changed: 22 additions & 10 deletions

src/Rules/Comparison/ConstantConditionRuleHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function shouldSkip(Scope $scope, Expr $expr): bool
5757
|| $expr instanceof Expr\StaticCall
5858
) && !$expr->isFirstClassCallable()
5959
) {
60-
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $expr);
60+
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $expr, true);
6161
if ($isAlways !== null) {
6262
return true;
6363
}

src/Rules/Comparison/ImpossibleCheckTypeFunctionCallRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function processNode(Node $node, Scope $scope): array
4343
}
4444

4545
$functionName = (string) $node->name;
46-
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node);
46+
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node, true);
4747
if ($isAlways === null) {
4848
return [];
4949
}
@@ -53,7 +53,7 @@ public function processNode(Node $node, Scope $scope): array
5353
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
5454
}
5555

56-
$isAlways = $this->impossibleCheckTypeHelper->doNotTreatPhpDocTypesAsCertain()->findSpecifiedType($scope, $node);
56+
$isAlways = $this->impossibleCheckTypeHelper->doNotTreatPhpDocTypesAsCertain()->findSpecifiedType($scope, $node, true);
5757
if ($isAlways !== null) {
5858
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
5959
}

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function __construct(
6161
public function findSpecifiedType(
6262
Scope $scope,
6363
Expr $node,
64+
bool $ignoreTraitContext,
6465
): ?bool
6566
{
6667
if ($node instanceof FuncCall) {
@@ -199,7 +200,10 @@ public function findSpecifiedType(
199200
} elseif ($functionName === 'method_exists' && $argsCount >= 2) {
200201
$objectArg = $args[0]->value;
201202

202-
if ($this->isExpressionDependentOnTraitContext($scope, $objectArg)) {
203+
if (
204+
$ignoreTraitContext
205+
&& $this->isExpressionDependentOnTraitContext($scope, $objectArg)
206+
) {
203207
$traitReflection = $scope->getTraitReflection();
204208
if ($traitReflection === null) {
205209
return null;
@@ -328,7 +332,10 @@ public function findSpecifiedType(
328332
continue;
329333
}
330334

331-
if ($this->isExpressionDependentOnTraitContext($scope, $sureType[0])) {
335+
if (
336+
$ignoreTraitContext
337+
&& $this->isExpressionDependentOnTraitContext($scope, $sureType[0])
338+
) {
332339
$results[] = TrinaryLogic::createMaybe();
333340
continue;
334341
}
@@ -359,7 +366,10 @@ public function findSpecifiedType(
359366
continue;
360367
}
361368

362-
if ($this->isExpressionDependentOnTraitContext($scope, $sureNotType[0])) {
369+
if (
370+
$ignoreTraitContext
371+
&& $this->isExpressionDependentOnTraitContext($scope, $sureNotType[0])
372+
) {
363373
$results[] = TrinaryLogic::createMaybe();
364374
continue;
365375
}

src/Rules/Comparison/ImpossibleCheckTypeMethodCallRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
4545
return [];
4646
}
4747

48-
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node);
48+
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node, true);
4949
if ($isAlways === null) {
5050
return [];
5151
}
@@ -55,7 +55,7 @@ public function processNode(Node $node, Scope $scope): array
5555
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
5656
}
5757

58-
$isAlways = $this->impossibleCheckTypeHelper->doNotTreatPhpDocTypesAsCertain()->findSpecifiedType($scope, $node);
58+
$isAlways = $this->impossibleCheckTypeHelper->doNotTreatPhpDocTypesAsCertain()->findSpecifiedType($scope, $node, true);
5959
if ($isAlways !== null) {
6060
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
6161
}

src/Rules/Comparison/ImpossibleCheckTypeStaticMethodCallRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
4545
return [];
4646
}
4747

48-
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node);
48+
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node, true);
4949
if ($isAlways === null) {
5050
return [];
5151
}
@@ -55,7 +55,7 @@ public function processNode(Node $node, Scope $scope): array
5555
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
5656
}
5757

58-
$isAlways = $this->impossibleCheckTypeHelper->doNotTreatPhpDocTypesAsCertain()->findSpecifiedType($scope, $node);
58+
$isAlways = $this->impossibleCheckTypeHelper->doNotTreatPhpDocTypesAsCertain()->findSpecifiedType($scope, $node, true);
5959
if ($isAlways !== null) {
6060
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
6161
}

src/Type/Php/TypeSpecifyingFunctionsDynamicReturnTypeExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function getTypeFromFunctionCall(
6666
$isAlways = $this->getHelper()->findSpecifiedType(
6767
$scope,
6868
$functionCall,
69+
false,
6970
);
7071
if ($isAlways === null) {
7172
return null;

tests/PHPStan/Rules/Comparison/data/bug-12798.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static function colors(): array {
1616
$color = is_subclass_of($case, Colorable::class) ? $case->color() : 'gray';
1717

1818
$colors[$key] = $color;
19+
$colors[$color] = $color;
1920
return $colors;
2021
}, []);
2122
}

0 commit comments

Comments
 (0)