Skip to content

Commit

Permalink
Add test for already migrated logicalAnd
Browse files Browse the repository at this point in the history
  • Loading branch information
nhovratov authored and simonschaufi committed Dec 1, 2023
1 parent 988df0f commit e60e266
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
Expand Down Expand Up @@ -75,6 +76,21 @@ public function refactor(Node $node): ?Node
return null;
}

if ($args[0]->unpack === true) {
// In this case, the code looks like this: $query->logicalAnd(...$constraints);
$parentIfStatement = $this->betterNodeFinder->findParentType($node, If_::class);
// the if should not contain a count, otherwise it is probably migrated already
if ($parentIfStatement instanceof If_ && $parentIfStatement->cond instanceof Identical) {
$comparison = $parentIfStatement->cond;
if ($comparison->left instanceof FuncCall && $this->isName($comparison->left, 'count')) {
return null;
}
if ($comparison->right instanceof FuncCall && $this->isName($comparison->right, 'count')) {
return null;
}
}
}

$firstArgument = $args[0]->value;

if ($firstArgument instanceof Variable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\typo3\HardenMethodSignatureOfLogicalAndAndLogicalOrRector\Fixture;

use TYPO3\CMS\Extbase\Persistence\Repository;

class ProductRepositoryLogicalAndWithConstraintsAsArguments extends Repository
{
public function findAllForList()
{
$constraints = [];
$query = $this->createQuery();
if (count($constraints) === 1) {
$query->matching(reset($constraints));
} elseif (count($constraints) >= 2) {
$query->matching($query->logicalAnd(...$constraints));
}
}

public function findAllForListWithDifferentQueryVariableName()
{
$constraints = [];
$q = $this->createQuery();
if (count($constraints) === 1) {
$q->matching(reset($constraints));
} elseif (count($constraints) >= 2) {
$q->matching($q->logicalAnd(...$constraints));
}
}

public function findAllForListWithLogicalOr()
{
$constraints = [];
$q = $this->createQuery();
if (count($constraints) === 1) {
$q->matching(reset($constraints));
} elseif (count($constraints) >= 2) {
$q->matching($q->logicalOr(...$constraints));
}
}
}

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\typo3\HardenMethodSignatureOfLogicalAndAndLogicalOrRector\Fixture;

use TYPO3\CMS\Extbase\Persistence\Repository;

class ProductRepositoryLogicalAndWithConstraintsAsArguments extends Repository
{
public function findAllForList()
{
$constraints = [];
$query = $this->createQuery();
if (count($constraints) === 1) {
$query->matching(reset($constraints));
} elseif (count($constraints) >= 2) {
$query->matching($query->logicalAnd(...$constraints));
}
}

public function findAllForListWithDifferentQueryVariableName()
{
$constraints = [];
$q = $this->createQuery();
if (count($constraints) === 1) {
$q->matching(reset($constraints));
} elseif (count($constraints) >= 2) {
$q->matching($q->logicalAnd(...$constraints));
}
}

public function findAllForListWithLogicalOr()
{
$constraints = [];
$q = $this->createQuery();
if (count($constraints) === 1) {
$q->matching(reset($constraints));
} elseif (count($constraints) >= 2) {
$q->matching($q->logicalOr(...$constraints));
}
}
}

?>

0 comments on commit e60e266

Please sign in to comment.