From 3b2304e1daa17cfccc1dc6718815da268d67af09 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 1 Aug 2018 18:15:06 +0200 Subject: [PATCH] add ConfiguredCodeSample --- .../Command/GenerateRectorOverviewCommand.php | 7 ++----- ...ertyInjectToConstructorInjectionRector.php | 14 ++++++++----- ...epositoryFromParentToConstructorRector.php | 11 +++++++--- .../PropertyAssignToMethodCallRector.php | 21 +++++++++++++++++-- .../config.yml | 3 ++- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/Console/Command/GenerateRectorOverviewCommand.php b/src/Console/Command/GenerateRectorOverviewCommand.php index d70110ab99e..dad28ad1db3 100644 --- a/src/Console/Command/GenerateRectorOverviewCommand.php +++ b/src/Console/Command/GenerateRectorOverviewCommand.php @@ -261,11 +261,8 @@ final class GenerateRectorOverviewCommand extends Command } } - /** - * @param mixed $configuration - */ - private function printCodeWrapped($configuration, string $format): void + private function printCodeWrapped(string $content, string $format): void { - $this->consoleStyle->writeln(sprintf('```%s%s%s%s```', $format, PHP_EOL, $configuration, PHP_EOL)); + $this->consoleStyle->writeln(sprintf('```%s%s%s%s```', $format, PHP_EOL, trim($content), PHP_EOL)); } } diff --git a/src/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector.php b/src/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector.php index 5b8725b8927..ac1f740abc6 100644 --- a/src/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector.php +++ b/src/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector.php @@ -12,7 +12,7 @@ use Rector\Builder\Class_\ClassPropertyCollector; use Rector\Node\Attribute; use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\Rector\AbstractRector; -use Rector\RectorDefinition\CodeSample; +use Rector\RectorDefinition\ConfiguredCodeSample; use Rector\RectorDefinition\RectorDefinition; /** @@ -47,7 +47,7 @@ final class AnnotatedPropertyInjectToConstructorInjectionRector extends Abstract ClassPropertyCollector $classPropertyCollector, DocBlockAnalyzer $docBlockAnalyzer, NodeTypeResolver $nodeTypeResolver, - string $annotation = 'inject' + string $annotation ) { $this->classPropertyCollector = $classPropertyCollector; $this->docBlockAnalyzer = $docBlockAnalyzer; @@ -87,11 +87,11 @@ final class AnnotatedPropertyInjectToConstructorInjectionRector extends Abstract return new RectorDefinition( 'Turns non-private properties with @annotation to private properties and constructor injection', [ - new CodeSample( + new ConfiguredCodeSample( <<<'CODE_SAMPLE' /** * @var SomeService - * @annotation + * @inject */ public $someService; CODE_SAMPLE @@ -107,7 +107,11 @@ public function __construct(SomeService $someService) $this->someService = $someService; } CODE_SAMPLE - ), + , + [ + '$annotation' => 'inject', + ] + ), ] ); } diff --git a/src/Rector/Architecture/RepositoryAsService/MoveRepositoryFromParentToConstructorRector.php b/src/Rector/Architecture/RepositoryAsService/MoveRepositoryFromParentToConstructorRector.php index dca6b026d95..f35cc147e70 100644 --- a/src/Rector/Architecture/RepositoryAsService/MoveRepositoryFromParentToConstructorRector.php +++ b/src/Rector/Architecture/RepositoryAsService/MoveRepositoryFromParentToConstructorRector.php @@ -16,7 +16,7 @@ use Rector\Exception\Bridge\RectorProviderException; use Rector\Node\Attribute; use Rector\Node\NodeFactory; use Rector\Rector\AbstractRector; -use Rector\RectorDefinition\CodeSample; +use Rector\RectorDefinition\ConfiguredCodeSample; use Rector\RectorDefinition\RectorDefinition; final class MoveRepositoryFromParentToConstructorRector extends AbstractRector @@ -104,7 +104,7 @@ final class MoveRepositoryFromParentToConstructorRector extends AbstractRector public function getDefinition(): RectorDefinition { return new RectorDefinition('Turns parent EntityRepository class to constructor dependency', [ - new CodeSample( + new ConfiguredCodeSample( <<<'CODE_SAMPLE' namespace App\Repository; @@ -133,7 +133,12 @@ final class PostRepository } } CODE_SAMPLE - ), + , + [ + '$entityRepositoryClass' => 'Doctrine\ORM\EntityRepository', + '$entityManagerClass' => 'Doctrine\ORM\EntityManager', + ] + ), ]); } diff --git a/src/Rector/Assign/PropertyAssignToMethodCallRector.php b/src/Rector/Assign/PropertyAssignToMethodCallRector.php index 023d7ee35e9..b83997e014f 100644 --- a/src/Rector/Assign/PropertyAssignToMethodCallRector.php +++ b/src/Rector/Assign/PropertyAssignToMethodCallRector.php @@ -9,7 +9,7 @@ use PhpParser\Node\Expr\Variable; use Rector\Node\MethodCallNodeFactory; use Rector\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\Rector\AbstractRector; -use Rector\RectorDefinition\CodeSample; +use Rector\RectorDefinition\ConfiguredCodeSample; use Rector\RectorDefinition\RectorDefinition; final class PropertyAssignToMethodCallRector extends AbstractRector @@ -40,6 +40,7 @@ final class PropertyAssignToMethodCallRector extends AbstractRector private $newMethodName; /** + * @todo check via https://github.com/rectorphp/rector/issues/548 * @param string[] $types */ public function __construct( @@ -59,7 +60,23 @@ final class PropertyAssignToMethodCallRector extends AbstractRector public function getDefinition(): RectorDefinition { return new RectorDefinition('Turns property assign of specific type and property name to method call', [ - new CodeSample('$control->oldProperty = false;', '$control->newMethodCall(false);'), + new ConfiguredCodeSample( +<<<'CODE_SAMPLE' +$someObject = new SomeClass; +$someObject->oldProperty = false; +CODE_SAMPLE + , +<<<'CODE_SAMPLE' +$someObject = new SomeClass; +$someObject->newMethodCall(false); +CODE_SAMPLE + , + [ + '$types' => ['SomeClass'], + '$oldPropertyName' => 'oldProperty', + '$newMethodName' => 'newMethodCall', + ] + ), ]); } diff --git a/tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/config.yml b/tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/config.yml index 783955e02d0..3e4c6d00c16 100644 --- a/tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/config.yml +++ b/tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/config.yml @@ -1,2 +1,3 @@ services: - Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector: ~ + Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector: + $annotation: 'inject'