mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 19:53:14 +01:00
specify GetterToPropertyRector
This commit is contained in:
parent
3b22a89ff4
commit
7c48c9a122
@ -11,6 +11,7 @@ use Rector\Naming\PropertyNaming;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\Node\PropertyFetchNodeFactory;
|
||||
use Rector\NodeAnalyzer\Contrib\Symfony\ContainerCallAnalyzer;
|
||||
use Rector\NodeAnalyzer\MethodCallAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
@ -47,18 +48,25 @@ final class GetterToPropertyRector extends AbstractRector
|
||||
*/
|
||||
private $serviceTypeForNameProvider;
|
||||
|
||||
/**
|
||||
* @var MethodCallAnalyzer
|
||||
*/
|
||||
private $methodCallAnalyzer;
|
||||
|
||||
public function __construct(
|
||||
PropertyNaming $propertyNaming,
|
||||
ClassPropertyCollector $classPropertyCollector,
|
||||
PropertyFetchNodeFactory $propertyFetchNodeFactory,
|
||||
ContainerCallAnalyzer $containerCallAnalyzer,
|
||||
ServiceTypeForNameProviderInterface $serviceTypeForNameProvider
|
||||
ServiceTypeForNameProviderInterface $serviceTypeForNameProvider,
|
||||
MethodCallAnalyzer $methodCallAnalyzer
|
||||
) {
|
||||
$this->propertyNaming = $propertyNaming;
|
||||
$this->classPropertyCollector = $classPropertyCollector;
|
||||
$this->propertyFetchNodeFactory = $propertyFetchNodeFactory;
|
||||
$this->containerCallAnalyzer = $containerCallAnalyzer;
|
||||
$this->serviceTypeForNameProvider = $serviceTypeForNameProvider;
|
||||
$this->methodCallAnalyzer = $methodCallAnalyzer;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
@ -67,7 +75,11 @@ final class GetterToPropertyRector extends AbstractRector
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->containerCallAnalyzer->isThisCall($node);
|
||||
return $this->methodCallAnalyzer->isTypeAndMethod(
|
||||
$node,
|
||||
'Symfony\Bundle\FrameworkBundle\Controller\Controller',
|
||||
'get'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,8 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
class ClassWithNamedService1 extends Controller
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
|
||||
class ClassWithNamedService extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \stdClass
|
||||
@ -12,6 +14,10 @@ class ClassWithNamedService1 extends Controller
|
||||
}
|
||||
public function render()
|
||||
{
|
||||
$this->stdClass->render();
|
||||
$someService = $this->stdClass;
|
||||
|
||||
$this->renderTwig([
|
||||
'posts' => $this->stdClass->callMe()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
class ClassWithNamedService2 implements ContainerAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var \stdClass
|
||||
*/
|
||||
private $stdClass;
|
||||
public function __construct(\stdClass $stdClass)
|
||||
{
|
||||
$this->stdClass = $stdClass;
|
||||
}
|
||||
public function render()
|
||||
{
|
||||
$someService = $this->stdClass;
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
class ClassWithNamedService3 implements ContainerAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var \stdClass
|
||||
*/
|
||||
private $stdClass;
|
||||
public function __construct(\stdClass $stdClass)
|
||||
{
|
||||
$this->stdClass = $stdClass;
|
||||
}
|
||||
public function render()
|
||||
{
|
||||
$someService = $this->stdClass;
|
||||
$someResult = $this->stdClass->callMe();
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
namespace SomeNamespace;
|
||||
|
||||
class ClassWithNamedService4 implements ContainerAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var \stdClass
|
||||
*/
|
||||
private $stdClass;
|
||||
public function __construct(\stdClass $stdClass)
|
||||
{
|
||||
$this->stdClass = $stdClass;
|
||||
}
|
||||
public function render()
|
||||
{
|
||||
$this->renderTwig([
|
||||
'posts' => $this->stdClass->callMe()
|
||||
]);
|
||||
}
|
||||
}
|
@ -24,9 +24,6 @@ final class GetterToPropertyRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
return [
|
||||
[__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'],
|
||||
[__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'],
|
||||
[__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'],
|
||||
[__DIR__ . '/Wrong/wrong4.php.inc', __DIR__ . '/Correct/correct4.php.inc'],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,15 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
class ClassWithNamedService1 extends Controller
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
|
||||
class ClassWithNamedService extends Controller
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
$this->get('some_service')->render();
|
||||
$someService = $this->get('some_service');
|
||||
|
||||
$this->renderTwig([
|
||||
'posts' => $this->get('some_service')->callMe()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +0,0 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
class ClassWithNamedService2 implements ContainerAwareInterface
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
$someService = $this->get('some_service');
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
class ClassWithNamedService3 implements ContainerAwareInterface
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
$someService = $this->get('some_service');
|
||||
$someResult = $this->get('some_service')->callMe();
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
namespace SomeNamespace;
|
||||
|
||||
class ClassWithNamedService4 implements ContainerAwareInterface
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
$this->renderTwig([
|
||||
'posts' => $this->get('some_service')->callMe()
|
||||
]);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user