Skip to content

Commit

Permalink
covert this container property fetch as well
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 1, 2024
1 parent 0002694 commit 990bceb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Fixture;

use Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Source\SomeService;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

final class CoverContainerProperty extends Controller
{
public function configure()
{
$someType = $this->container->get(SomeService::class);
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Fixture;

use Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Source\SomeService;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

final class CoverContainerProperty extends Controller
{
public function __construct(private \Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Source\SomeService $someService)
{
}
public function configure()
{
$someType = $this->someService;
}
}

?>
8 changes: 8 additions & 0 deletions rules/DependencyInjection/ThisGetTypeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use Rector\NodeNameResolver\NodeNameResolver;

Expand Down Expand Up @@ -60,6 +61,13 @@ private function isValidContainerCall(MethodCall $methodCall): bool
return true;
}

if ($methodCall->var instanceof PropertyFetch && $this->nodeNameResolver->isName(
$methodCall->var->var,
'this'
) && $this->nodeNameResolver->isName($methodCall->var->name, 'container')) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

abstract class Controller
{
protected $container;

/**
* @param string|AbstractType|FormInterface $formType
*/
Expand Down

0 comments on commit 990bceb

Please sign in to comment.