Skip to content

Commit

Permalink
[EarlyReturn] Allow usage ChangeOrIfReturnToEarlyReturnRector and Rem…
Browse files Browse the repository at this point in the history
…oveAlwaysElseRector without elseif (#655)

* [EarlyReturn] Allow usage ChangeOrIfReturnToEarlyReturnRector and RemoveAlwaysElseRector without elseif

* phpstan
  • Loading branch information
samsonasik committed Aug 12, 2021
1 parent 99301b3 commit de5c4c7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
23 changes: 14 additions & 9 deletions rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,7 @@ public function refactor(Node $node): ?Node
return null;
}

// to avoid repetitive If_ creation when used along with ChangeOrIfReturnToEarlyReturnRector
// @see https://github.com/rectorphp/rector-src/pull/651
if ($node->cond instanceof BooleanOr) {
return null;
}

// to avoid repetitive flipped elseif above return when used along with ChangeAndIfReturnToEarlyReturnRector
// @see https://github.com/rectorphp/rector-src/pull/654
if ($node->cond instanceof BooleanAnd && count($node->elseifs) > 1) {
if ($this->shouldSkip($node)) {
return null;
}

Expand Down Expand Up @@ -127,6 +119,19 @@ public function refactor(Node $node): ?Node
return null;
}

private function shouldSkip(If_ $if): bool
{
// to avoid repetitive If_ creation when used along with ChangeOrIfReturnToEarlyReturnRector
// @see https://github.com/rectorphp/rector-src/pull/651
if ($if->cond instanceof BooleanOr && $if->elseifs !== []) {
return true;
}

// to avoid repetitive flipped elseif above return when used along with ChangeAndIfReturnToEarlyReturnRector
// @see https://github.com/rectorphp/rector-src/pull/654
return $if->cond instanceof BooleanAnd && count($if->elseifs) > 1;
}

/**
* @return ElseIf_[]
*/
Expand Down
9 changes: 4 additions & 5 deletions rules/Php81/NodeFactory/EnumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Php81\NodeFactory;

use Nette\Utils\Strings;
use PhpParser\BuilderFactory;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
Expand Down Expand Up @@ -44,8 +43,8 @@ public function createFromSpatieClass(Class_ $class): Enum_
$enum = new Enum_($shortClassName);

// constant to cases
$classDocInfo = $this->phpDocInfoFactory->createFromNode($class);
$docBlockMethods = $classDocInfo?->getTagsByName('@method');
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($class);
$docBlockMethods = $phpDocInfo?->getTagsByName('@method');
if ($docBlockMethods !== null) {
foreach ($docBlockMethods as $docBlockMethod) {
$enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod);
Expand All @@ -67,12 +66,12 @@ private function createEnumCaseFromConst(ClassConst $classConst): EnumCase
return $enumCase;
}

private function createEnumCaseFromDocComment(PhpDocTagNode $docTagNode): EnumCase
private function createEnumCaseFromDocComment(PhpDocTagNode $phpDocTagNode): EnumCase
{
/**
* @var MethodTagValueNode $nodeValue
*/
$nodeValue = $docTagNode->value;
$nodeValue = $phpDocTagNode->value;
return new EnumCase($nodeValue->methodName, $this->builderFactory->val($nodeValue->methodName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\IssueReturnBeforeElseIf\Fixture;

class ComplexIfCondOrWithoutElseIf
{
public function run($a, $b, $c)
{
if (($a && false === $b) || ! $c) {
return 'a';
} else {
return 'b';
}
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\IssueReturnBeforeElseIf\Fixture;

class ComplexIfCondOrWithoutElseIf
{
public function run($a, $b, $c)
{
if ($a && false === $b) {
return 'a';
}
if (! $c) {
return 'a';
}
return 'b';
}
}

?>

0 comments on commit de5c4c7

Please sign in to comment.