diff --git a/config/v12/typo3-120.php b/config/v12/typo3-120.php index 3e56e5537..ba172b0c7 100644 --- a/config/v12/typo3-120.php +++ b/config/v12/typo3-120.php @@ -11,7 +11,6 @@ use Ssch\TYPO3Rector\Rector\v12\v0\typo3\AddMethodToWidgetInterfaceClassesRector; use Ssch\TYPO3Rector\Rector\v12\v0\typo3\ChangeExtbaseValidatorsRector; use Ssch\TYPO3Rector\Rector\v12\v0\typo3\ContentObjectRegistrationViaServiceConfigurationRector; -use Ssch\TYPO3Rector\Rector\v12\v0\typo3\HardenMethodSignatureOfLogicalAndAndLogicalOrRector; use Ssch\TYPO3Rector\Rector\v12\v0\typo3\HintNecessaryUploadedFileChangesRector; use Ssch\TYPO3Rector\Rector\v12\v0\typo3\ImplementSiteLanguageAwareInterfaceRector; use Ssch\TYPO3Rector\Rector\v12\v0\typo3\MigrateQueryBuilderExecuteRector; @@ -171,5 +170,4 @@ $rectorConfig->rule(RegisterExtbaseTypeConvertersAsServicesRector::class); $rectorConfig->rule(ChangeExtbaseValidatorsRector::class); $rectorConfig->rule(ContentObjectRegistrationViaServiceConfigurationRector::class); - $rectorConfig->rule(HardenMethodSignatureOfLogicalAndAndLogicalOrRector::class); }; diff --git a/src/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector.php b/src/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector.php deleted file mode 100644 index a53ec90c7..000000000 --- a/src/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector.php +++ /dev/null @@ -1,366 +0,0 @@ -nodesToAddCollector = $nodesToAddCollector; - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [MethodCall::class]; - } - - /** - * @param MethodCall $node - */ - public function refactor(Node $node): ?Node - { - if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( - $node, - new ObjectType('TYPO3\CMS\Extbase\Persistence\QueryInterface') - )) { - return null; - } - - if (! $this->isNames($node->name, ['logicalAnd', 'logicalOr'])) { - return null; - } - - $args = $node->getArgs(); - if (! isset($args[0])) { - return null; - } - - if (count($args) > 1) { - return null; - } - - if ($args[0]->unpack) { - // 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) { - // In this case, the code looks like this: $query->logicalAnd($constraints); - return $this->handleArgumentIsVariable($node, $firstArgument); - } - - if (! ($firstArgument instanceof Array_)) { - return null; - } - - // In this case, the code looks like this: $query->logicalAnd([...]) - - // Maybe add "new \PhpParser\Node\Stmt\Nop()" somehow for long lines? - $node->args = $this->nodeFactory->createArgs([...$firstArgument->items]); - - return $node; - } - - /** - * @codeCoverageIgnore - */ - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition( - 'Use multiple parameters instead of array for logicalOr and logicalAnd of Extbase Query class', - [ - new CodeSample( - <<<'CODE_SAMPLE' -use TYPO3\CMS\Extbase\Persistence\Repository; - -class ProductRepositoryLogicalAnd extends Repository -{ - public function findAllForList() - { - $query = $this->createQuery(); - $query->matching($query->logicalAnd([ - $query->equals('propertyName1', 'value1'), - $query->equals('propertyName2', 'value2'), - $query->equals('propertyName3', 'value3'), - ])); - } - public function findAllForSomething() - { - $query = $this->createQuery(); - $constraints[] = $query->lessThan('foo', 1); - $constraints[] = $query->lessThan('bar', 1); - $query->matching($query->logicalAnd($constraints)); - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -use TYPO3\CMS\Extbase\Persistence\Repository; - -class ProductRepositoryLogicalAnd extends Repository -{ - public function findAllForList() - { - $query = $this->createQuery(); - $query->matching($query->logicalAnd( - $query->equals('propertyName1', 'value1'), - $query->equals('propertyName2', 'value2'), - $query->equals('propertyName3', 'value3'), - )); - } - public function findAllForSomething() - { - $query = $this->createQuery(); - $constraints[] = $query->lessThan('foo', 1); - $constraints[] = $query->lessThan('bar', 1); - $query->matching($query->logicalAnd(...$constraints)); - } -} -CODE_SAMPLE - ), - ] - ); - } - - private function handleArgumentIsVariable(MethodCall $node, Variable $firstArgument): ?MethodCall - { - $parentIfStatement = $this->betterNodeFinder->findParentType($node, If_::class); - - $currentStmt = $this->betterNodeFinder->resolveCurrentStatement($node); - if (! $currentStmt instanceof Stmt) { - return null; - } - - $parentNode = $node->getAttribute('parent'); - - $logicalAndOrOr = $this->isName($node->name, 'logicalAnd') ? 'logicalAnd' : 'logicalOr'; - - $queryVariable = $node->var instanceof Variable ? $node->var : new Variable('query'); - - if ($parentNode instanceof Assign && $parentNode->expr instanceof MethodCall) { - // FIXME: This case is quite complicated as we don't really know how the code looks like. This is WIP for now... - // This is how a real world example could look like. See: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72244/8/typo3/sysext/beuser/Classes/Domain/Repository/BackendUserRepository.php - // $constraints = []; - // $query = $this->createQuery(); - // $query->setOrderings(['userName' => QueryInterface::ORDER_ASCENDING]); - // // Username - // if ($demand->getUserName() !== '') { - // $searchConstraints = []; - // $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users'); - // foreach (['userName', 'realName'] as $field) { - // $searchConstraints[] = $query->like( - // $field, - // '%' . $queryBuilder->escapeLikeWildcards($demand->getUserName()) . '%' - // ); - // } - // if (MathUtility::canBeInterpretedAsInteger($demand->getUserName())) { - // $searchConstraints[] = $query->equals('uid', (int)$demand->getUserName()); - // } - // $constraints[] = $query->logicalOr($searchConstraints); - // } - $if = $this->createIfForAssignment($parentNode, $firstArgument); - } else { - $if = $this->createIfForNormalMethod($firstArgument, $queryVariable, $logicalAndOrOr); - } - - if (! ($parentIfStatement instanceof If_)) { - $this->nodesToAddCollector->addNodeBeforeNode($if, $node); - return $node; - } - - if ($this->isBinaryOpAndNameAppearsInConditions($parentIfStatement, $firstArgument) - || $this->isBooleanNotAndNameAppears($parentIfStatement, $firstArgument) - || $this->isEmptyAndNameAppears($parentIfStatement, $firstArgument) - || $this->isVariableAndNameAppears($parentIfStatement, $firstArgument) - ) { - $this->removeNode($parentIfStatement); - $this->nodesToAddCollector->addNodeBeforeNode($if, $parentIfStatement); - } else { - #$this->removeNode($node); - $this->nodesToAddCollector->addNodeBeforeNode($if, $node); - } - - return $node; - } - - private function createIfForAssignment(Assign $parentNode, Variable $firstArgument): If_ - { - $ifExpression = clone $parentNode; - $ifExpression->expr = $this->nodeFactory->createFuncCall('reset', [$firstArgument]); - - return new If_( - new Identical($this->nodeFactory->createFuncCall('count', [$firstArgument]), new LNumber(1)), - [ - 'stmts' => [$ifExpression], - 'elseifs' => [ - new ElseIf_( - new GreaterOrEqual( - $this->nodeFactory->createFuncCall('count', [$firstArgument]), - new LNumber(2) - ), - [new Expression($parentNode->expr)] - ), - ], - ] - ); - } - - private function createIfForNormalMethod( - Variable $firstArgument, - Variable $queryVariable, - string $logicalAndOrOr - ): If_ { - return new If_( - new Identical($this->nodeFactory->createFuncCall('count', [$firstArgument]), new LNumber(1)), - [ - 'stmts' => [ - new Expression( - $this->nodeFactory->createMethodCall( - $queryVariable, - 'matching', - [new Arg($this->nodeFactory->createFuncCall('reset', [$firstArgument]))] - ) - ), - ], - 'elseifs' => [ - new ElseIf_( - new GreaterOrEqual( - $this->nodeFactory->createFuncCall('count', [$firstArgument]), - new LNumber(2) - ), - [ - new Expression( - $this->nodeFactory->createMethodCall( - $queryVariable, - 'matching', - [ - new Arg( - $this->nodeFactory->createMethodCall( - $queryVariable, - $logicalAndOrOr, - [new Arg($firstArgument, false, true)] - ), - ), - ] - ) - ), - ] - ), - ], - ] - ); - } - - private function isBinaryOpAndNameAppearsInConditions(If_ $parentIfStatement, Variable $firstArgument): bool - { - if (! ($parentIfStatement->cond instanceof BinaryOp)) { - return false; - } - - /** @var string $name */ - $name = $firstArgument->name; - - return ($parentIfStatement->cond->left instanceof Variable && $this->isName( - $parentIfStatement->cond->left, - $name - )) - || ($parentIfStatement->cond->right instanceof Variable && $this->isName( - $parentIfStatement->cond->right, - $name - )); - } - - private function isBooleanNotAndNameAppears(If_ $parentIfStatement, Variable $firstArgument): bool - { - if (! ($parentIfStatement->cond instanceof BooleanNot)) { - return false; - } - - /** @var string $name */ - $name = $firstArgument->name; - - if ($parentIfStatement->cond->expr instanceof Variable) { - return $this->isName($parentIfStatement->cond->expr, $name); - } - - if ($parentIfStatement->cond->expr instanceof Empty_) { - return $this->isName($parentIfStatement->cond->expr->expr, $name); - } - - return false; - } - - private function isEmptyAndNameAppears(If_ $parentIfStatement, Variable $firstArgument): bool - { - if (! ($parentIfStatement->cond instanceof Empty_)) { - return false; - } - - /** @var string $name */ - $name = $firstArgument->name; - - return $this->isName($parentIfStatement->cond->expr, $name); - } - - private function isVariableAndNameAppears(If_ $parentIfStatement, Variable $firstArgument): bool - { - if (! ($parentIfStatement->cond instanceof Variable)) { - return false; - } - - /** @var string $name */ - $name = $firstArgument->name; - - return $this->isName($parentIfStatement->cond, $name); - } -} diff --git a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/fixture.php.inc b/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/fixture.php.inc deleted file mode 100644 index 97a482ccd..000000000 --- a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,40 +0,0 @@ -createQuery(); - $query->matching( - $query->logicalAnd([ - $query->lessThan('foo', 1), - $query->lessThan('bar', 1) - ]) - ); - } -} - -?> ------ -createQuery(); - $query->matching( - $query->logicalAnd($query->lessThan('foo', 1), $query->lessThan('bar', 1)) - ); - } -} - -?> diff --git a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/log_entry_repository.php.inc b/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/log_entry_repository.php.inc deleted file mode 100644 index 7d16555ce..000000000 --- a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/log_entry_repository.php.inc +++ /dev/null @@ -1,105 +0,0 @@ -createQuery(); - $queryConstraints = $this->createQueryConstraints($query, $constraint); - if (!empty($queryConstraints)) { - $query->matching($query->logicalAnd($queryConstraints)); - } - $query->setOrderings(['uid' => QueryInterface::ORDER_DESCENDING]); - $query->setLimit($constraint->getNumber()); - return $query->execute(); - } - - public function findByConstraintWithVariableComparison(Constraint $constraint): QueryResultInterface - { - $query = $this->createQuery(); - $queryConstraints = $this->createQueryConstraints($query, $constraint); - if ($queryConstraints) { - $query->matching($query->logicalAnd($queryConstraints)); - } - $query->setOrderings(['uid' => QueryInterface::ORDER_DESCENDING]); - $query->setLimit($constraint->getNumber()); - return $query->execute(); - } - - /** - * @param QueryInterface $query - * @param Constraint $constraint - * @return ConstraintInterface[] - */ - protected function createQueryConstraints(QueryInterface $query, Constraint $constraint): array - { - $queryConstraints = []; - $queryConstraints[] = $query->greaterThanOrEqual('tstamp', $constraint->getStartTimestamp()); - $queryConstraints[] = $query->lessThan('tstamp', $constraint->getEndTimestamp()); - return $queryConstraints; - } -} -?> ------ -createQuery(); - $queryConstraints = $this->createQueryConstraints($query, $constraint); - if (count($queryConstraints) === 1) { - $query->matching(reset($queryConstraints)); - } elseif (count($queryConstraints) >= 2) { - $query->matching($query->logicalAnd(...$queryConstraints)); - } - $query->setOrderings(['uid' => QueryInterface::ORDER_DESCENDING]); - $query->setLimit($constraint->getNumber()); - return $query->execute(); - } - - public function findByConstraintWithVariableComparison(Constraint $constraint): QueryResultInterface - { - $query = $this->createQuery(); - $queryConstraints = $this->createQueryConstraints($query, $constraint); - if (count($queryConstraints) === 1) { - $query->matching(reset($queryConstraints)); - } elseif (count($queryConstraints) >= 2) { - $query->matching($query->logicalAnd(...$queryConstraints)); - } - $query->setOrderings(['uid' => QueryInterface::ORDER_DESCENDING]); - $query->setLimit($constraint->getNumber()); - return $query->execute(); - } - - /** - * @param QueryInterface $query - * @param Constraint $constraint - * @return ConstraintInterface[] - */ - protected function createQueryConstraints(QueryInterface $query, Constraint $constraint): array - { - $queryConstraints = []; - $queryConstraints[] = $query->greaterThanOrEqual('tstamp', $constraint->getStartTimestamp()); - $queryConstraints[] = $query->lessThan('tstamp', $constraint->getEndTimestamp()); - return $queryConstraints; - } -} -?> diff --git a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_individual_arguments.php.inc b/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_individual_arguments.php.inc deleted file mode 100644 index a130405c1..000000000 --- a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_individual_arguments.php.inc +++ /dev/null @@ -1,18 +0,0 @@ -createQuery(); - $query->matching( - $query->logicalAnd($query->lessThan('foo', 1), $query->lessThan('bar', 1)) - ); - } -} - -?> diff --git a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_one_individual_argument.php.inc b/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_one_individual_argument.php.inc deleted file mode 100644 index 2d4d65ffe..000000000 --- a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_one_individual_argument.php.inc +++ /dev/null @@ -1,18 +0,0 @@ -createQuery(); - $query->matching( - $query->logicalAnd($query->lessThan('foo', 1)) - ); - } -} - -?> diff --git a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_variables_as_arguments.php.inc b/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_variables_as_arguments.php.inc deleted file mode 100644 index d052f775e..000000000 --- a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_variables_as_arguments.php.inc +++ /dev/null @@ -1,81 +0,0 @@ -createQuery(); - if ($constraints !== []) { - $query->matching($query->logicalAnd(...$constraints)); - } - } - - public function findAllForListWithDifferentQueryVariableName() - { - $constraints = []; - $q = $this->createQuery(); - if ($constraints !== []) { - $q->matching($q->logicalAnd(...$constraints)); - } - } - - public function findAllForListWithLogicalOr() - { - $constraints = []; - $q = $this->createQuery(); - if ($constraints !== []) { - $q->matching($q->logicalOr(...$constraints)); - } - } -} - -?> ------ -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)); - } - } -} - -?> diff --git a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_variables_as_arguments_migrated.php.inc b/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_variables_as_arguments_migrated.php.inc deleted file mode 100644 index 18f2c6b9f..000000000 --- a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_and_with_variables_as_arguments_migrated.php.inc +++ /dev/null @@ -1,87 +0,0 @@ -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)); - } - } -} - -?> ------ -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)); - } - } -} - -?> diff --git a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_or.php.inc b/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_or.php.inc deleted file mode 100644 index 9aa1cc7fc..000000000 --- a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/Fixture/query_logical_or.php.inc +++ /dev/null @@ -1,40 +0,0 @@ -createQuery(); - $query->matching( - $query->logicalOr([ - $query->lessThan('foo', 1), - $query->lessThan('bar', 1), - ]) - ); - } -} - -?> ------ -createQuery(); - $query->matching( - $query->logicalOr($query->lessThan('foo', 1), $query->lessThan('bar', 1)) - ); - } -} - -?> diff --git a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/HardenMethodSignatureOfLogicalAndAndLogicalOrRectorTest.php b/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/HardenMethodSignatureOfLogicalAndAndLogicalOrRectorTest.php deleted file mode 100644 index 304ab4e55..000000000 --- a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/HardenMethodSignatureOfLogicalAndAndLogicalOrRectorTest.php +++ /dev/null @@ -1,32 +0,0 @@ -doTestFile($filePath); - } - - /** - * @return Iterator> - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/config/configured_rule.php b/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/config/configured_rule.php deleted file mode 100644 index ba0bb42cc..000000000 --- a/tests/Rector/v12/v0/typo3/HardenMethodSignatureOfLogicalAndAndLogicalOrRector/config/configured_rule.php +++ /dev/null @@ -1,11 +0,0 @@ -import(__DIR__ . '/../../../../../../../config/config_test.php'); - $rectorConfig->rule(HardenMethodSignatureOfLogicalAndAndLogicalOrRector::class); -};