mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit 2ad03db37f187d715c3a2f189dc181063126e596
2ad03db37f
[Php83] Adds rule for adding Override attribute (#5170)
This commit is contained in:
parent
23db14797e
commit
cdc1ef461c
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202311;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
use Rector\Set\ValueObject\SetList;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->sets([SetList::PHP_83, LevelSetList::UP_TO_PHP_82]);
|
||||
// parameter must be defined after import, to override imported param version
|
||||
$rectorConfig->phpVersion(PhpVersion::PHP_83);
|
||||
};
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202311;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rules([AddOverrideAttributeToOverriddenMethodsRector::class]);
|
||||
};
|
|
@ -1,4 +1,4 @@
|
|||
# 355 Rules Overview
|
||||
# 356 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -44,6 +44,8 @@
|
|||
|
||||
- [Php82](#php82) (4)
|
||||
|
||||
- [Php83](#php83) (1)
|
||||
|
||||
- [Privatization](#privatization) (4)
|
||||
|
||||
- [Removing](#removing) (5)
|
||||
|
@ -5314,6 +5316,33 @@ Change deprecated utf8_decode and utf8_encode to mb_convert_encoding
|
|||
|
||||
<br>
|
||||
|
||||
## Php83
|
||||
|
||||
### AddOverrideAttributeToOverriddenMethodsRector
|
||||
|
||||
Add override attribute to overridden methods
|
||||
|
||||
- class: [`Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector`](../rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php)
|
||||
|
||||
```diff
|
||||
class ParentClass
|
||||
{
|
||||
public function foo()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class ChildClass extends ParentClass
|
||||
{
|
||||
+ #[\Override]
|
||||
public function foo()
|
||||
{
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## Privatization
|
||||
|
||||
### FinalizeClassesWithoutChildrenRector
|
||||
|
|
|
@ -9,6 +9,10 @@ use Rector\Set\Contract\SetListInterface;
|
|||
*/
|
||||
final class LevelSetList implements SetListInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const UP_TO_PHP_83 = __DIR__ . '/../../../config/set/level/up-to-php83.php';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
|
|
@ -85,6 +85,10 @@ final class SetList implements SetListInterface
|
|||
* @var string
|
||||
*/
|
||||
public const PHP_82 = __DIR__ . '/../../../config/set/php82.php';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PHP_83 = __DIR__ . '/../../../config/set/php83.php';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,138 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php83\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Attribute;
|
||||
use PhpParser\Node\AttributeGroup;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see https://wiki.php.net/rfc/marking_overriden_methods
|
||||
* @see \Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\AddOverrideAttributeToOverriddenMethodsRectorTest
|
||||
*/
|
||||
class AddOverrideAttributeToOverriddenMethodsRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \PHPStan\Reflection\ReflectionProvider
|
||||
*/
|
||||
private $reflectionProvider;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
|
||||
*/
|
||||
private $classAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer
|
||||
*/
|
||||
private $phpAttributeAnalyzer;
|
||||
public function __construct(ReflectionProvider $reflectionProvider, ClassAnalyzer $classAnalyzer, PhpAttributeAnalyzer $phpAttributeAnalyzer)
|
||||
{
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
$this->classAnalyzer = $classAnalyzer;
|
||||
$this->phpAttributeAnalyzer = $phpAttributeAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add override attribute to overridden methods', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class ParentClass
|
||||
{
|
||||
public function foo()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class ChildClass extends ParentClass
|
||||
{
|
||||
public function foo()
|
||||
{
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class ParentClass
|
||||
{
|
||||
public function foo()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class ChildClass extends ParentClass
|
||||
{
|
||||
#[\Override]
|
||||
public function foo()
|
||||
{
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Class_::class];
|
||||
}
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
// Detect if class extends a parent class
|
||||
if ($this->shouldSkipClass($node)) {
|
||||
return null;
|
||||
}
|
||||
// Fetch the parent class reflection
|
||||
$parentClassReflection = $this->reflectionProvider->getClass((string) $node->extends);
|
||||
$hasChanged = \false;
|
||||
foreach ($node->getMethods() as $method) {
|
||||
if ($method->name->toString() === '__construct') {
|
||||
continue;
|
||||
}
|
||||
// Private methods should be ignored
|
||||
if ($parentClassReflection->hasNativeMethod($method->name->toString())) {
|
||||
// ignore if it is a private method on the parent
|
||||
$parentMethod = $parentClassReflection->getNativeMethod($method->name->toString());
|
||||
if ($parentMethod->isPrivate()) {
|
||||
continue;
|
||||
}
|
||||
// ignore if it already uses the attribute
|
||||
if ($this->phpAttributeAnalyzer->hasPhpAttribute($method, 'Override')) {
|
||||
continue;
|
||||
}
|
||||
$method->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified('Override'))]);
|
||||
$hasChanged = \true;
|
||||
}
|
||||
}
|
||||
if (!$hasChanged) {
|
||||
return null;
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::OVERRIDE_ATTRIBUTE;
|
||||
}
|
||||
private function shouldSkipClass(Class_ $class) : bool
|
||||
{
|
||||
if ($this->classAnalyzer->isAnonymousClass($class)) {
|
||||
return \true;
|
||||
}
|
||||
if (!$class->extends instanceof FullyQualified) {
|
||||
return \true;
|
||||
}
|
||||
return !$this->reflectionProvider->hasClass($class->extends->toString());
|
||||
}
|
||||
}
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'cc97becc06dcf1b69c5de01aebf09b498c0d9006';
|
||||
public const PACKAGE_VERSION = '2ad03db37f187d715c3a2f189dc181063126e596';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-11-03 13:00:11';
|
||||
public const RELEASE_DATE = '2023-11-03 13:11:18';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -522,4 +522,9 @@ final class PhpVersionFeature
|
|||
* @var int
|
||||
*/
|
||||
public const SENSITIVE_PARAMETER_ATTRIBUTE = \Rector\Core\ValueObject\PhpVersion::PHP_82;
|
||||
/**
|
||||
* @see https://wiki.php.net/rfc/marking_overriden_methods
|
||||
* @var int
|
||||
*/
|
||||
public const OVERRIDE_ATTRIBUTE = \Rector\Core\ValueObject\PhpVersion::PHP_83;
|
||||
}
|
||||
|
|
|
@ -1902,6 +1902,7 @@ return array(
|
|||
'Rector\\Php82\\Rector\\FuncCall\\Utf8DecodeEncodeToMbConvertEncodingRector' => $baseDir . '/rules/Php82/Rector/FuncCall/Utf8DecodeEncodeToMbConvertEncodingRector.php',
|
||||
'Rector\\Php82\\Rector\\New_\\FilesystemIteratorSkipDotsRector' => $baseDir . '/rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php',
|
||||
'Rector\\Php82\\Rector\\Param\\AddSensitiveParameterAttributeRector' => $baseDir . '/rules/Php82/Rector/Param/AddSensitiveParameterAttributeRector.php',
|
||||
'Rector\\Php83\\Rector\\ClassMethod\\AddOverrideAttributeToOverriddenMethodsRector' => $baseDir . '/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper' => $baseDir . '/packages/PhpAttribute/AnnotationToAttributeMapper.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper\\ArrayAnnotationToAttributeMapper' => $baseDir . '/packages/PhpAttribute/AnnotationToAttributeMapper/ArrayAnnotationToAttributeMapper.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper\\ArrayItemNodeAnnotationToAttributeMapper' => $baseDir . '/packages/PhpAttribute/AnnotationToAttributeMapper/ArrayItemNodeAnnotationToAttributeMapper.php',
|
||||
|
|
|
@ -2120,6 +2120,7 @@ class ComposerStaticInit18ad0e678efbbb500e116f7c54cccdd4
|
|||
'Rector\\Php82\\Rector\\FuncCall\\Utf8DecodeEncodeToMbConvertEncodingRector' => __DIR__ . '/../..' . '/rules/Php82/Rector/FuncCall/Utf8DecodeEncodeToMbConvertEncodingRector.php',
|
||||
'Rector\\Php82\\Rector\\New_\\FilesystemIteratorSkipDotsRector' => __DIR__ . '/../..' . '/rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php',
|
||||
'Rector\\Php82\\Rector\\Param\\AddSensitiveParameterAttributeRector' => __DIR__ . '/../..' . '/rules/Php82/Rector/Param/AddSensitiveParameterAttributeRector.php',
|
||||
'Rector\\Php83\\Rector\\ClassMethod\\AddOverrideAttributeToOverriddenMethodsRector' => __DIR__ . '/../..' . '/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper' => __DIR__ . '/../..' . '/packages/PhpAttribute/AnnotationToAttributeMapper.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper\\ArrayAnnotationToAttributeMapper' => __DIR__ . '/../..' . '/packages/PhpAttribute/AnnotationToAttributeMapper/ArrayAnnotationToAttributeMapper.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper\\ArrayItemNodeAnnotationToAttributeMapper' => __DIR__ . '/../..' . '/packages/PhpAttribute/AnnotationToAttributeMapper/ArrayItemNodeAnnotationToAttributeMapper.php',
|
||||
|
|
Loading…
Reference in New Issue