From e8c8f9996d555a26d6e99921001944eb7d1bd90d Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Thu, 2 Sep 2021 17:11:07 +0200 Subject: [PATCH] Add minimum PHP versions for PHP 8.1 rules (#814) --- .../FinalizePublicClassConstantRector.php | 9 ++++++++- .../Rector/Class_/MyCLabsClassToEnumRector.php | 9 ++++++++- .../MyCLabsMethodCallToEnumConstRector.php | 9 ++++++++- .../Rector/Property/ReadOnlyPropertyRector.php | 9 ++++++++- src/ValueObject/PhpVersionFeature.php | 18 ++++++++++++++++++ 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php b/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php index 3d046722d17..ee29b375ce2 100644 --- a/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php +++ b/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php @@ -7,6 +7,8 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassConst; use Rector\Core\Rector\AbstractRector; +use Rector\Core\ValueObject\PhpVersionFeature; +use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -15,7 +17,7 @@ * * @see \Rector\Tests\Php81\Rector\ClassConst\FinalizePublicClassConstantRector\FinalizePublicClassConstantRectorTest */ -final class FinalizePublicClassConstantRector extends AbstractRector +final class FinalizePublicClassConstantRector extends AbstractRector implements MinPhpVersionInterface { public function getRuleDefinition(): RuleDefinition { @@ -67,4 +69,9 @@ public function refactor(Node $node): ?Node $this->visibilityManipulator->makeFinal($node); return $node; } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::FINAL_CLASS_CONSTANTS; + } } diff --git a/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php b/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php index cdaf4e675c5..ae0fc3ebd76 100644 --- a/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php +++ b/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php @@ -8,7 +8,9 @@ use PhpParser\Node\Stmt\Class_; use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; +use Rector\Core\ValueObject\PhpVersionFeature; use Rector\Php81\NodeFactory\EnumFactory; +use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -18,7 +20,7 @@ * * @see \Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\MyCLabsClassToEnumRectorTest */ -final class MyCLabsClassToEnumRector extends AbstractRector +final class MyCLabsClassToEnumRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private EnumFactory $enumFactory @@ -70,4 +72,9 @@ public function refactor(Node $node): ?Node return $this->enumFactory->createFromClass($node); } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::ENUM; + } } diff --git a/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php b/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php index 4412c097dc4..5493a3ccf2c 100644 --- a/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php +++ b/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php @@ -9,6 +9,8 @@ use PhpParser\Node\Expr\StaticCall; use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; +use Rector\Core\ValueObject\PhpVersionFeature; +use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -18,7 +20,7 @@ * * @see \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\MyCLabsMethodCallToEnumConstRectorTest */ -final class MyCLabsMethodCallToEnumConstRector extends AbstractRector +final class MyCLabsMethodCallToEnumConstRector extends AbstractRector implements MinPhpVersionInterface { public function getRuleDefinition(): RuleDefinition { @@ -74,4 +76,9 @@ public function refactor(Node $node): ?Node return $this->nodeFactory->createClassConstFetch($className, $enumCaseName); } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::ENUM; + } } diff --git a/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php b/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php index 8b97a4ea17e..e9c314cf61c 100644 --- a/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php +++ b/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php @@ -10,6 +10,8 @@ use PhpParser\Node\Stmt\Property; use Rector\Core\NodeManipulator\PropertyManipulator; use Rector\Core\Rector\AbstractRector; +use Rector\Core\ValueObject\PhpVersionFeature; +use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -18,7 +20,7 @@ * * @see \Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\ReadOnlyPropertyRectorTest */ -final class ReadOnlyPropertyRector extends AbstractRector +final class ReadOnlyPropertyRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private PropertyManipulator $propertyManipulator @@ -111,4 +113,9 @@ private function refactorParam(Param $param): Param | null $this->visibilityManipulator->makeReadonly($param); return $param; } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::READONLY_PROPERTY; + } } diff --git a/src/ValueObject/PhpVersionFeature.php b/src/ValueObject/PhpVersionFeature.php index 3c34538a460..762cb997e9c 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -206,4 +206,22 @@ final class PhpVersionFeature * @var int */ public const VARIADIC_PARAM = PhpVersion::PHP_56; + + /** + * @see https://wiki.php.net/rfc/readonly_and_immutable_properties + * @var int + */ + public const READONLY_PROPERTY = PhpVersion::PHP_81; + + /** + * @see https://wiki.php.net/rfc/final_class_const + * @var int + */ + public const FINAL_CLASS_CONSTANTS = PhpVersion::PHP_81; + + /** + * @see https://wiki.php.net/rfc/enumerations + * @var int + */ + public const ENUM = PhpVersion::PHP_81; }