Merge pull request #778 from bendavies/JmsInjectAnnotationRector-improve

impove JmsInjectAnnotationRector
This commit is contained in:
Tomáš Votruba 2018-11-15 14:26:12 +02:00 committed by GitHub
commit 6a94b8c274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 3 deletions

View File

@ -3,10 +3,10 @@
namespace Rector\Jms\Rector\Property;
use Nette\Utils\Strings;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\Application\Error;
use Rector\Application\ErrorCollector;
use Rector\Bridge\Contract\AnalyzedApplicationContainerInterface;
use Rector\NodeTypeResolver\Node\Attribute;
@ -118,6 +118,9 @@ CODE_SAMPLE
$this->docBlockAnalyzer->removeTagFromNode($node, self::INJECT_ANNOTATION);
// set to private
$node->flags = Class_::MODIFIER_PRIVATE;
$this->addPropertyToClass($node->getAttribute(Attribute::CLASS_NODE), $type, $this->getName($node));
return $node;
@ -151,13 +154,13 @@ CODE_SAMPLE
private function resolveServiceName(PhpDocTagNode $phpDocTagNode, Node $node): ?string
{
$injectTagContent = (string) $phpDocTagNode->value;
$match = Strings::match($injectTagContent, '#(\'|")(?<serviceName>[\w\._-]+)(\'|")#');
$match = Strings::match($injectTagContent, '#(\'|")(?<serviceName>.*?)(\'|")#');
if ($match['serviceName']) {
return $match['serviceName'];
}
$match = Strings::match($injectTagContent, '#(\'|")%(?<parameterName>[\w\._-]+)%(\'|")#');
$match = Strings::match($injectTagContent, '#(\'|")%(?<parameterName>.*?)%(\'|")#');
// it's parameter, we don't resolve that here
if (isset($match['parameterName'])) {
return null;

View File

@ -0,0 +1,17 @@
<?php
namespace Rector\Jms\Tests\Rector\Property\JmsInjectAnnotationRector\Wrong;
use JMS\DiExtraBundle\Annotation as DI;
class ClassWithPublicInjects
{
/**
* @var \Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslatorInterface
*/
private $translator;
public function __construct(\Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslatorInterface $translator)
{
$this->translator = $translator;
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Rector\Jms\Tests\Rector\Property\JmsInjectAnnotationRector\Wrong;
use JMS\DiExtraBundle\Annotation as DI;
class SomeController2
{
/**
* @var \Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeEntityManager
*/
private $entityManager;
public function __construct(\Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeEntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
}

View File

@ -23,6 +23,8 @@ final class JmsInjectAnnotationRectorTest extends AbstractRectorTestCase
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
yield [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'];
yield [__DIR__ . '/Wrong/wrong4.php.inc', __DIR__ . '/Correct/correct4.php.inc'];
yield [__DIR__ . '/Wrong/wrong5.php.inc', __DIR__ . '/Correct/correct5.php.inc'];
}
protected function provideConfig(): string

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\Jms\Tests\Rector\Property\JmsInjectAnnotationRector\Wrong;
use JMS\DiExtraBundle\Annotation as DI;
class ClassWithPublicInjects
{
/**
* @DI\Inject
*/
public $translator;
}

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\Jms\Tests\Rector\Property\JmsInjectAnnotationRector\Wrong;
use JMS\DiExtraBundle\Annotation as DI;
class SomeController2
{
/**
* @DI\Inject("Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeEntityManager")
*/
private $entityManager;
}

View File

@ -30,6 +30,7 @@ final class SomeKernelClass extends Kernel
$containerBuilder->setAlias(SomeTranslatorInterface::class, 'translator.data_collector');
$containerBuilder->register('entity.manager', SomeEntityManager::class);
$containerBuilder->setAlias(SomeEntityManager::class, 'entity.manager');
}
public function getCacheDir()