mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-22 02:52:23 +00:00
Added upgrade league/event to 3.0 (#5929)
This commit is contained in:
parent
7d6aab5e80
commit
2d469d3068
103
config/set/league-event-30.php
Normal file
103
config/set/league-event-30.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPStan\Type\ObjectWithoutClassType;
|
||||
use PHPStan\Type\StringType;
|
||||
use PHPStan\Type\VoidType;
|
||||
use Rector\Composer\Rector\ChangePackageVersionComposerRector;
|
||||
use Rector\Composer\ValueObject\PackageAndVersion;
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
|
||||
use Rector\Removing\Rector\Class_\RemoveParentRector;
|
||||
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
|
||||
use Rector\Renaming\Rector\Name\RenameClassRector;
|
||||
use Rector\Renaming\ValueObject\MethodCallRename;
|
||||
use Rector\Transform\Rector\Class_\AddInterfaceByParentRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector;
|
||||
use Rector\TypeDeclaration\ValueObject\AddParamTypeDeclaration;
|
||||
use Rector\TypeDeclaration\ValueObject\AddReturnTypeDeclaration;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->set(ChangePackageVersionComposerRector::class)
|
||||
->call('configure', [[
|
||||
ChangePackageVersionComposerRector::PACKAGES_AND_VERSIONS => ValueObjectInliner::inline([
|
||||
new PackageAndVersion('league/event', '^3.0'),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(RenameMethodRector::class)
|
||||
->call('configure', [[
|
||||
RenameMethodRector::METHOD_CALL_RENAMES => ValueObjectInliner::inline([
|
||||
new MethodCallRename('League\Event\EventInterface', 'getName', 'eventName'),
|
||||
new MethodCallRename('League\Event\EmitterInterface', 'emit', 'dispatch'),
|
||||
new MethodCallRename('League\Event\EmitterInterface', 'addListener', 'subscribeTo'),
|
||||
new MethodCallRename('League\Event\EmitterInterface', 'addOneTimeListener', 'subscribeOneTo'),
|
||||
new MethodCallRename('League\Event\EmitterInterface', 'useListenerProvider', 'subscribeListenersFrom'),
|
||||
new MethodCallRename('League\Event\ListenerInterface', 'handle', '__invoke'),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(AddParamTypeDeclarationRector::class)
|
||||
->call('configure', [[
|
||||
AddParamTypeDeclarationRector::PARAMETER_TYPEHINTS => ValueObjectInliner::inline([
|
||||
new AddParamTypeDeclaration(
|
||||
'League\Event\ListenerInterface',
|
||||
'__invoke',
|
||||
0,
|
||||
new ObjectWithoutClassType()
|
||||
),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(AddReturnTypeDeclarationRector::class)
|
||||
->call('configure', [[
|
||||
AddReturnTypeDeclarationRector::METHOD_RETURN_TYPES => ValueObjectInliner::inline([
|
||||
new AddReturnTypeDeclaration('League\Event\EventInterface', 'eventName', new StringType()),
|
||||
new AddReturnTypeDeclaration('League\Event\ListenerInterface', '__invoke', new VoidType()),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(RenameClassRector::class)
|
||||
->call('configure', [[
|
||||
RenameClassRector::OLD_TO_NEW_CLASSES => [
|
||||
'League\Event\Emitter' => 'League\Event\EventDispatcher',
|
||||
'League\Event\ListenerInterface' => 'League\Event\Listener',
|
||||
'League\Event\GeneratorInterface' => 'League\Event\EventGenerator',
|
||||
'League\Event\ListenerProviderInterface' => 'League\Event\ListenerSubscriber',
|
||||
'League\Event\ListenerAcceptorInterface' => 'League\Event\ListenerRegistry',
|
||||
],
|
||||
]]);
|
||||
|
||||
$services->set(AddInterfaceByParentRector::class)
|
||||
->call('configure', [[
|
||||
AddInterfaceByParentRector::INTERFACE_BY_PARENT => [
|
||||
'League\Event\AbstractEvent' => 'League\Event\HasEventName',
|
||||
'League\Event\AbstractListener' => 'League\Event\Listener',
|
||||
],
|
||||
]]);
|
||||
|
||||
$services->set(RemoveInterfacesRector::class)
|
||||
->call('configure', [[
|
||||
RemoveInterfacesRector::INTERFACES_TO_REMOVE => [
|
||||
'League\Event\EventInterface',
|
||||
]
|
||||
]]);
|
||||
|
||||
$services->set(RemoveParentRector::class)
|
||||
->call('configure', [[
|
||||
RemoveParentRector::PARENT_TYPES_TO_REMOVE => [
|
||||
'League\Event\AbstractEvent',
|
||||
'League\Event\Event',
|
||||
'League\Event\AbstractListener',
|
||||
]
|
||||
]]);
|
||||
};
|
|
@ -129,6 +129,7 @@ parameters:
|
|||
- '#Cognitive complexity for "Rector\\TypeDeclaration\\PHPStan\\Type\\ObjectTypeSpecifier\:\:matchShortenedObjectType\(\)" is 10, keep it under 9#'
|
||||
- '#Cognitive complexity for "Rector\\Core\\PhpParser\\Node\\Value\\ValueResolver\:\:getValue\(\)" is \d+, keep it under 9#'
|
||||
- '#Cognitive complexity for "Rector\\DeadCode\\NodeManipulator\\LivingCodeManipulator\:\:keepLivingCodeFromExpr\(\)" is \d+, keep it under 9#'
|
||||
- '#Cognitive complexity for "Rector\\Transform\\Rector\\Class_\\AddInterfaceByParentRector\:\:refactor\(\)" is \d+, keep it under 9#'
|
||||
|
||||
- '#Parameter \#1 \$type of method PhpParser\\Builder\\FunctionLike\:\:setReturnType\(\) expects PhpParser\\Node\\Name\|PhpParser\\Node\\NullableType\|string, PhpParser\\Node\\Identifier\|PhpParser\\Node\\Name\|PhpParser\\Node\\NullableType\|PhpParser\\Node\\UnionType given#'
|
||||
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class AddInterfaceByParentRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParent;
|
||||
|
||||
class SomeClass extends SomeParent
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParent;
|
||||
|
||||
class SomeClass extends SomeParent implements \Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeInterface
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeInterface;
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParent;
|
||||
|
||||
class SkipExisting extends SomeParent implements SomeInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParentChild;
|
||||
|
||||
class SkipIndirectChild extends SomeParentChild
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source;
|
||||
|
||||
interface SomeInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source;
|
||||
|
||||
class SomeParent
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source;
|
||||
|
||||
class SomeParentChild extends SomeParent
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeInterface;
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParent;
|
||||
use Rector\Transform\Rector\Class_\AddInterfaceByParentRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(AddInterfaceByParentRector::class)
|
||||
->call('configure', [[
|
||||
AddInterfaceByParentRector::INTERFACE_BY_PARENT => [
|
||||
SomeParent::class => SomeInterface::class,
|
||||
],
|
||||
]]);
|
||||
};
|
107
rules/Transform/Rector/Class_/AddInterfaceByParentRector.php
Normal file
107
rules/Transform/Rector/Class_/AddInterfaceByParentRector.php
Normal file
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Transform\Rector\Class_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\AddInterfaceByParentRectorTest
|
||||
*/
|
||||
final class AddInterfaceByParentRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const INTERFACE_BY_PARENT = 'interface_by_parent';
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private $interfaceByParent = [];
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add interface by parent', [
|
||||
new ConfiguredCodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass extends SomeParent
|
||||
{
|
||||
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass extends SomeParent implements SomeInterface
|
||||
{
|
||||
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, [
|
||||
self::INTERFACE_BY_PARENT => [
|
||||
'SomeParent' => 'SomeInterface',
|
||||
],
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Class_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
/** @var Scope $scope */
|
||||
$scope = $node->getAttribute(AttributeKey::SCOPE);
|
||||
$classReflection = $scope->getClassReflection();
|
||||
if (! $classReflection instanceof ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parentClassReflection = $classReflection->getParentClass();
|
||||
if (! $parentClassReflection) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($this->interfaceByParent as $parentName => $interfaceName) {
|
||||
if ($parentName !== $parentClassReflection->getName()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($node->implements as $implement) {
|
||||
if ($this->isName($implement, $interfaceName)) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
$node->implements[] = new FullyQualified($interfaceName);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, array<string, string>> $configuration
|
||||
*/
|
||||
public function configure(array $configuration): void
|
||||
{
|
||||
$this->interfaceByParent = $configuration[self::INTERFACE_BY_PARENT] ?? [];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user