[JMS] Add RemoveJmsInjectParamsAnnotationRector and RemoveJmsServiceAnnotationRector

This commit is contained in:
TomasVotruba 2020-02-23 18:20:00 +01:00
parent 18b3b15f8c
commit 8347a4c784
17 changed files with 587 additions and 8 deletions

View File

@ -116,7 +116,8 @@
"Rector\\DoctrineGedmoToKnplabs\\": "rules/doctrine-gedmo-to-knplabs/src",
"Rector\\MinimalScope\\": "rules/minimal-scope/src",
"Rector\\Polyfill\\": "packages/polyfill/src",
"Rector\\CakePHPToSymfony\\": "rules/cakephp-to-symfony/src"
"Rector\\CakePHPToSymfony\\": "rules/cakephp-to-symfony/src",
"Rector\\JMS\\": "rules/jms/src"
},
"files": [
"src/functions/rector_dump_function.php"
@ -187,7 +188,8 @@
"Rector\\DoctrineGedmoToKnplabs\\Tests\\": "rules/doctrine-gedmo-to-knplabs/tests",
"Rector\\MinimalScope\\Tests\\": "rules/minimal-scope/tests",
"Rector\\Polyfill\\Tests\\": "packages/polyfill/tests",
"Rector\\CakePHPToSymfony\\Tests\\": "rules/cakephp-to-symfony/tests"
"Rector\\CakePHPToSymfony\\Tests\\": "rules/cakephp-to-symfony/tests",
"Rector\\JMS\\Tests\\": "rules/jms/tests"
},
"classmap": [
"tests/Source",

View File

@ -1,4 +1,4 @@
# All 457 Rectors Overview
# All 459 Rectors Overview
- [Projects](#projects)
- [General](#general)
@ -20,6 +20,7 @@
- [ElasticSearchDSL](#elasticsearchdsl)
- [FileSystemRector](#filesystemrector)
- [Guzzle](#guzzle)
- [JMS](#jms)
- [Laravel](#laravel)
- [Legacy](#legacy)
- [MinimalScope](#minimalscope)
@ -3985,6 +3986,55 @@ Changes getMessage(..., true) to getMessageAsArray()
<br>
## JMS
### `RemoveJmsInjectParamsAnnotationRector`
- class: [`Rector\JMS\Rector\ClassMethod\RemoveJmsInjectParamsAnnotationRector`](/../master/rules/jms/src/Rector/ClassMethod/RemoveJmsInjectParamsAnnotationRector.php)
- [test fixtures](/../master/rules/jms/tests/Rector/ClassMethod/RemoveJmsInjectParamsAnnotationRector/Fixture)
Removes JMS\DiExtraBundle\Annotation\InjectParams annotation
```diff
use JMS\DiExtraBundle\Annotation as DI;
class SomeClass
{
- /**
- * @DI\InjectParams({
- * "subscribeService" = @DI\Inject("app.email.service.subscribe"),
- * "ipService" = @DI\Inject("app.util.service.ip")
- * })
- */
public function __construct()
{
}
-}
+}
```
<br>
### `RemoveJmsInjectServiceAnnotationRector`
- class: [`Rector\JMS\Rector\Class_\RemoveJmsInjectServiceAnnotationRector`](/../master/rules/jms/src/Rector/Class_/RemoveJmsInjectServiceAnnotationRector.php)
- [test fixtures](/../master/rules/jms/tests/Rector/Class_/RemoveJmsInjectServiceAnnotationRector/Fixture)
Removes JMS\DiExtraBundle\Annotation\Services annotation
```diff
use JMS\DiExtraBundle\Annotation as DI;
-/**
- * @DI\Service("email.web.services.subscribe_token", public=true)
- */
class SomeClass
{
}
```
<br>
## Laravel
### `FacadeStaticCallToConstructorInjectionRector`

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocNode\JMS;
use JMS\DiExtraBundle\Annotation\InjectParams;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\ShortNameAwareTagInterface;
use Rector\BetterPhpDocParser\PhpDocNode\AbstractTagValueNode;
final class JMSInjectParamsTagValueNode extends AbstractTagValueNode implements ShortNameAwareTagInterface
{
/**
* @var string
*/
public const CLASS_NAME = InjectParams::class;
public function __construct(string $originalContent)
{
$this->originalContent = $originalContent;
}
public function __toString(): string
{
return $this->originalContent;
}
public function getShortName(): string
{
return '@DI\InjectParams';
}
}

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocNode\JMS;
use JMS\DiExtraBundle\Annotation\Service;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\ShortNameAwareTagInterface;
use Rector\BetterPhpDocParser\PhpDocNode\AbstractTagValueNode;
final class JMSServiceValueNode extends AbstractTagValueNode implements ShortNameAwareTagInterface
{
/**
* @var string
*/
public const CLASS_NAME = Service::class;
public function __construct(string $originalContent)
{
$this->originalContent = $originalContent;
}
public function __toString(): string
{
return $this->originalContent;
}
public function getShortName(): string
{
return '@DI\Service';
}
}

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocNodeFactory\JMS;
use JMS\DiExtraBundle\Annotation\InjectParams;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use Rector\BetterPhpDocParser\PhpDocNode\JMS\JMSInjectParamsTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNodeFactory\AbstractPhpDocNodeFactory;
final class JMSInjectParamsPhpDocNodeFactory extends AbstractPhpDocNodeFactory
{
public function getClass(): string
{
return InjectParams::class;
}
/**
* @return JMSInjectParamsTagValueNode|null
*/
public function createFromNodeAndTokens(Node $node, TokenIterator $tokenIterator): ?PhpDocTagValueNode
{
if (! $node instanceof ClassMethod) {
return null;
}
/** @var InjectParams|null $injectParams */
$injectParams = $this->nodeAnnotationReader->readMethodAnnotation($node, $this->getClass());
if ($injectParams === null) {
return null;
}
$annotationContent = $this->resolveContentFromTokenIterator($tokenIterator);
return new JMSInjectParamsTagValueNode($annotationContent);
}
}

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocNodeFactory\JMS;
use JMS\DiExtraBundle\Annotation\Service;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use Rector\BetterPhpDocParser\PhpDocNode\JMS\JMSServiceValueNode;
use Rector\BetterPhpDocParser\PhpDocNodeFactory\AbstractPhpDocNodeFactory;
final class JMSServicePhpDocNodeFactory extends AbstractPhpDocNodeFactory
{
public function getClass(): string
{
return Service::class;
}
/**
* @return JMSServiceValueNode|null
*/
public function createFromNodeAndTokens(Node $node, TokenIterator $tokenIterator): ?PhpDocTagValueNode
{
if (! $node instanceof ClassMethod && ! $node instanceof Class_) {
return null;
}
if ($node instanceof ClassMethod) {
/** @var Service|null $service */
$service = $this->nodeAnnotationReader->readMethodAnnotation($node, $this->getClass());
if ($service === null) {
return null;
}
}
if ($node instanceof Class_) {
/** @var Service|null $service */
$service = $this->nodeAnnotationReader->readClassAnnotation($node, $this->getClass());
if ($service === null) {
return null;
}
}
$annotationContent = $this->resolveContentFromTokenIterator($tokenIterator);
return new JMSServiceValueNode($annotationContent);
}
}

View File

@ -76,8 +76,8 @@ final class ComposerPackageAutoloadUpdater
return new Package(
'Rector\\' . $configuration->getPackage() . '\\',
'Rector\\' . $configuration->getPackage() . '\\Tests\\',
'packages/' . $configuration->getPackage() . '/src',
'packages/' . $configuration->getPackage() . '/tests'
'rules/' . $configuration->getPackageDirectory() . '/src',
'rules/' . $configuration->getPackageDirectory() . '/tests'
);
}

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\RectorGenerator\ValueObject;
use Nette\Utils\Strings;
use Rector\Core\Util\RectorStrings;
final class Configuration
{
@ -110,6 +111,11 @@ final class Configuration
return $this->package;
}
public function getPackageDirectory(): string
{
return RectorStrings::camelCaseToDashes($this->package);
}
public function getName(): string
{
return $this->name;

View File

@ -15,9 +15,6 @@ parameters:
- stubs
- compiler/src
autoload_files:
- ci/check_services_in_yaml_configs.php
paths:
- ci
- bin
@ -28,6 +25,7 @@ parameters:
- compiler/src
excludes_analyse:
- ci/check_services_in_yaml_configs.php
- "*/Expected/*"
# complex printer
- "packages/ContributorTools/src/Command/DumpNodesCommand.php"
@ -247,3 +245,6 @@ parameters:
- '#Right side of && is always true#'
- '#Parameter \#(.*?) (.*?) of class PhpParser\\Node\\Expr\\BinaryOp\\(.*?) constructor expects PhpParser\\Node\\Expr, PhpParser\\Node given#'
- '#Method Rector\\BetterPhpDocParser\\PhpDocNode\\JMS\\JMSInjectParamsTagValueNode\:\:__toString\(\) should return string but returns string\|null#'
- '#Method Rector\\BetterPhpDocParser\\PhpDocNode\\JMS\\JMSServiceValueNode\:\:__toString\(\) should return string but returns string\|null#'

View File

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace Rector\JMS\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocNode\JMS\JMSInjectParamsTagValueNode;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @see https://github.com/rectorphp/rector/issues/2864
*
* @see \Rector\JMS\Tests\Rector\ClassMethod\RemoveJmsInjectParamsAnnotationRector\RemoveJmsInjectParamsAnnotationRectorTest
*/
final class RemoveJmsInjectParamsAnnotationRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Removes JMS\DiExtraBundle\Annotation\InjectParams annotation', [
new CodeSample(
<<<'PHP'
use JMS\DiExtraBundle\Annotation as DI;
class SomeClass
{
/**
* @DI\InjectParams({
* "subscribeService" = @DI\Inject("app.email.service.subscribe"),
* "ipService" = @DI\Inject("app.util.service.ip")
* })
*/
public function __construct()
{
}
}
PHP
,
<<<'PHP'
use JMS\DiExtraBundle\Annotation as DI;
class SomeClass
{
public function __construct()
{
}
}
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, '__construct')) {
return null;
}
/** @var PhpDocInfo $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if (! $phpDocInfo->hasByType(JMSInjectParamsTagValueNode::class)) {
return null;
}
$phpDocInfo->removeByType(JMSInjectParamsTagValueNode::class);
return $node;
}
}

View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Rector\JMS\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocNode\JMS\JMSServiceValueNode;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @see https://github.com/rectorphp/rector/issues/2864
*
* @see \Rector\JMS\Tests\Rector\Class_\RemoveJmsInjectServiceAnnotationRector\RemoveJmsInjectServiceAnnotationRectorTest
*/
final class RemoveJmsInjectServiceAnnotationRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Removes JMS\DiExtraBundle\Annotation\Services annotation', [
new CodeSample(
<<<'PHP'
use JMS\DiExtraBundle\Annotation as DI;
/**
* @DI\Service("email.web.services.subscribe_token", public=true)
*/
class SomeClass
{
}
PHP
,
<<<'PHP'
use JMS\DiExtraBundle\Annotation as DI;
class SomeClass
{
}
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Class_::class, ClassMethod::class];
}
/**
* @param Class_|ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
/** @var PhpDocInfo $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if (! $phpDocInfo->hasByType(JMSServiceValueNode::class)) {
return null;
}
$phpDocInfo->removeByType(JMSServiceValueNode::class);
return $node;
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace Rector\JMS\Tests\Rector\ClassMethod\RemoveJmsInjectParamsAnnotationRector\Fixture;
use JMS\DiExtraBundle\Annotation as DI;
class SomeClass
{
/**
* @DI\InjectParams({
* "subscribeService" = @DI\Inject("app.email.service.subscribe"),
* "ipService" = @DI\Inject("app.util.service.ip")
* })
*/
public function __construct() {
}
}
?>
-----
<?php
namespace Rector\JMS\Tests\Rector\ClassMethod\RemoveJmsInjectParamsAnnotationRector\Fixture;
use JMS\DiExtraBundle\Annotation as DI;
class SomeClass
{
public function __construct() {
}
}
?>

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Rector\JMS\Tests\Rector\ClassMethod\RemoveJmsInjectParamsAnnotationRector;
use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\JMS\Rector\ClassMethod\RemoveJmsInjectParamsAnnotationRector;
final class RemoveJmsInjectParamsAnnotationRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return RemoveJmsInjectParamsAnnotationRector::class;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Rector\JMS\Tests\Rector\Class_\RemoveJmsInjectServiceAnnotationRector\Fixture;
use JMS\DiExtraBundle\Annotation as DI;
/**
* @DI\Service("email.web.services.subscribe_token", public=true)
*/
class SomeClass
{
}
?>
-----
<?php
namespace Rector\JMS\Tests\Rector\Class_\RemoveJmsInjectServiceAnnotationRector\Fixture;
use JMS\DiExtraBundle\Annotation as DI;
class SomeClass
{
}
?>

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Rector\JMS\Tests\Rector\Class_\RemoveJmsInjectServiceAnnotationRector;
use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\JMS\Rector\Class_\RemoveJmsInjectServiceAnnotationRector;
final class RemoveJmsInjectServiceAnnotationRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return RemoveJmsInjectServiceAnnotationRector::class;
}
}

View File

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace JMS\DiExtraBundle\Annotation;
// @see https://github.com/schmittjoh/JMSDiExtraBundle/blob/master/Annotation/InjectParams.php
if (class_exists('JMS\DiExtraBundle\Annotation\InjectParams')) {
return;
}
/**
* @Annotation
* @Target("METHOD")
*/
final class InjectParams
{
/**
* @var \JMS\DiExtraBundle\Annotation\Inject[]
*/
public $params = [];
}

View File

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace JMS\DiExtraBundle\Annotation;
// mimics @see https://github.com/schmittjoh/JMSDiExtraBundle/blob/master/Annotation/Service.php
if (class_exists('JMS\DiExtraBundle\Annotation\Service')) {
return;
}
/**
* @Annotation
* @Target({"CLASS", "METHOD"})
*/
class Service extends Reference
{
/** @var string */
public $id;
/** @var string */
public $parent;
/** @var bool */
public $public;
/** @var string */
public $scope;
/** @var bool */
public $shared;
/** @var string */
public $deprecated;
/** @var string */
public $decorates;
/**
* @var string
*
* @deprecated since version 1.8, to be removed in 2.0. Use $decorationInnerName instead.
*/
public $decoration_inner_name;
/** @var string */
public $decorationInnerName;
/** @var bool */
public $abstract;
/** @var array<string> */
public $environments = array();
/** @var bool */
public $autowire;
/** @var array<string> */
public $autowiringTypes;
}