Skip to content

Commit

Permalink
Add minimum PHP versions for PHP 8.1 rules (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
DieterHolvoet committed Sep 2, 2021
1 parent 2ea6e90 commit e8c8f99
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
{
Expand Down Expand Up @@ -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;
}
}
9 changes: 8 additions & 1 deletion rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -70,4 +72,9 @@ public function refactor(Node $node): ?Node

return $this->enumFactory->createFromClass($node);
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ENUM;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
{
Expand Down Expand Up @@ -74,4 +76,9 @@ public function refactor(Node $node): ?Node

return $this->nodeFactory->createClassConstFetch($className, $enumCaseName);
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ENUM;
}
}
9 changes: 8 additions & 1 deletion rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
18 changes: 18 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e8c8f99

Please sign in to comment.