From 4a13da1cba371cc71dceb602956a6e39f899616e Mon Sep 17 00:00:00 2001 From: Felix Sokoliuk Date: Thu, 26 Dec 2019 20:54:00 +0100 Subject: [PATCH] add PHP version feature checks --- ...DirNameFileConstantToDirConstantRector.php | 5 ++ .../Rector/Ternary/TernaryToElvisRector.php | 5 ++ .../StringClassNameToClassConstantRector.php | 5 ++ .../src/Rector/FuncCall/PowToExpRector.php | 5 ++ .../Rector/FuncCall/MultiDirnameRector.php | 5 ++ .../Rector/FuncCall/RandomFunctionRector.php | 5 ++ .../ExceptionHandlerTypehintRector.php | 5 ++ .../src/Rector/If_/IfToSpaceshipRector.php | 5 ++ .../Rector/List_/ListSwapArrayOrderRector.php | 4 ++ .../PublicConstantVisibilityRector.php | 5 ++ .../List_/ListToArrayDestructRector.php | 5 ++ .../TryCatch/MultiExceptionCatchRector.php | 5 ++ .../FuncCall/JsonThrowOnErrorRector.php | 5 ++ .../ArraySpreadInsteadOfArrayMergeRector.php | 5 ++ .../GetCalledClassToStaticClassRector.php | 5 ++ .../ClassConstantToSelfClassRector.php | 5 ++ src/ValueObject/PhpVersionFeature.php | 60 +++++++++++++++++++ 17 files changed, 139 insertions(+) diff --git a/packages/Php53/src/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php b/packages/Php53/src/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php index b6269a6c7f72..bf7e8dc4b267 100644 --- a/packages/Php53/src/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php +++ b/packages/Php53/src/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php @@ -11,6 +11,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see \Rector\Php53\Tests\Rector\FuncCall\DirNameFileConstantToDirConstantRector\DirNameFileConstantToDirConstantRectorTest @@ -57,6 +58,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::DIR_CONSTANT)) { + return null; + } + if (! $this->isName($node, 'dirname')) { return null; } diff --git a/packages/Php53/src/Rector/Ternary/TernaryToElvisRector.php b/packages/Php53/src/Rector/Ternary/TernaryToElvisRector.php index 3ed2e82e4670..52e62a8c69d6 100644 --- a/packages/Php53/src/Rector/Ternary/TernaryToElvisRector.php +++ b/packages/Php53/src/Rector/Ternary/TernaryToElvisRector.php @@ -10,6 +10,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary @@ -52,6 +53,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::ELVIS_OPERATOR)) { + return null; + } + if (! $this->areNodesEqual($node->cond, $node->if)) { return null; } diff --git a/packages/Php55/src/Rector/String_/StringClassNameToClassConstantRector.php b/packages/Php55/src/Rector/String_/StringClassNameToClassConstantRector.php index 1a0d0acffdd5..492e174344bf 100644 --- a/packages/Php55/src/Rector/String_/StringClassNameToClassConstantRector.php +++ b/packages/Php55/src/Rector/String_/StringClassNameToClassConstantRector.php @@ -13,6 +13,7 @@ use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; use Rector\Util\RectorStrings; +use Rector\ValueObject\PhpVersionFeature; use ReflectionClass; /** @@ -94,6 +95,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::CLASSNAME_CONSTANT)) { + return null; + } + $classLikeName = $node->value; // remove leading slash diff --git a/packages/Php56/src/Rector/FuncCall/PowToExpRector.php b/packages/Php56/src/Rector/FuncCall/PowToExpRector.php index 637ef9dcf618..a365a48d5d0a 100644 --- a/packages/Php56/src/Rector/FuncCall/PowToExpRector.php +++ b/packages/Php56/src/Rector/FuncCall/PowToExpRector.php @@ -10,6 +10,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see \Rector\Php56\Tests\Rector\FuncCall\PowToExpRector\PowToExpRectorTest @@ -37,6 +38,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::EXP_OPERATOR)) { + return null; + } + if (! $this->isName($node, 'pow')) { return null; } diff --git a/packages/Php70/src/Rector/FuncCall/MultiDirnameRector.php b/packages/Php70/src/Rector/FuncCall/MultiDirnameRector.php index e3a9b3553b75..cac8af658f7b 100644 --- a/packages/Php70/src/Rector/FuncCall/MultiDirnameRector.php +++ b/packages/Php70/src/Rector/FuncCall/MultiDirnameRector.php @@ -11,6 +11,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see \Rector\Php70\Tests\Rector\FuncCall\MultiDirnameRector\MultiDirnameRectorTest @@ -43,6 +44,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::DIRNAME_LEVELS)) { + return null; + } + $this->nestingLevel = 0; if (! $this->isName($node, 'dirname')) { diff --git a/packages/Php70/src/Rector/FuncCall/RandomFunctionRector.php b/packages/Php70/src/Rector/FuncCall/RandomFunctionRector.php index 123be0c92315..d16c0c4eb54a 100644 --- a/packages/Php70/src/Rector/FuncCall/RandomFunctionRector.php +++ b/packages/Php70/src/Rector/FuncCall/RandomFunctionRector.php @@ -12,6 +12,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see \Rector\Php70\Tests\Rector\FuncCall\RandomFunctionRector\RandomFunctionRectorTest @@ -49,6 +50,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::CSPRNG_FUNCTIONS)) { + return null; + } + foreach ($this->oldToNewFunctionNames as $oldFunctionName => $newFunctionName) { if ($this->isName($node, $oldFunctionName)) { $node->name = new Name($newFunctionName); diff --git a/packages/Php70/src/Rector/FunctionLike/ExceptionHandlerTypehintRector.php b/packages/Php70/src/Rector/FunctionLike/ExceptionHandlerTypehintRector.php index afb17c43287f..86503a5da41a 100644 --- a/packages/Php70/src/Rector/FunctionLike/ExceptionHandlerTypehintRector.php +++ b/packages/Php70/src/Rector/FunctionLike/ExceptionHandlerTypehintRector.php @@ -13,6 +13,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see https://wiki.php.net/rfc/typed_properties_v2#proposal @@ -54,6 +55,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::THROWABLE_TYPE)) { + return null; + } + // exception handle has 1 param exactly if (count($node->params) !== 1) { return null; diff --git a/packages/Php70/src/Rector/If_/IfToSpaceshipRector.php b/packages/Php70/src/Rector/If_/IfToSpaceshipRector.php index 92f7c9000c76..54c9c08d0ee2 100644 --- a/packages/Php70/src/Rector/If_/IfToSpaceshipRector.php +++ b/packages/Php70/src/Rector/If_/IfToSpaceshipRector.php @@ -19,6 +19,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see https://wiki.php.net/rfc/combined-comparison-operator @@ -100,6 +101,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::SPACESHIP)) { + return null; + } + $this->reset(); if ($node->cond instanceof Equal || $node->cond instanceof Identical) { diff --git a/packages/Php70/src/Rector/List_/ListSwapArrayOrderRector.php b/packages/Php70/src/Rector/List_/ListSwapArrayOrderRector.php index 66afcfd3d8e3..44d07a59cf48 100644 --- a/packages/Php70/src/Rector/List_/ListSwapArrayOrderRector.php +++ b/packages/Php70/src/Rector/List_/ListSwapArrayOrderRector.php @@ -40,6 +40,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion('7.0')) { + return null; + } + if (! $node->var instanceof List_) { return null; } diff --git a/packages/Php71/src/Rector/ClassConst/PublicConstantVisibilityRector.php b/packages/Php71/src/Rector/ClassConst/PublicConstantVisibilityRector.php index e1f51ceff4a9..151cf435d7b6 100644 --- a/packages/Php71/src/Rector/ClassConst/PublicConstantVisibilityRector.php +++ b/packages/Php71/src/Rector/ClassConst/PublicConstantVisibilityRector.php @@ -9,6 +9,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see https://wiki.php.net/rfc/class_const_visibility @@ -54,6 +55,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::CONSTANT_VISIBILITY)) { + return null; + } + // already non-public if (! $node->isPublic()) { return null; diff --git a/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php b/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php index e39d1a3ef672..a2d17179288a 100644 --- a/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php +++ b/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php @@ -13,6 +13,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see https://wiki.php.net/rfc/short_list_syntax @@ -70,6 +71,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_DESTRUCT)) { + return null; + } + $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); if ($parentNode instanceof Assign) { diff --git a/packages/Php71/src/Rector/TryCatch/MultiExceptionCatchRector.php b/packages/Php71/src/Rector/TryCatch/MultiExceptionCatchRector.php index af3ca7d517f9..303818dc7381 100644 --- a/packages/Php71/src/Rector/TryCatch/MultiExceptionCatchRector.php +++ b/packages/Php71/src/Rector/TryCatch/MultiExceptionCatchRector.php @@ -10,6 +10,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see https://wiki.php.net/rfc/multiple-catch @@ -58,6 +59,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::MULTI_EXCEPTION_CATCH)) { + return null; + } + if (count($node->catches) < 2) { return null; } diff --git a/packages/Php73/src/Rector/FuncCall/JsonThrowOnErrorRector.php b/packages/Php73/src/Rector/FuncCall/JsonThrowOnErrorRector.php index 67aae7f59cd8..9af8049f080e 100644 --- a/packages/Php73/src/Rector/FuncCall/JsonThrowOnErrorRector.php +++ b/packages/Php73/src/Rector/FuncCall/JsonThrowOnErrorRector.php @@ -13,6 +13,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see http://wiki.php.net/rfc/json_throw_on_error @@ -54,6 +55,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::JSON_EXCEPTION)) { + return null; + } + if ($this->isName($node, 'json_encode')) { return $this->processJsonEncode($node); } diff --git a/packages/Php74/src/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php b/packages/Php74/src/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php index 625e5713ba5d..0c3a611c2232 100644 --- a/packages/Php74/src/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php +++ b/packages/Php74/src/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php @@ -16,6 +16,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see https://wiki.php.net/rfc/spread_operator_for_array @@ -76,6 +77,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_SPREAD)) { + return null; + } + if ($this->isName($node, 'array_merge')) { return $this->refactorArray($node); } diff --git a/packages/Php74/src/Rector/FuncCall/GetCalledClassToStaticClassRector.php b/packages/Php74/src/Rector/FuncCall/GetCalledClassToStaticClassRector.php index 3f3dc19b5cef..50013e3f4402 100644 --- a/packages/Php74/src/Rector/FuncCall/GetCalledClassToStaticClassRector.php +++ b/packages/Php74/src/Rector/FuncCall/GetCalledClassToStaticClassRector.php @@ -9,6 +9,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see https://wiki.php.net/rfc/deprecations_php_7_4 (not confirmed yet) @@ -57,6 +58,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::CLASSNAME_CONSTANT)) { + return null; + } + if (! $this->isName($node, 'get_called_class')) { return null; } diff --git a/packages/Php74/src/Rector/MagicConstClass/ClassConstantToSelfClassRector.php b/packages/Php74/src/Rector/MagicConstClass/ClassConstantToSelfClassRector.php index 23d053c01df7..8511f7851eb7 100644 --- a/packages/Php74/src/Rector/MagicConstClass/ClassConstantToSelfClassRector.php +++ b/packages/Php74/src/Rector/MagicConstClass/ClassConstantToSelfClassRector.php @@ -11,6 +11,7 @@ use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; +use Rector\ValueObject\PhpVersionFeature; /** * @see https://wiki.php.net/rfc/deprecations_php_7_4 (not confirmed yet) @@ -59,6 +60,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if (! $this->isAtLeastPhpVersion(PhpVersionFeature::CLASSNAME_CONSTANT)) { + return null; + } + return new ClassConstFetch(new Name('self'), 'class'); } } diff --git a/src/ValueObject/PhpVersionFeature.php b/src/ValueObject/PhpVersionFeature.php index a204dd7aafa2..7cf881a01ba7 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -6,6 +6,26 @@ final class PhpVersionFeature { + /** + * @var string + */ + public const DIR_CONSTANT = '5.3'; + + /** + * @var string + */ + public const ELVIS_OPERATOR = '5.3'; + + /** + * @var string + */ + public const CLASSNAME_CONSTANT = '5.5'; + + /** + * @var string + */ + public const EXP_OPERATOR = '5.6'; + /** * @var string */ @@ -21,6 +41,21 @@ final class PhpVersionFeature */ public const SPACESHIP = '7.0'; + /** + * @var string + */ + public const DIRNAME_LEVELS = '7.0'; + + /** + * @var string + */ + public const CSPRNG_FUNCTIONS = '7.0'; + + /** + * @var string + */ + public const THROWABLE_TYPE = '7.0'; + /** * @var string */ @@ -31,6 +66,21 @@ final class PhpVersionFeature */ public const VOID_TYPE = '7.1'; + /** + * @var string + */ + public const CONSTANT_VISIBILITY = '7.1'; + + /** + * @var string + */ + public const ARRAY_DESTRUCT = '7.1'; + + /** + * @var string + */ + public const MULTI_EXCEPTION_CATCH = '7.1'; + /** * @var string */ @@ -46,6 +96,11 @@ final class PhpVersionFeature */ public const ARRAY_KEY_FIRST_LAST = '7.3'; + /** + * @var string + */ + public const JSON_EXCEPTION = '7.3'; + /** * @var string */ @@ -77,6 +132,11 @@ final class PhpVersionFeature */ public const COVARIANT_RETURN = '7.4'; + /** + * @var string + */ + public const ARRAY_SPREAD = '7.4'; + /** * @var string */