exactCompareFactory = $exactCompareFactory; } public function getRuleDefinition() : RuleDefinition { $errorMessage = \sprintf('Fixer for PHPStan reports by strict type rule - "%s"', 'PHPStan\\Rules\\BooleansInConditions\\BooleanInIfConditionRule'); return new RuleDefinition($errorMessage, [new ConfiguredCodeSample(<<<'CODE_SAMPLE' final class NegatedString { public function run(string $name) { if ($name) { return 'name'; } return 'no name'; } } CODE_SAMPLE , <<<'CODE_SAMPLE' final class NegatedString { public function run(string $name) { if ($name !== '') { return 'name'; } return 'no name'; } } CODE_SAMPLE , [\Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector::TREAT_AS_NON_EMPTY => \false])]); } /** * @return array> */ public function getNodeTypes() : array { return [If_::class]; } /** * @param If_ $node */ public function refactorWithScope(Node $node, Scope $scope) : ?If_ { $hasChanged = \false; // 1. if $ifCondExprType = $scope->getNativeType($node->cond); $notIdentical = $this->exactCompareFactory->createNotIdenticalFalsyCompare($ifCondExprType, $node->cond, $this->treatAsNonEmpty); if ($notIdentical !== null) { $node->cond = $notIdentical; $hasChanged = \true; } // 2. elseifs foreach ($node->elseifs as $elseif) { $elseifCondExprType = $scope->getNativeType($elseif->cond); $notIdentical = $this->exactCompareFactory->createNotIdenticalFalsyCompare($elseifCondExprType, $elseif->cond, $this->treatAsNonEmpty); if (!$notIdentical instanceof Expr) { continue; } $elseif->cond = $notIdentical; $hasChanged = \true; } if ($hasChanged) { return $node; } return null; } }