betterStandardPrinter = $betterStandardPrinter;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove if/else if they have same content', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
if (true) {
return 1;
} else {
return 1;
}
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
return 1;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array>
*/
public function getNodeTypes() : array
{
return [If_::class];
}
/**
* @param If_ $node
* @return Stmt[]|null
*/
public function refactor(Node $node) : ?array
{
if (!$node->else instanceof Else_) {
return null;
}
if (!$this->isIfWithConstantReturns($node)) {
return null;
}
return $node->stmts;
}
private function isIfWithConstantReturns(If_ $if) : bool
{
$possibleContents = [];
$possibleContents[] = $this->betterStandardPrinter->print($if->stmts);
foreach ($if->elseifs as $elseif) {
$possibleContents[] = $this->betterStandardPrinter->print($elseif->stmts);
}
$else = $if->else;
if (!$else instanceof Else_) {
throw new ShouldNotHappenException();
}
$possibleContents[] = $this->betterStandardPrinter->print($else->stmts);
$uniqueContents = \array_unique($possibleContents);
// only one content for all
return \count($uniqueContents) === 1;
}
}