ifManipulator = $ifManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Changes if || to early return', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function canDrive(Car $newCar)
{
foreach ($cars as $car) {
if ($car->hasWheels() || $car->hasFuel()) {
continue;
}
$car->setWheel($newCar->wheel);
$car->setFuel($newCar->fuel);
}
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function canDrive(Car $newCar)
{
foreach ($cars as $car) {
if ($car->hasWheels()) {
continue;
}
if ($car->hasFuel()) {
continue;
}
$car->setWheel($newCar->wheel);
$car->setFuel($newCar->fuel);
}
}
}
CODE_SAMPLE
)]);
}
/**
* @return array>
*/
public function getNodeTypes() : array
{
return [If_::class];
}
/**
* @param If_ $node
* @return null|If_[]
*/
public function refactor(Node $node) : ?array
{
if (!$this->ifManipulator->isIfWithOnly($node, Continue_::class)) {
return null;
}
if (!$node->cond instanceof BooleanOr) {
return null;
}
return $this->processMultiIfContinue($node);
}
/**
* @return null|If_[]
*/
private function processMultiIfContinue(If_ $if) : ?array
{
$node = clone $if;
/** @var Continue_ $continue */
$continue = $if->stmts[0];
$ifs = $this->createMultipleIfs($if->cond, $continue, []);
// ensure ifs not removed by other rules
if ($ifs === []) {
return null;
}
$this->mirrorComments($ifs[0], $node);
return $ifs;
}
/**
* @param If_[] $ifs
* @return If_[]
*/
private function createMultipleIfs(Expr $expr, Continue_ $continue, array $ifs) : array
{
while ($expr instanceof BooleanOr) {
$ifs = \array_merge($ifs, $this->collectLeftBooleanOrToIfs($expr, $continue, $ifs));
$ifs[] = new If_($expr->right, ['stmts' => [$continue]]);
$expr = $expr->right;
}
$lastContinueIf = new If_($expr, ['stmts' => [$continue]]);
// the + is on purpose here, to keep only single continue as last
return $ifs + [$lastContinueIf];
}
/**
* @param If_[] $ifs
* @return If_[]
*/
private function collectLeftBooleanOrToIfs(BooleanOr $booleanOr, Continue_ $continue, array $ifs) : array
{
$left = $booleanOr->left;
if (!$left instanceof BooleanOr) {
$if = new If_($left, ['stmts' => [$continue]]);
return [$if];
}
return $this->createMultipleIfs($left, $continue, $ifs);
}
}