Skip to content

Commit

Permalink
Utilize MinPhpVersionInterface in more rules (#3752)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed May 7, 2023
1 parent 34eddac commit e97b7f5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
Expand All @@ -21,20 +20,20 @@
use Rector\TypeDeclaration\Helper\PhpDocNullableTypeHelper;
use Rector\TypeDeclaration\NodeAnalyzer\ParamAnalyzer;
use Rector\TypeDeclaration\PhpDocParser\ParamPhpDocNodeFactory;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamAnnotationIncorrectNullableRector\ParamAnnotationIncorrectNullableRectorTest
*/
final class ParamAnnotationIncorrectNullableRector extends AbstractRector
final class ParamAnnotationIncorrectNullableRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly TypeComparator $typeComparator,
private readonly PhpDocNullableTypeHelper $phpDocNullableTypeHelper,
private readonly PhpDocNestedAnnotationGuard $phpDocNestedAnnotationGuard,
private readonly ParamPhpDocNodeFactory $paramPhpDocNodeFactory,
private readonly PhpVersionProvider $phpVersionProvider,
private readonly ParamAnalyzer $paramAnalyzer
) {
}
Expand Down Expand Up @@ -88,6 +87,11 @@ public function getNodeTypes(): array
return [ClassMethod::class, Function_::class];
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}

/**
* @param ClassMethod|Function_ $node
*/
Expand All @@ -97,10 +101,6 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
return null;
}

if (! $this->phpDocNestedAnnotationGuard->isPhpDocCommentCorrectlyParsed($node)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
use Rector\TypeDeclaration\TypeAnalyzer\ReturnStrictTypeAnalyzer;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\ReturnTypeFromStrictTypedCallRectorTest
*/
final class ReturnTypeFromStrictTypedCallRector extends AbstractRector
final class ReturnTypeFromStrictTypedCallRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly TypeNodeUnwrapper $typeNodeUnwrapper,
Expand Down Expand Up @@ -90,6 +91,11 @@ public function getNodeTypes(): array
return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::SCALAR_TYPES;
}

/**
* @param ClassMethod|Function_|Closure|ArrowFunction $node
*/
Expand Down Expand Up @@ -193,10 +199,6 @@ private function processSingleUnionType(

private function isSkipped(ClassMethod | Function_ | Closure | ArrowFunction $node): bool
{
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return true;
}

if ($node instanceof ArrowFunction) {
return $node->returnType !== null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PhpDocParser\PhpDocInfoAnalyzer;
use Rector\TypeDeclaration\Guard\PhpDocNestedAnnotationGuard;
use Rector\TypeDeclaration\Helper\PhpDocNullableTypeHelper;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNullableRector\VarAnnotationIncorrectNullableRectorTest
*/
final class VarAnnotationIncorrectNullableRector extends AbstractRector
final class VarAnnotationIncorrectNullableRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly PhpDocNullableTypeHelper $phpDocNullableTypeHelper,
private readonly PhpDocNestedAnnotationGuard $phpDocNestedAnnotationGuard,
private readonly PhpVersionProvider $phpVersionProvider,
private readonly PhpDocInfoAnalyzer $phpDocInfoAnalyzer,
) {
}
Expand Down Expand Up @@ -71,6 +70,11 @@ public function getNodeTypes(): array
return [Property::class];
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}

/**
* @param Property $node
*/
Expand All @@ -80,10 +84,6 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
return null;
}

if (! $this->phpDocNestedAnnotationGuard->isPhpDocCommentCorrectlyParsed($node)) {
return null;
}
Expand Down

0 comments on commit e97b7f5

Please sign in to comment.