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

[FEATURE] Provide migration for extbase magic methods #3943

Merged
merged 5 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions config/v12/typo3-123.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');
$rectorConfig->rule(\Ssch\TYPO3Rector\Rector\v12\v3\typo3\MigrateMagicRepositoryMethodsRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,34 @@ public function change(string $condition): ?string
}

switch ($type) {
case 'TSFE' :
case 'TSFE':
$conditions[$key][] = $this->refactorTsfe($property, $operator, $value);
break;
case 'GP' :
case 'GP':
$conditions[$key][] = $this->refactorGetPost($property, $operator, $value);
break;
case 'LIT' :
case 'LIT':
$conditions[$key][] = sprintf('"%s" %s "%s"', $value, self::OPERATOR_MAPPING[$operator], $property);
break;
case 'ENV' :
case 'ENV':
$conditions[$key][] = $this->createEnvCondition($property, $operator, $value);
break;
case 'IENV' :
case 'IENV':
$conditions[$key][] = $this->createIndependentCondition($property, $operator, $value);
break;
case 'BE_USER' :
case 'BE_USER':
$conditions[$key][] = $this->createBackendUserCondition($property, $operator, $value);
break;
case '_GET' :
case '_GET':
$conditions[$key][] = $this->refactorGet($property, $operator, $value);
break;
case 'GPmerged' :
case 'GPmerged':
$conditions[$key][] = $this->refactorGetPost($property, $operator, $value);
break;
case '_POST' :
case '_POST':
$conditions[$key][] = $this->refactorPost($property, $operator, $value);
break;
default :
default:
$conditions[$key][] = $condition;
break;
}
Expand Down
126 changes: 126 additions & 0 deletions src/Rector/v12/v3/typo3/MigrateMagicRepositoryMethodsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v12\v3\typo3;

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.3/Deprecation-100071-MagicRepositoryFindByMethods.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v3\typo3\MigrateMagicRepositoryMethodsRector\MigrateMagicRepositoryMethodsRectorTest
*/
final class MigrateMagicRepositoryMethodsRector extends AbstractRector
{
private ReflectionResolver $reflectionResolver;

public function __construct(ReflectionResolver $reflectionResolver)
{
$this->reflectionResolver = $reflectionResolver;
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param Node\Expr\MethodCall $node
*/
public function refactor(Node $node): ?Node
{
// TODO: this doesn't work with my ExampleRepo Stub
simonschaufi marked this conversation as resolved.
Show resolved Hide resolved
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\CMS\Extbase\Persistence\Repository')
)) {
return null;
}

// TODO: check if method already exists in class

$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($node);
helsner marked this conversation as resolved.
Show resolved Hide resolved
if ($methodReflection instanceof MethodReflection) {
$declaringClass = $methodReflection->getDeclaringClass();
if ($declaringClass instanceof ClassReflection) {
$classOfClassMethod = $declaringClass->getName();
}
}

$methodName = $this->getName($node->name);

if ($methodName === null) {
return null;
}

if (! \str_starts_with($methodName, 'findBy')
&& ! \str_starts_with($methodName, 'findOneBy')
&& ! \str_starts_with($methodName, 'countBy')
) {
return null;
}

$propertyName = '';
$newMethodCall = '';

// TODO: make this better somehow?
if (\str_starts_with($methodName, 'findBy')) {
$propertyName = str_replace('findBy', '', $methodName);
$newMethodCall = 'findBy';
}

if (\str_starts_with($methodName, 'findOneBy')) {
$propertyName = str_replace('findOneBy', '', $methodName);
$newMethodCall = 'findOneBy';
}

if (\str_starts_with($methodName, 'countBy')) {
$propertyName = str_replace('countBy', '', $methodName);
$newMethodCall = 'count';
}

if ($propertyName === '' || $newMethodCall === '') {
return null;
}

$newArgs = new Array_([new ArrayItem($node->args[0]->value, new String_(lcfirst($propertyName)))]);

return $this->nodeFactory->createMethodCall($node->var, $newMethodCall, [$newArgs->items]);
}

/**
* @codeCoverageIgnore
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrate the magic findBy methods', [
new CodeSample(
<<<'CODE_SAMPLE'
$blogRepository->findByFooBar('bar');
$blogRepository->findOneByFoo('bar');
$blogRepository->countByFoo('bar');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$blogRepository->findBy(['fooBar' => 'bar']);
$blogRepository->findOneBy(['foo' => 'bar']);
$blogRepository->count(['foo' => 'bar']);
CODE_SAMPLE
),
]);
}
}
10 changes: 10 additions & 0 deletions stubs/TYPO3/CMS/Extbase/Persistence/ExampleRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace TYPO3\CMS\Extbase\Persistence;

class ExampleRepository extends Repository
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v3\typo3\MigrateMagicRepositoryMethodsRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\ExampleRepository;
use TYPO3\CMS\Extbase\Persistence\Repository;

$exampleRepo = GeneralUtility::makeInstance(Repository::class);
$exampleRepo->findByFoo('bar');
$exampleRepo->findByFooBar('bar');
$bar = 'bar';
$exampleRepo->findByFoo($bar);
$exampleRepo->findOneByFoo('bar');
$exampleRepo->countByFoo('bar');

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v3\typo3\MigrateMagicRepositoryMethodsRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\ExampleRepository;
use TYPO3\CMS\Extbase\Persistence\Repository;

$exampleRepo = GeneralUtility::makeInstance(Repository::class);
$exampleRepo->findBy(['foo' => 'bar']);
$exampleRepo->findBy(['fooBar' => 'bar']);
$bar = 'bar';
$exampleRepo->findBy(['foo' => $bar]);
$exampleRepo->findOneBy(['foo' => 'bar']);
$exampleRepo->count(['foo' => 'bar']);

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

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v3\typo3\MigrateMagicRepositoryMethodsRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class MigrateMagicRepositoryMethodsRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
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,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\Rector\v12\v3\typo3\MigrateMagicRepositoryMethodsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../../../config/config_test.php');
$rectorConfig->rule(MigrateMagicRepositoryMethodsRector::class);
};