Skip to content

Commit

Permalink
[CodeQuality][EarlyReturn][TypeDeclaration] Handle ChangeOrIfReturnTo…
Browse files Browse the repository at this point in the history
…EarlyReturnRector + ChangeAndIfToEarlyReturnRector + AddArrayReturnDocTypeRector + NarrowUnionTypeDocRector (#795)
  • Loading branch information
samsonasik committed Aug 31, 2021
1 parent 3e5ecbc commit 9247ba9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ public function refactor(Node $node): ?Node
}
}

if ($phpDocInfo->hasChanged()) {
$node->setAttribute(AttributeKey::HAS_PHP_DOC_INFO_JUST_CHANGED, true);
return $node;
if (! $phpDocInfo->hasChanged()) {
return null;
}

$node->setAttribute(AttributeKey::HAS_PHP_DOC_INFO_JUST_CHANGED, true);

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

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

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

return;
}
}

?>
-----
<?php

declare(strict_types=1);

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

class AndNextOrReturnVoid
{
/**
* @return null|void
*/
public function run($a, $b, $c, $d)
{
if ($a && $b) {
return null;
}
if ($c) {
return null;
}
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\IssueEarlyReturnAndOrNarrow;

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

final class IssueEarlyReturnAndOrNarrowTest 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,17 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector;
use Rector\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector;
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(AddArrayReturnDocTypeRector::class);
$services->set(NarrowUnionTypeDocRector::class);
};

0 comments on commit 9247ba9

Please sign in to comment.