Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/Php53/src/Rector/Ternary/TernaryToElvisRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use Rector\Util\RectorStrings;
use Rector\ValueObject\PhpVersionFeature;
use ReflectionClass;

/**
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions packages/Php56/src/Rector/FuncCall/PowToExpRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/Php70/src/Rector/FuncCall/MultiDirnameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')) {
Expand Down
5 changes: 5 additions & 0 deletions packages/Php70/src/Rector/FuncCall/RandomFunctionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions packages/Php70/src/Rector/If_/IfToSpaceshipRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions packages/Php70/src/Rector/List_/ListSwapArrayOrderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions packages/Php71/src/Rector/List_/ListToArrayDestructRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/Php73/src/Rector/FuncCall/JsonThrowOnErrorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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');
}
}
60 changes: 60 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -77,6 +132,11 @@ final class PhpVersionFeature
*/
public const COVARIANT_RETURN = '7.4';

/**
* @var string
*/
public const ARRAY_SPREAD = '7.4';

/**
* @var string
*/
Expand Down