Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/sets/typed-collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Rector\Doctrine\TypedCollections\Rector\FuncCall\ArrayMapOnCollectionToArrayRector;
use Rector\Doctrine\TypedCollections\Rector\FuncCall\ArrayMergeOnCollectionToArrayRector;
use Rector\Doctrine\TypedCollections\Rector\FuncCall\InArrayOnCollectionToContainsCallRector;
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector;
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector;
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIsArrayOnCollectionRector;
use Rector\Doctrine\TypedCollections\Rector\MethodCall\AssertNullOnCollectionToAssertEmptyRector;
Expand All @@ -45,6 +46,7 @@
RemoveCoalesceAssignOnCollectionRector::class,
RemoveIfInstanceofCollectionRector::class,
RemoveIsArrayOnCollectionRector::class,
RemoveIfCollectionIdenticalToNullRector::class,

// collection method calls
ArrayDimFetchAssignToAddCollectionCallRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class SkipAnd
{
public Collection $items;

public function someMethod()
{
if ($this->items === null && $this->items->isEmpty()) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class SomeIf
{
public Collection $items;

public function someMethod()
{
if ($this->items === null || $this->items->isEmpty()) {
return true;
}

return false;
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class SomeIf
{
public Collection $items;

public function someMethod()
{
if ($this->items->isEmpty()) {
return true;
}

return false;
}
}

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

declare(strict_types=1);

namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveIfCollectionIdenticalToNullRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::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,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveIfCollectionIdenticalToNullRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\TypedCollections\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Stmt\If_;
use Rector\Doctrine\TypedCollections\TypeAnalyzer\CollectionTypeDetector;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfCollectionIdenticalToNullRector\RemoveIfCollectionIdenticalToNullRectorTest
*/
final class RemoveIfCollectionIdenticalToNullRector extends AbstractRector
{
public function __construct(
private readonly CollectionTypeDetector $collectionTypeDetector,
) {

}

public function getNodeTypes(): array
{
return [If_::class];
}

/**
* @param If_ $node
*/
public function refactor(Node $node): ?If_
{
if (! $node->cond instanceof BooleanOr) {
return null;
}

$leftCondition = $node->cond->left;
if (! $leftCondition instanceof Identical) {
return null;
}

if (! $leftCondition->right instanceof ConstFetch || ! $this->isName(
$leftCondition->right->name,
'null'
)) {
return null;
}

if (! $this->collectionTypeDetector->isCollectionType($leftCondition->left)) {
return null;
}

$rightCondition = $node->cond->right;

$node->cond = $rightCondition;

return $node;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove collection identical to null from if || condition', [
new CodeSample(
<<<'CODE_SAMPLE'
use Doctrine\Common\Collections\Collection;

final class SomeClass
{
private Collection $collection;

public function someMethod()
{
if ($this->collection === null || $this->collection->isEmpty()) {
return true;
}

return false;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Doctrine\Common\Collections\Collection;

final class SomeClass
{
private Collection $collection;

public function someMethod()
{
if ($this->collection->isEmpty()) {
return true;
}

return false;
}
}
CODE_SAMPLE
)]);
}
}