ifManipulator = $ifManipulator; $this->stmtsManipulator = $stmtsManipulator; } public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Change if/else value to early return', [ new CodeSample( <<<'CODE_SAMPLE' class SomeClass { public function run() { if ($this->hasDocBlock($tokens, $index)) { $docToken = $tokens[$this->getDocBlockIndex($tokens, $index)]; } else { $docToken = null; } return $docToken; } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { if ($this->hasDocBlock($tokens, $index)) { return $tokens[$this->getDocBlockIndex($tokens, $index)]; } return null; } } CODE_SAMPLE ), ]); } /** * @return array> */ public function getNodeTypes(): array { return [If_::class]; } /** * @param If_ $node */ public function refactor(Node $node): ?Node { $nextNode = $node->getAttribute(AttributeKey::NEXT_NODE); if (! $nextNode instanceof Return_) { return null; } if ($nextNode->expr === null) { return null; } if (! $this->ifManipulator->isIfAndElseWithSameVariableAssignAsLastStmts($node, $nextNode->expr)) { return null; } $lastIfStmtKey = array_key_last($node->stmts); /** @var Assign $assign */ $assign = $this->stmtsManipulator->getUnwrappedLastStmt($node->stmts); $return = new Return_($assign->expr); $this->mirrorComments($return, $assign); $node->stmts[$lastIfStmtKey] = $return; $else = $node->else; if (! $else instanceof Else_) { throw new ShouldNotHappenException(); } $elseStmts = $else->stmts; /** @var Assign $assign */ $assign = $this->stmtsManipulator->getUnwrappedLastStmt($elseStmts); $lastElseStmtKey = array_key_last($elseStmts); $return = new Return_($assign->expr); $this->mirrorComments($return, $assign); $elseStmts[$lastElseStmtKey] = $return; $node->else = null; $this->addNodesAfterNode($elseStmts, $node); $this->removeNode($nextNode); return $node; } }