Skip to content

Commit

Permalink
[BUGFIX] Check if property and param names match and rename if not
Browse files Browse the repository at this point in the history
Fixes: #4447
  • Loading branch information
simonschaufi committed Nov 23, 2024
1 parent 0a0209c commit 55006b6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace Ssch\TYPO3Rector\CodeQuality\General;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\ObjectType;
use Rector\NodeManipulator\ClassDependencyManipulator;
Expand Down Expand Up @@ -96,7 +99,6 @@ public function refactor(Node $node): ?Node
$node->getMethods(),
static fn ($classMethod) => str_starts_with((string) $classMethod->name, 'inject')
);

if ($injectMethods === []) {
return null;
}
Expand All @@ -107,21 +109,37 @@ public function refactor(Node $node): ?Node
continue;
}

reset($params);

/** @var Param $param */
$param = current($params);

if (! $param->type instanceof FullyQualified) {
continue;
}

$paramName = $this->getName($param->var);

if ($paramName === null) {
continue;
}

if (isset($injectMethod->stmts[0]) && $injectMethod->stmts[0] instanceof Expression) {
// check for the property name and if they match
$statement = $injectMethod->stmts[0];

$assign = $statement->expr;
if (! $assign instanceof Assign) {
continue;
}

$propertyFetch = $assign->var;
if (! $propertyFetch instanceof PropertyFetch) {
continue;
}

$paramName = $this->getName($propertyFetch->name);
if ($paramName === null) {
continue;
}
}

$this->classDependencyManipulator->addConstructorDependency(
$node,
new PropertyMetadata($paramName, new ObjectType((string) $param->type), Class_::MODIFIER_PROTECTED)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\General\InjectMethodToConstructorInjectionRector\Fixture;

use TYPO3\CMS\Core\Cache\CacheManager;

class Service
{
private CacheManager $cacheManagerWithDifferentVariableName;

public function injectCacheManager(CacheManager $cacheManager): void
{
$this->cacheManagerWithDifferentVariableName = $cacheManager;
}
}
?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\General\InjectMethodToConstructorInjectionRector\Fixture;

use TYPO3\CMS\Core\Cache\CacheManager;

class Service
{
private CacheManager $cacheManagerWithDifferentVariableName;

public function __construct(CacheManager $cacheManagerWithDifferentVariableName)
{
$this->cacheManagerWithDifferentVariableName = $cacheManagerWithDifferentVariableName;
}
}
?>

0 comments on commit 55006b6

Please sign in to comment.