Skip to content

Commit

Permalink
[ci-review] Rector Rectify
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user authored and helsner committed Jan 4, 2024
1 parent e7f4e60 commit f3de441
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
18 changes: 6 additions & 12 deletions src/Rector/v12/v3/typo3/MigrateMagicRepositoryMethodsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;
Expand Down Expand Up @@ -51,16 +50,6 @@ public function refactor(Node $node): ?Node
return null;
}

// TODO: check if method already exists in class

$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($node);
if ($methodReflection instanceof MethodReflection) {
$declaringClass = $methodReflection->getDeclaringClass();
if ($declaringClass instanceof ClassReflection) {
$classOfClassMethod = $declaringClass->getName();
}
}

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

if ($methodName === null) {
Expand All @@ -74,10 +63,15 @@ public function refactor(Node $node): ?Node
return null;
}

$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($node);
# Class reflection only works when the method exists - thus we don't migrate if it succeeds/is not null
if ($methodReflection !== null) {
return null;
}

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

// TODO: make this better somehow?
if (\str_starts_with($methodName, 'findBy')) {
$propertyName = str_replace('findBy', '', $methodName);
$newMethodCall = 'findBy';
Expand Down
1 change: 1 addition & 0 deletions stubs/TYPO3/CMS/Extbase/Persistence/ExampleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

class ExampleRepository extends Repository
{
public function findByMethodExists(string $random): void {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ namespace Ssch\TYPO3Rector\Tests\Rector\v12\v3\typo3\MigrateMagicRepositoryMetho

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

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

$exampleRepo->findByMethodExists('bar');

?>
-----
<?php
Expand All @@ -22,14 +23,15 @@ namespace Ssch\TYPO3Rector\Tests\Rector\v12\v3\typo3\MigrateMagicRepositoryMetho

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

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

$exampleRepo->findByMethodExists('bar');

?>

0 comments on commit f3de441

Please sign in to comment.