Merge pull request #39 from TomasVotruba/levels

Use levels and configs over per Rector setup
This commit is contained in:
Tomáš Votruba 2017-09-09 20:53:34 +02:00 committed by GitHub
commit dfa47b493e
47 changed files with 61 additions and 286 deletions

View File

@ -5,22 +5,14 @@
This tool will **upgrade your application** for you.
## All Reconstructors
### [Nette](https://github.com/nette/)
At the moment these packages are supported:
- `FormCallbackRector`
- `InjectPropertyRector`
- `HtmlAddMethodRector`
- `NetteObjectToSmartTraitRector`
- `RemoveConfiguratorConstantsRector`
- and many others
### [Symfony](https://github.com/symfony/)
- `NamedServicesToConstructorNodeTraverser`
- and many others
- [Nette](/src/Rector/Contrib/Nette)
- [Symfony](/src/Rector/Contrib/Symfony)
- [PHPUnit](/src/Rector/Contrib/PHPUnit)
- [PHP_CodeSniffer](/src/Rector/Contrib/PHP_CodeSniffer)
## Install
@ -32,35 +24,17 @@ composer require rector/rector --dev
## Use (WIP)
```bash
vendor/bin/rector reconstruct src --framework Nette --to-version 2.4
vendor/bin/rector reconstruct src --framework Symfony --to-version 3.3
vendor/bin/rector reconstruct src --level=nette24
vendor/bin/rector reconstruct src --level=symfony40
```
### How to Add New Rector
### 6 Steps to Add New Rector
Just extend `Rector\Rector\AbstractRector`.
It will prepare **4 methods** - 2 informative and 2 processing the node.
It will prepare **2 methods** processing the node.
```php
/**
* A project that is related to this.
* E.g "Nette", "Symfony"
* Use constants from @see SetNames, if possible.
*/
public function getSetName(): string
{
}
/**
* Version this deprecations is active since.
* E.g. 2.3.
*/
public function sinceVersion(): float
{
}
public function isCandidate(Node $node): bool
{
}
@ -87,9 +61,11 @@ final class MyRector extends AbstractRector
3. Add a Test Case
4. Submit PR
4. Add to specific level, e.g. [`/src/config/level/nette/nette24.yml`](/src/config/level/nette/nette24.yml)
5. Submit PR
5. :check:
6. :check:

View File

@ -30,7 +30,7 @@ checkers:
extraAllowedClasses:
- 'PhpParser\Node\*'
- 'PhpParser\Comment\Doc'
- SplObjectStorage # temporary before adding to Symplify\CodingStandard package
- 'Rector\DeprecationExtractor\Deprecation\*'
parameters:
exclude_checkers:

View File

@ -19,16 +19,6 @@ final class ConfigurableChangeMethodNameRector extends AbstractChangeMethodNameR
$this->perClassOldToNewMethod = $perClassOldToNewMethod;
}
public function getSetName(): string
{
return 'dynamic';
}
public function sinceVersion(): float
{
return 0.0;
}
/**
* @return string[][] { class => [ oldMethod => newMethod ] }
*/

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Scalar\MagicConst\Method;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\DeprecationExtractor\Contract\Deprecation\DeprecationInterface;
use Rector\DeprecationExtractor\Deprecation\ClassMethodDeprecation;
use Rector\Exception\NotImplementedException;
use Rector\Node\Attribute;

View File

@ -34,7 +34,7 @@ final class MessageToDeprecationTransformerTest extends AbstractContainerAwareTe
public function test(): void
{
$this->deprecationExtractor->scanDirectories([
__DIR__ . '/../../../../vendor/symfony/dependency-injection'
__DIR__ . '/../../../../vendor/symfony/dependency-injection',
]);
$deprecationMessages = $this->deprecationCollector->getDeprecationMessages();

View File

@ -1,21 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\Contract\Deprecation;
use Rector\Rector\Set\SetNames;
interface DeprecationInterface
{
/**
* A project that is related to this.
* E.g "Nette", "Symfony"
* Use constants from @see SetNames, if possible.
*/
public function getSetName(): string;
/**
* Version this deprecations is active since.
* E.g. 2.3.
*/
public function sinceVersion(): float;
}

View File

@ -5,10 +5,9 @@ namespace Rector\Rector;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\Deprecation\DeprecationInterface;
use Rector\Contract\Rector\RectorInterface;
abstract class AbstractRector extends NodeVisitorAbstract implements DeprecationInterface, RectorInterface
abstract class AbstractRector extends NodeVisitorAbstract implements RectorInterface
{
/**
* @return null|int|Node

View File

@ -9,7 +9,6 @@ use PhpParser\Node\Expr\PropertyFetch;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject.
@ -31,16 +30,6 @@ final class FormCallbackRector extends AbstractRector
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::NETTE;
}
public function sinceVersion(): float
{
return 2.4;
}
/**
* Detects "$form->onSuccess[] = $this->someAction;"
*/

View File

@ -9,7 +9,6 @@ use Rector\Node\Attribute;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Covers https://forum.nette.org/cs/26672-missing-setrequired-true-false-on-field-abc-in-form
@ -37,16 +36,6 @@ final class FormSetRequiredRector extends AbstractRector
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::NETTE;
}
public function sinceVersion(): float
{
return 2.4;
}
public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isMethodCallTypeAndMethods($node, self::FORM_CLASS, ['addCondition'])) {

View File

@ -3,20 +3,9 @@
namespace Rector\Rector\Contrib\Nette;
use Rector\Rector\AbstractChangeMethodNameRector;
use Rector\Rector\Set\SetNames;
final class HtmlAddMethodRector extends AbstractChangeMethodNameRector
{
public function getSetName(): string
{
return SetNames::NETTE;
}
public function sinceVersion(): float
{
return 2.4;
}
/**
* @return string[][]
*/

View File

@ -9,7 +9,6 @@ use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\DocBlockAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
final class InjectPropertyRector extends AbstractRector
{
@ -56,16 +55,6 @@ final class InjectPropertyRector extends AbstractRector
return $propertyNode;
}
public function getSetName(): string
{
return SetNames::NETTE;
}
public function sinceVersion(): float
{
return 2.1;
}
private function addPropertyToCollector(Property $propertyNode): void
{
$propertyType = $this->docBlockAnalyzer->getAnnotationFromNode($propertyNode, 'var');

View File

@ -3,20 +3,9 @@
namespace Rector\Rector\Contrib\Nette;
use Rector\Rector\AbstractClassReplacerRector;
use Rector\Rector\Set\SetNames;
final class NetteConfiguratorRector extends AbstractClassReplacerRector
{
public function getSetName(): string
{
return SetNames::NETTE;
}
public function sinceVersion(): float
{
return 2.1;
}
/**
* @return string[]
*/

View File

@ -9,7 +9,6 @@ use Rector\Builder\StatementGlue;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject.
@ -42,16 +41,6 @@ final class NetteObjectToSmartTraitRector extends AbstractRector
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::NETTE;
}
public function sinceVersion(): float
{
return 2.2;
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof Class_ || $node->extends === null || $node->isAnonymous()) {

View File

@ -3,7 +3,6 @@
namespace Rector\Rector\Contrib\Nette;
use Rector\Rector\AbstractChangeMethodNameRector;
use Rector\Rector\Set\SetNames;
final class PhpGeneratorDocumentMethodRector extends AbstractChangeMethodNameRector
{
@ -16,16 +15,6 @@ final class PhpGeneratorDocumentMethodRector extends AbstractChangeMethodNameRec
'getDocuments' => 'getComment',
];
public function getSetName(): string
{
return SetNames::NETTE;
}
public function sinceVersion(): float
{
return 2.4;
}
/**
* @return string[][]
*/

View File

@ -7,7 +7,6 @@ use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Scalar\String_;
use Rector\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
final class RemoveConfiguratorConstantsRector extends AbstractRector
{
@ -38,16 +37,6 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector
return new String_($originalConstantValue);
}
public function getSetName(): string
{
return SetNames::NETTE;
}
public function sinceVersion(): float
{
return 2.3;
}
private function getClassNameFromClassConstFetch(ClassConstFetch $classConstFetchNode): string
{
/** @var Node\Name\FullyQualified $fqnName */

View File

@ -3,20 +3,12 @@
namespace Rector\Rector\Contrib\PHPUnit;
use Rector\Rector\AbstractClassReplacerRector;
use Rector\Rector\Set\SetNames;
/**
* Covers https://github.com/sebastianbergmann/phpunit/blob/master/ChangeLog-6.0.md#changed-1
*/
final class NamespaceClassRector extends AbstractClassReplacerRector
{
public function getSetName(): string
{
return SetNames::PHPUNIT;
}
public function sinceVersion(): float
{
return 6.0;
}
/**
* @return string[]
*/

View File

@ -3,20 +3,12 @@
namespace Rector\Rector\Contrib\PHP_CodeSniffer;
use Rector\Rector\AbstractClassReplacerRector;
use Rector\Rector\Set\SetNames;
/**
* Covers https://github.com/squizlabs/PHP_CodeSniffer/wiki/Version-3.0-Upgrade-Guide
*/
final class NamespaceClassRector extends AbstractClassReplacerRector
{
public function getSetName(): string
{
return SetNames::PHP_CODE_SNIFER;
}
public function sinceVersion(): float
{
return 3.0;
}
/**
* @return string[]
*/
@ -24,6 +16,9 @@ final class NamespaceClassRector extends AbstractClassReplacerRector
{
return [
'PHP_CodeSniffer_Sniffs_Sniff' => 'PHP_CodeSniffer\Sniffs\Sniff',
'PHP_CodeSniffer_File' => 'PHP_CodeSniffer\Files\File',
'PHP_CodeSniffer_Tokens' => 'PHP_CodeSniffer\Util\Tokens',
'StandardName_Tests_Category_TestSniffUnitTest' => 'PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest',
];
}
}

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\SymfonyExtra;
namespace Rector\Rector\Contrib\Symfony;
use Nette\Utils\Strings;
use PhpParser\Node;
@ -13,8 +13,7 @@ use Rector\Node\Attribute;
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\LocalKernel;
use Rector\Tests\Rector\Contrib\Symfony\GetterToPropertyRector\Source\LocalKernel;
/**
* Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#console
@ -77,16 +76,6 @@ final class CommandToConstructorInjectionRector extends AbstractRector
$this->symfonyContainerCallsAnalyzer = $symfonyContainerCallsAnalyzer;
}
public function getSetName(): string
{
return SetNames::SYMFONY_EXTRA;
}
public function sinceVersion(): float
{
return 3.3;
}
public function isCandidate(Node $node): bool
{
$class = (string) $node->getAttribute(Attribute::CLASS_NAME);

View File

@ -8,7 +8,6 @@ use PhpParser\Node\Scalar\String_;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#validator
@ -36,16 +35,6 @@ final class ConstraintUrlOptionRector extends AbstractRector
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::SYMFONY;
}
public function sinceVersion(): float
{
return 4.0;
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof ConstFetch) {

View File

@ -3,29 +3,13 @@
namespace Rector\Rector\Contrib\Symfony;
use Rector\Rector\AbstractChangeParentClassRector;
use Rector\Rector\Set\SetNames;
/**
* Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#validator
*
* Converts all:
* Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest
*
* into:
* Symfony\Component\Validator\Test\ConstraintValidatorTestCase
*/
final class ConstraintValidatorTestClassRenameRector extends AbstractChangeParentClassRector
{
public function getSetName(): string
{
return SetNames::SYMFONY;
}
public function sinceVersion(): float
{
return 4.0;
}
protected function getOldClassName(): string
{
return 'Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest';

View File

@ -8,7 +8,6 @@ use PhpParser\Node\Expr\MethodCall;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Converts all:
@ -29,16 +28,6 @@ final class FormIsValidRector extends AbstractRector
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::SYMFONY;
}
public function sinceVersion(): float
{
return 4.0;
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof MethodCall) {

View File

@ -3,7 +3,6 @@
namespace Rector\Rector\Contrib\Symfony;
use Rector\Rector\AbstractClassReplacerRector;
use Rector\Rector\Set\SetNames;
/**
* Ref.: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#frameworkbundle
@ -12,16 +11,6 @@ use Rector\Rector\Set\SetNames;
*/
final class FrameworkBundleClassReplacementsRector extends AbstractClassReplacerRector
{
public function getSetName(): string
{
return SetNames::SYMFONY;
}
public function sinceVersion(): float
{
return 4.0;
}
/**
* @return string[]
*/

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\SymfonyExtra;
namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
@ -11,8 +11,7 @@ use Rector\Node\Attribute;
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\LocalKernel;
use Rector\Tests\Rector\Contrib\Symfony\GetterToPropertyRector\Source\LocalKernel;
/**
* Converts all:
@ -95,14 +94,4 @@ final class GetterToPropertyRector extends AbstractRector
return $this->nodeFactory->createLocalPropertyFetch($propertyName);
}
public function getSetName(): string
{
return SetNames::SYMFONY_EXTRA;
}
public function sinceVersion(): float
{
return 3.3;
}
}

View File

@ -3,11 +3,9 @@
namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Converts all:
@ -65,16 +63,6 @@ final class StringFormTypeToClassRector extends AbstractRector
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::SYMFONY;
}
public function sinceVersion(): float
{
return 4.0;
}
public function isCandidate(Node $node): bool
{
return $node instanceof String_ && isset($this->nameToClassMap[$node->value]);

View File

@ -8,7 +8,6 @@ use PhpParser\Node\Scalar\String_;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Converts all:
@ -42,16 +41,6 @@ final class VarDumperTestTraitMethodArgsRector extends AbstractRector
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::SYMFONY;
}
public function sinceVersion(): float
{
return 4.0;
}
public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isStaticMethodCallTypeAndMethods(

View File

@ -17,7 +17,7 @@ final class SetNames
/**
* @var string
*/
public const SYMFONY_EXTRA = 'SymfonyExtra';
public const SYMFONY_EXTRA = 'Symfony';
/**
* @var string

View File

@ -0,0 +1,3 @@
checkers:
- Rector\Rector\Contrib\Nette\InjectPropertyRector
- Rector\Rector\Contrib\Nette\NetteConfiguratorRector

View File

@ -0,0 +1,2 @@
checkers:
- Rector\Rector\Contrib\Nette\RemoveConfiguratorConstantsRector

View File

@ -0,0 +1,6 @@
checkers:
- Rector\Rector\Contrib\Nette\FormCallbackRector
- Rector\Rector\Contrib\Nette\FormSetRequiredRector
- Rector\Rector\Contrib\Nette\HtmlAddMethodRector
- Rector\Rector\Contrib\Nette\NetteObjectToSmartTraitRector
- Rector\Rector\Contrib\Nette\PhpGeneratorDocumentMethodRector

View File

@ -0,0 +1,2 @@
rectors:
- Rector\Rector\Contrib\PHP_CodeSniffer\NamespaceClassRector

View File

@ -0,0 +1,2 @@
rectors:
- Rector\Rector\Contrib\PHPUnit\NamespaceClassRector

View File

@ -0,0 +1,3 @@
rectors:
- Rector\Rector\Contrib\Symfony\CommandToConstructorInjectionRector
- Rector\Rector\Contrib\Symfony\GetterToPropertyRector

View File

@ -0,0 +1,7 @@
rectors:
- Rector\Rector\Contrib\Symfony\ConstraintUrlOptionRector
- Rector\Rector\Contrib\Symfony\ConstraintValidatorTestClassRenameRector
- Rector\Rector\Contrib\Symfony\FormIsValidRector
- Rector\Rector\Contrib\Symfony\FrameworkBundleClassReplacementsRector
- Rector\Rector\Contrib\Symfony\StringFormTypeToClassRector
- Rector\Rector\Contrib\Symfony\VarDumperTestTraitMethodArgsRector

View File

@ -1,8 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\SymfonyExtra\CommandToConstructorInjectionRector;
namespace Rector\Tests\Rector\Contrib\Symfony\CommandToConstructorInjectionRector;
use Rector\Rector\Contrib\SymfonyExtra\CommandToConstructorInjectionRector;
use Rector\Rector\Contrib\Symfony\CommandToConstructorInjectionRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class Test extends AbstractRectorTestCase

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source;
namespace Rector\Tests\Rector\Contrib\Symfony\GetterToPropertyRector\Source;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

View File

@ -1,8 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector;
namespace Rector\Tests\Rector\Contrib\Symfony\GetterToPropertyRector;
use Rector\Rector\Contrib\SymfonyExtra\GetterToPropertyRector;
use Rector\Rector\Contrib\Symfony\GetterToPropertyRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class Test extends AbstractRectorTestCase