mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 00:10:51 +00:00
[PHP 7.2] Add covariant param support for trait methods (#6190)
* [Downgrade PHP 7.2] Handle trait methods too * move DowngradeParameterTypeWideningRector to Class_ namespace * [ci-review] Rector Rectify Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
parent
a57fd196c8
commit
ff57c49662
|
@ -5,7 +5,7 @@ declare(strict_types=1);
|
|||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector;
|
||||
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
|
||||
use Rector\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector;
|
||||
use Rector\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector;
|
||||
use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
|
|
@ -4480,7 +4480,7 @@ Remove the "object" param and return type, add a `@param` and `@return` tags ins
|
|||
|
||||
Change param type to match the lowest type in whole family tree
|
||||
|
||||
- class: [`Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector`](../rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php)
|
||||
- class: [`Rector\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector`](../rules/DowngradePhp72/Rector/Class_/DowngradeParameterTypeWideningRector.php)
|
||||
|
||||
```diff
|
||||
interface A
|
||||
|
|
|
@ -494,7 +494,9 @@ final class NodeRepository
|
|||
|
||||
public function findClassLike(string $classLikeName): ?ClassLike
|
||||
{
|
||||
return $this->findClass($classLikeName) ?? $this->findInterface($classLikeName);
|
||||
return $this->findClass($classLikeName) ?? $this->findInterface($classLikeName) ?? $this->findTrait(
|
||||
$classLikeName
|
||||
);
|
||||
}
|
||||
|
||||
private function collectArray(Array_ $array): void
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
use Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Source\SomeContainerInterface;
|
||||
|
||||
class KeepInterfaceDonwgraded implements SomeContainerInterface
|
||||
{
|
||||
public function get($id)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Source;
|
||||
|
||||
final class AnyService
|
||||
{
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
interface A
|
||||
{
|
||||
|
@ -25,7 +25,7 @@ class C implements A
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
interface A
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DowngradePhp72\Tests\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\DowngradePhp72\Tests\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
trait StreamDecoratorTrait
|
||||
{
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
final class SomeContainer implements ContainerInterface
|
||||
{
|
||||
use ServiceLocatorTrait;
|
||||
}
|
||||
|
||||
trait ServiceLocatorTrait
|
||||
{
|
||||
public function get(string $name)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
interface ContainerInterface
|
||||
{
|
||||
public function get($name);
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
final class SomeContainer implements ContainerInterface
|
||||
{
|
||||
use ServiceLocatorTrait;
|
||||
}
|
||||
|
||||
trait ServiceLocatorTrait
|
||||
{
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
interface ContainerInterface
|
||||
{
|
||||
public function get($name);
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
interface SomeInterface
|
||||
{
|
||||
|
@ -23,7 +23,7 @@ class SomeChildClass extends AbstractSomeAncestorClass
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
interface SomeInterface
|
||||
{
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
use Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Source\SomeContainerInterface;
|
||||
|
||||
class KeepInterfaceDonwgraded implements SomeContainerInterface
|
||||
{
|
||||
public function get($id)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
interface ParentWithString
|
||||
{
|
||||
|
@ -18,7 +18,7 @@ class ChildString implements ParentWithString
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
interface ParentWithString
|
||||
{
|
|
@ -1,8 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
use Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Source\AnyService;
|
||||
use Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Source\AnyService;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
final class SkipCommand extends Command
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
interface SkipNothingHappens
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
use PhpParser\Node\Param;
|
||||
use PHPStan\Type\Type;
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
class AncestorClass
|
||||
{
|
||||
|
@ -22,7 +22,7 @@ class ChildClass extends AncestorClass
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
class AncestorClass
|
||||
{
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Source;
|
||||
|
||||
final class AnyService
|
||||
{
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Source;
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Source;
|
||||
|
||||
interface SomeContainerInterface
|
||||
{
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
|
||||
use Rector\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp72\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\NodeCollector\NodeCollector\NodeRepository;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
final class ClassLikeWithTraitsClassMethodResolver
|
||||
{
|
||||
/**
|
||||
* @var NodeRepository
|
||||
*/
|
||||
private $nodeRepository;
|
||||
|
||||
public function __construct(NodeRepository $nodeRepository)
|
||||
{
|
||||
$this->nodeRepository = $nodeRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ClassMethod[]
|
||||
*/
|
||||
public function resolve(Class_ $class): array
|
||||
{
|
||||
$classMethods = $class->getMethods();
|
||||
|
||||
$scope = $class->getAttribute(AttributeKey::SCOPE);
|
||||
if (! $scope instanceof Scope) {
|
||||
return $classMethods;
|
||||
}
|
||||
|
||||
$classReflection = $scope->getClassReflection();
|
||||
if (! $classReflection instanceof ClassReflection) {
|
||||
return $classMethods;
|
||||
}
|
||||
|
||||
foreach ($classReflection->getTraits() as $traitClassReflection) {
|
||||
$trait = $this->nodeRepository->findTrait($traitClassReflection->getName());
|
||||
if ($trait instanceof Trait_) {
|
||||
$classMethods = array_merge($classMethods, $trait->getMethods());
|
||||
}
|
||||
}
|
||||
|
||||
return $classMethods;
|
||||
}
|
||||
}
|
|
@ -2,10 +2,11 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp72\Rector\ClassMethod;
|
||||
namespace Rector\DowngradePhp72\Rector\Class_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Analyser\Scope;
|
||||
|
@ -13,6 +14,7 @@ use PHPStan\Reflection\ClassReflection;
|
|||
use PHPStan\Type\Type;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DowngradePhp72\NodeAnalyzer\ClassLikeWithTraitsClassMethodResolver;
|
||||
use Rector\DowngradePhp72\NodeAnalyzer\NativeTypeClassTreeResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
|
||||
|
@ -23,7 +25,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
* @changelog https://www.php.net/manual/en/migration72.new-features.php#migration72.new-features.param-type-widening
|
||||
* @see https://3v4l.org/fOgSE
|
||||
*
|
||||
* @see \Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\DowngradeParameterTypeWideningRectorTest
|
||||
* @see \Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\DowngradeParameterTypeWideningRectorTest
|
||||
*/
|
||||
final class DowngradeParameterTypeWideningRector extends AbstractRector
|
||||
{
|
||||
|
@ -42,14 +44,21 @@ final class DowngradeParameterTypeWideningRector extends AbstractRector
|
|||
*/
|
||||
private $typeFactory;
|
||||
|
||||
/**
|
||||
* @var ClassLikeWithTraitsClassMethodResolver
|
||||
*/
|
||||
private $classLikeWithTraitsClassMethodResolver;
|
||||
|
||||
public function __construct(
|
||||
PhpDocTypeChanger $phpDocTypeChanger,
|
||||
NativeTypeClassTreeResolver $nativeTypeClassTreeResolver,
|
||||
TypeFactory $typeFactory
|
||||
TypeFactory $typeFactory,
|
||||
ClassLikeWithTraitsClassMethodResolver $classLikeWithTraitsClassMethodResolver
|
||||
) {
|
||||
$this->phpDocTypeChanger = $phpDocTypeChanger;
|
||||
$this->nativeTypeClassTreeResolver = $nativeTypeClassTreeResolver;
|
||||
$this->typeFactory = $typeFactory;
|
||||
$this->classLikeWithTraitsClassMethodResolver = $classLikeWithTraitsClassMethodResolver;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -81,7 +90,7 @@ class C implements A
|
|||
public function test(array $input){}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -90,41 +99,30 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
return [Class_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->isMagic()) {
|
||||
return null;
|
||||
$classMethods = $this->classLikeWithTraitsClassMethodResolver->resolve($node);
|
||||
|
||||
$scope = $node->getAttribute(AttributeKey::SCOPE);
|
||||
foreach ($classMethods as $classMethod) {
|
||||
$this->refactorClassMethod($classMethod, $scope);
|
||||
}
|
||||
|
||||
if ($node->params === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (array_keys($node->params) as $position) {
|
||||
$this->refactorParamForSelfAndSiblings($node, (int) $position);
|
||||
}
|
||||
|
||||
return null;
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* The topmost class is the source of truth, so we go only down to avoid up/down collission
|
||||
*/
|
||||
private function refactorParamForSelfAndSiblings(ClassMethod $classMethod, int $position): void
|
||||
private function refactorParamForSelfAndSiblings(ClassMethod $classMethod, int $position, Scope $classScope): void
|
||||
{
|
||||
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
|
||||
if (! $scope instanceof Scope) {
|
||||
// possibly trait
|
||||
return;
|
||||
}
|
||||
|
||||
$classReflection = $scope->getClassReflection();
|
||||
$classReflection = $classScope->getClassReflection();
|
||||
if (! $classReflection instanceof ClassReflection) {
|
||||
return;
|
||||
}
|
||||
|
@ -294,4 +292,19 @@ CODE_SAMPLE
|
|||
$this->removeParamTypeFromMethodForChildren($className, $methodName, $position);
|
||||
}
|
||||
}
|
||||
|
||||
private function refactorClassMethod(ClassMethod $classMethod, Scope $classScope): void
|
||||
{
|
||||
if ($classMethod->isMagic()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($classMethod->params === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (array_keys($classMethod->params) as $position) {
|
||||
$this->refactorParamForSelfAndSiblings($classMethod, (int) $position, $classScope);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user