Skip to content

Commit

Permalink
[CodeQuality][EarlyReturn] Handle ExplicitBoolCompareRector + ChangeO…
Browse files Browse the repository at this point in the history
…rIfReturnToEarlyRector (#836)

* [CodeQuality][EarlyReturn] Failing Fixture ExplicitBOolCompare + ChangeOrIfReturnToEarly Rector

* Fixed 🎉
  • Loading branch information
samsonasik committed Sep 6, 2021
1 parent f2bec56 commit df9246c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
14 changes: 14 additions & 0 deletions rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
use Rector\NodeTypeResolver\TypeAnalyzer\StringTypeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -119,11 +122,22 @@ public function refactor(Node $node): ?Node
return null;
}

$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
// avoid duplicated ifs when combined with ChangeOrIfReturnToEarlyReturnRector
if ($this->shouldSkip($conditionStaticType, $newConditionNode, $nextNode)) {
return null;
}

$node->cond = $newConditionNode;

return $node;
}

private function shouldSkip(Type $conditionStaticType, BinaryOp $binaryOp, ?Node $nextNode): bool
{
return $conditionStaticType instanceof StringType && $binaryOp instanceof BooleanOr && ! $nextNode instanceof Node;
}

private function resolveNewConditionNode(Expr $expr, bool $isNegated): ?BinaryOp
{
if ($expr instanceof FuncCall && $this->nodeNameResolver->isName($expr, 'count')) {
Expand Down
40 changes: 40 additions & 0 deletions tests/Issues/IssueOrIfExplicitBoolCompare/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

class Fixture
{
/**
* @param string $a
*/
public function run($a = '"', $b)
{
if (! $a || ! $b) {
return;
}
}
}

?>
-----
<?php

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

class Fixture
{
/**
* @param string $a
*/
public function run($a = '"', $b)
{
if (! $a) {
return;
}
if (! $b) {
return;
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\IssueOrIfExplicitBoolCompare;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class IssueOrIfExplicitBoolCompareTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ExplicitBoolCompareRector::class);
$services->set(ChangeOrIfReturnToEarlyReturnRector::class);
};

0 comments on commit df9246c

Please sign in to comment.