specify GetterToPropertyRector

This commit is contained in:
TomasVotruba 2018-03-03 18:07:25 +01:00
parent 3b22a89ff4
commit 7c48c9a122
10 changed files with 30 additions and 97 deletions

View File

@ -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'
);
}
/**

View File

@ -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()
]);
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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()
]);
}
}

View File

@ -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'],
];
}

View File

@ -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()
]);
}
}

View File

@ -1,9 +0,0 @@
<?php declare (strict_types=1);
class ClassWithNamedService2 implements ContainerAwareInterface
{
public function render()
{
$someService = $this->get('some_service');
}
}

View File

@ -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();
}
}

View File

@ -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()
]);
}
}