add few definitions

This commit is contained in:
Tomas Votruba 2018-04-08 13:51:26 +02:00
parent 120b0b1cf2
commit 1bc8f2d65c
12 changed files with 214 additions and 31 deletions

View File

@ -7,15 +7,10 @@ use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\Node\Attribute;
use Rector\Rector\AbstractPHPUnitRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use Rector\ReflectionDocBlock\NodeAnalyzer\DocBlockAnalyzer;
/**
* Before:
* - @scenario
*
* After:
* - @test
*/
final class AnnotationReplacerRector extends AbstractPHPUnitRector
{
/**
@ -42,6 +37,18 @@ final class AnnotationReplacerRector extends AbstractPHPUnitRector
$this->classToAnnotationMap = $classToAnnotationMap;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Turns defined annotations above properties and methods to their new values.', [
new CodeSample(
'/** @test */
public function someMethod() {};',
'/** @scenario */
public function someMethod() {};'
)
]);
}
public function isCandidate(Node $node): bool
{
if ($this->shouldSkip($node)) {

View File

@ -15,6 +15,8 @@ use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeAnalyzer\StaticMethodCallAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Rector\Dynamic\Configuration\ArgumentReplacerRecipe;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class ArgumentReplacerRector extends AbstractRector
{
@ -65,6 +67,21 @@ final class ArgumentReplacerRector extends AbstractRector
$this->constExprEvaluator = $constExprEvaluator;
}
/**
* @todo complete list with all possibilities
*/
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Adds, removes or replaces defined arguments in defined methods and their calls.', [
new CodeSample(
'$containerBuilder = new Symfony\Component\DependencyInjection\ContainerBuilder;
$containerBuilder->compile();',
'$containerBuilder = new Symfony\Component\DependencyInjection\ContainerBuilder;
$containerBuilder->compile(true);'
)
]);
}
public function isCandidate(Node $node): bool
{
$this->activeArgumentReplacerRecipes = $this->matchArgumentChanges($node);

View File

@ -8,6 +8,8 @@ use PhpParser\Node\Identifier;
use Rector\NodeAnalyzer\ClassConstAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class ClassConstantReplacerRector extends AbstractRector
{
@ -48,6 +50,16 @@ final class ClassConstantReplacerRector extends AbstractRector
$this->identifierRenamer = $identifierRenamer;
}
/**
* @todo complete list with all possibilities
*/
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Replaces defined class constants in their calls.', [
new CodeSample('$value = SomeClass::OLD_CONSTANT;','$value = SomeClass::NEW_CONSTANT;')
]);
}
public function isCandidate(Node $node): bool
{
$this->activeType = null;

View File

@ -8,6 +8,8 @@ use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Use_;
use Rector\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class ClassReplacerRector extends AbstractRector
{
@ -24,6 +26,16 @@ final class ClassReplacerRector extends AbstractRector
$this->oldToNewClasses = $oldToNewClasses;
}
/**
* @todo complete list with all possibilities
*/
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Replaces defined classes by new ones.', [
new CodeSample('$value = new SomeOldClass;','$value = new SomeNewClass;')
]);
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof Name) {

View File

@ -10,11 +10,13 @@ use Rector\Node\Attribute;
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* This Rector handles 2 things:
* - removes "$return this;" method bodies
* - changes fluent calls to standalone calls
* Inspiration:
* - https://ocramius.github.io/blog/fluent-interfaces-are-evil/
* - http://www.yegor256.com/2018/03/13/fluent-interfaces.html
*/
final class FluentReplaceRector extends AbstractRector
{
@ -39,6 +41,49 @@ final class FluentReplaceRector extends AbstractRector
$this->methodCallNodeFactory = $methodCallNodeFactory;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Turns fluent interfaces to classic ones.', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function someFunction()
{
return $this;
}
public function otherFunction()
{
return $this;
}
}
$someClass = new SomeClass();
$someClass->someFunction()
->otherFunction();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function someFunction()
{
}
public function otherFunction()
{
}
}
$someClass = new SomeClass();
$someClass->someFunction();
$someClass->otherFunction();
CODE_SAMPLE
)
]);
}
public function isCandidate(Node $node): bool
{
// @todo this run has to be first, dual run?

View File

@ -13,6 +13,8 @@ use Rector\NodeAnalyzer\MethodNameAnalyzer;
use Rector\NodeAnalyzer\StaticMethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class MethodNameReplacerRector extends AbstractRector
{
@ -78,6 +80,24 @@ final class MethodNameReplacerRector extends AbstractRector
$this->identifierRenamer = $identifierRenamer;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Turns method names to new ones.', [
new CodeSample(
<<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->oldMethod();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->newMethod();
CODE_SAMPLE
),
new CodeSample('SomeClass::oldStaticMethod();', 'SomeClass::newStaticMethod();'),
]);
}
public function isCandidate(Node $node): bool
{
$this->activeTypes = [];

View File

@ -11,6 +11,8 @@ use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use Rector\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class NamespaceReplacerRector extends AbstractRector
{
@ -29,6 +31,13 @@ final class NamespaceReplacerRector extends AbstractRector
$this->oldToNewNamespaces = $oldToNewNamespaces;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Replaces old namespace by new one.', [
new CodeSample('$someObject = new SomeOldNamespace\SomeClass;', '$someObject = new SomeNewNamespace\SomeClass;'),
]);
}
public function isCandidate(Node $node): bool
{
if (! $this->isGivenKind($node, [Namespace_::class, Use_::class, Name::class, FullyQualified::class])) {

View File

@ -15,19 +15,9 @@ use Rector\BetterReflection\Reflection\TypeAnalyzer;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* Useful when parent class or interface gets new typehints,
* that breaks contract with child instances.
*
* E.g. interface SomeInterface
* {
* public read($content);
* }
*
* After
* public read(string $content);
*/
final class ParentTypehintedArgumentRector extends AbstractRector
{
/**
@ -64,6 +54,37 @@ final class ParentTypehintedArgumentRector extends AbstractRector
$this->nodeTypeResolver = $nodeTypeResolver;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Changes defined parent class typehints.', [
new CodeSample(
<<<'CODE_SAMPLE'
interface SomeInterface
{
public read(string $content);
}
class SomeClass implements SomeInterface
{
public read($content);
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
interface SomeInterface
{
public read(string $content);
}
class SomeClass implements SomeInterface
{
public read(string $content);
}
CODE_SAMPLE
),
]);
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof ClassMethod) {

View File

@ -8,6 +8,8 @@ use PhpParser\Node\Identifier;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class PropertyNameReplacerRector extends AbstractRector
{
@ -48,6 +50,13 @@ final class PropertyNameReplacerRector extends AbstractRector
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Replaces defined old properties by new ones.', [
new CodeSample('$someObject->someOldProperty;', '$someObject->someNewProperty;'),
]);
}
public function isCandidate(Node $node): bool
{
$this->activeTypes = [];

View File

@ -11,16 +11,9 @@ use Rector\Node\MethodCallNodeFactory;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* Example - from:
* - $result = $object->property;
* - $object->property = $value;
*
* To
* - $result = $object->getProperty();
* - $object->setProperty($value);
*/
final class PropertyToMethodRector extends AbstractRector
{
/**
@ -67,6 +60,23 @@ final class PropertyToMethodRector extends AbstractRector
$this->methodCallNodeFactory = $methodCallNodeFactory;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Replaces properties assign calls be defined methods.', [
new CodeSample(
<<<'CODE_SAMPLE'
$result = $object->property;
$object->property = $value;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$result = $object->getProperty();
$object->setProperty($value);
CODE_SAMPLE
),
]);
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof Assign) {

View File

@ -12,6 +12,8 @@ use Rector\Builder\StatementGlue;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class PseudoNamespaceToNamespaceRector extends AbstractRector
{
@ -55,6 +57,13 @@ final class PseudoNamespaceToNamespaceRector extends AbstractRector
$this->statementGlue = $statementGlue;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Replaces defined Pseudo_Namespaces by Namespace\Ones.', [
new CodeSample('$someServie = Some_Object;', '$someServie = Some\Object;')
]);
}
public function isCandidate(Node $node): bool
{
$name = $this->resolveNameFromNode($node);

View File

@ -10,6 +10,8 @@ use PhpParser\Node\Stmt\Property;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use Rector\ReflectionDocBlock\NodeAnalyzer\DocBlockAnalyzer;
final class ValueObjectRemoverRector extends AbstractRector
@ -42,6 +44,16 @@ final class ValueObjectRemoverRector extends AbstractRector
$this->nodeTypeResolver = $nodeTypeResolver;
}
/**
* @todo complete the rest of cases
*/
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('[Dynamic] Remove values objects and use directly the value.', [
new CodeSample('$someValue = new SomeValueObject("just_a_string");', '$someValue = "just_a_string";')
]);
}
public function isCandidate(Node $node): bool
{
if ($node instanceof New_) {