Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for already migrated logicalAnd #3929

Merged
merged 1 commit into from
Dec 1, 2023
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
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));
}
}
}

?>