mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-03 08:03:30 +00:00
Merge pull request #778 from bendavies/JmsInjectAnnotationRector-improve
impove JmsInjectAnnotationRector
This commit is contained in:
commit
6a94b8c274
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user