Skip to content

Commit

Permalink
[CodeQuality][EarlyReturn] Handle ChangeOrIfReturnToEarlyReturnRector…
Browse files Browse the repository at this point in the history
… + ChangeAndIfToEarlyReturnRector + DateTimeToDateTimeInterfaceRector (#794)
  • Loading branch information
samsonasik committed Aug 31, 2021
1 parent 9fee4dd commit 3e5ecbc
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,8 @@ parameters:
- rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php

- '#^Cognitive complexity for "Rector\\CodingStyle\\Naming\\NameRenamer\:\:renameNameNode\(\)" is 13, keep it under 9$#'

-
message: '#The string value "" is repeated 6 times\. Refactor to enum to avoid typos and make clear allowed value#'
paths:
- packages/BetterPhpDocParser/PhpDocManipulator/PhpDocTagRemover.php #45
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public function refactor(Node $node): ?Node
{
if ($node instanceof ClassMethod) {
$this->refactorClassMethod($node);
return $node;

// @see https://github.com/rectorphp/rector-src/pull/794
// avoid duplicated ifs and returns when combined with ChangeOrIfReturnToEarlyReturnRector and ChangeAndIfToEarlyReturnRector
return null;
}

return $this->refactorProperty($node);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

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

class AndNextOr
{
public function run($a, $b, $c, $d)
{
if ($a && $b || $c) {
return null;
}

$a = 1;
$b = 2;

if ($b > $a) {
return null;
}

$d = 1;

return;
}
}

?>
-----
<?php

declare(strict_types=1);

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

class AndNextOr
{
public function run($a, $b, $c, $d)
{
if ($a && $b) {
return null;
}
if ($c) {
return null;
}
$a = 1;
$b = 2;

if ($b > $a) {
return null;
}

$d = 1;

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\IssueEarlyReturnAndOrDatetime;

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

final class IssueEarlyReturnAndOrDatetimeTest 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,15 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector;
use Rector\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

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

0 comments on commit 3e5ecbc

Please sign in to comment.