Updated Rector to commit 394fa70c963bb059acd6aae4b998173bdfc6c80c

394fa70c96 [TypeDeclaration] Add FalseReturnClassMethodToNullableRector (#3229)
This commit is contained in:
Tomas Votruba 2022-12-20 20:40:25 +00:00
parent 856f8421c0
commit 13fa9d478f
35 changed files with 358 additions and 89 deletions

View File

@ -1,4 +1,4 @@
# 413 Rules Overview
# 414 Rules Overview
<br>
@ -64,7 +64,7 @@
- [Transform](#transform) (34)
- [TypeDeclaration](#typedeclaration) (37)
- [TypeDeclaration](#typedeclaration) (38)
- [Visibility](#visibility) (3)
@ -711,17 +711,11 @@ Flip type control to use exclusive type
- class: [`Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector`](../rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php)
```diff
class SomeClass
{
public function __construct(array $values)
{
- /** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO);
- if ($phpDocInfo === null) {
+ if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}
}
-/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO);
-if ($phpDocInfo === null) {
+if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}
```
@ -9176,6 +9170,33 @@ Add array shape exact types based on constant keys of array
<br>
### FalseReturnClassMethodToNullableRector
Change class method that returns false as invalid state, to nullable
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\FalseReturnClassMethodToNullableRector`](../rules/TypeDeclaration/Rector/ClassMethod/FalseReturnClassMethodToNullableRector.php)
```diff
class SomeClass
{
- /**
- * @return false|int
- */
- public function run(int $number)
+ public function run(int $number): ?int
{
if ($number === 10) {
- return false;
+ return null;
}
return $number;
}
}
```
<br>
### ParamAnnotationIncorrectNullableRector
Add or remove null type from `@param` phpdoc typehint based on php parameter type declaration

View File

@ -5,6 +5,7 @@ namespace Rector\PHPStanStaticTypeMapper\Contract;
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Type;
@ -26,7 +27,7 @@ interface TypeMapperInterface
/**
* @param TType $type
* @param TypeKind::* $typeKind
* @return Name|ComplexType|null
* @return Name|ComplexType|Identifier|null
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node;
}

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
@ -51,7 +52,7 @@ final class PHPStanStaticTypeMapper
}
/**
* @param TypeKind::* $typeKind
* @return \PhpParser\Node\Name|\PhpParser\Node\ComplexType|null
* @return \PhpParser\Node\Name|\PhpParser\Node\ComplexType|\PhpParser\Node\Identifier|null
*/
public function mapToPhpParserNode(Type $type, string $typeKind)
{

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
@ -48,6 +48,6 @@ final class AccessoryLiteralStringTypeMapper implements TypeMapperInterface
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
return new Identifier('string');
}
}

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
@ -48,6 +48,6 @@ final class AccessoryNonEmptyStringTypeMapper implements TypeMapperInterface
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
return new Identifier('string');
}
}

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
@ -48,6 +48,6 @@ final class AccessoryNonFalsyStringTypeMapper implements TypeMapperInterface
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
return new Identifier('string');
}
}

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
@ -48,6 +48,6 @@ final class AccessoryNumericStringTypeMapper implements TypeMapperInterface
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
return new Identifier('string');
}
}

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
@ -118,7 +118,7 @@ final class ArrayTypeMapper implements TypeMapperInterface
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
return new Name('array');
return new Identifier('array');
}
/**
* @param TypeKind::* $typeKind

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\BooleanType;
@ -57,9 +57,9 @@ final class BooleanTypeMapper implements TypeMapperInterface
return null;
}
if ($this->isFalseBooleanTypeWithUnion($type)) {
return new Name('false');
return new Identifier('false');
}
return new Name('bool');
return new Identifier('bool');
}
private function isFalseBooleanTypeWithUnion(Type $type) : bool
{

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ClassStringType;
@ -58,6 +58,6 @@ final class ClassStringTypeMapper implements TypeMapperInterface
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
return new Identifier('string');
}
}

View File

@ -5,7 +5,7 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
@ -67,7 +67,7 @@ final class GenericClassStringTypeMapper implements TypeMapperInterface
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
return new Identifier('string');
}
/**
* @return \PHPStan\Type\ObjectType|\PHPStan\Type\Type

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
@ -35,6 +35,6 @@ final class HasOffsetTypeMapper implements TypeMapperInterface
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
return new Name('array');
return new Identifier('array');
}
}

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
@ -35,6 +35,6 @@ final class HasOffsetValueTypeTypeMapper implements TypeMapperInterface
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
return new Name('array');
return new Identifier('array');
}
}

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\IntegerType;
@ -48,6 +48,6 @@ final class IntegerTypeMapper implements TypeMapperInterface
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('int');
return new Identifier('int');
}
}

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\NonEmptyArrayType;
@ -35,6 +35,6 @@ final class NonEmptyArrayTypeMapper implements TypeMapperInterface
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
return new Name('array');
return new Identifier('array');
}
}

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\StringType;
@ -45,6 +45,6 @@ final class StringTypeMapper implements TypeMapperInterface
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
return new Identifier('string');
}
}

View File

@ -4,7 +4,7 @@ declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Type;
@ -48,6 +48,6 @@ final class TypeWithClassNameTypeMapper implements TypeMapperInterface
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
return new Identifier('string');
}
}

View File

@ -13,9 +13,7 @@ use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PhpParser\NodeAbstract;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
@ -39,6 +37,7 @@ use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\BoolUnionTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeCommonTypeNarrower;
use Rector\PHPStanStaticTypeMapper\ValueObject\UnionTypeAnalysis;
use function RectorPrefix202212\Symfony\Component\String\b;
use RectorPrefix202212\Symfony\Contracts\Service\Attribute\Required;
/**
* @implements TypeMapperInterface<UnionType>
@ -132,7 +131,7 @@ final class UnionTypeMapper implements TypeMapperInterface
}
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES) && $this->isFalseBoolUnion($type)) {
// return new Bool
return new Name('bool');
return new Identifier('bool');
}
// special case for nullable
$nullabledType = $this->matchTypeForNullableUnionType($type);
@ -241,7 +240,7 @@ final class UnionTypeMapper implements TypeMapperInterface
}
/**
* @param TypeKind::* $typeKind
* @return Name|FullyQualified|PhpParserUnionType|NullableType|null
* @return Name|FullyQualified|ComplexType|Identifier|null
*/
private function matchTypeForUnionedObjectTypes(UnionType $unionType, string $typeKind) : ?Node
{
@ -250,35 +249,29 @@ final class UnionTypeMapper implements TypeMapperInterface
return $phpParserUnionType;
}
if ($phpParserUnionType !== null) {
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
// maybe all one type?
if ($this->boolUnionTypeAnalyzer->isBoolUnionType($unionType)) {
return new Name('bool');
}
return null;
}
if ($this->hasObjectAndStaticType($phpParserUnionType)) {
return null;
}
return $phpParserUnionType;
return $this->narrowBoolType($unionType, $phpParserUnionType);
}
if ($this->boolUnionTypeAnalyzer->isBoolUnionType($unionType)) {
return new Name('bool');
return new Identifier('bool');
}
$compatibleObjectTypeNode = $this->processResolveCompatibleObjectCandidates($unionType);
if ($compatibleObjectTypeNode instanceof NullableType || $compatibleObjectTypeNode instanceof FullyQualified) {
return $compatibleObjectTypeNode;
}
return $this->processResolveCompatibleStringCandidates($unionType);
$integerIdentifier = $this->narrowIntegerType($unionType);
if ($integerIdentifier instanceof Identifier) {
return $integerIdentifier;
}
return $this->narrowStringTypes($unionType);
}
private function processResolveCompatibleStringCandidates(UnionType $unionType) : ?Name
private function narrowStringTypes(UnionType $unionType) : ?Identifier
{
foreach ($unionType->getTypes() as $type) {
if (!\in_array(\get_class($type), [ClassStringType::class, GenericClassStringType::class], \true)) {
foreach ($unionType->getTypes() as $unionedType) {
if (!$unionedType->isString()->yes()) {
return null;
}
}
return new Name('string');
return new Identifier('string');
}
private function processResolveCompatibleObjectCandidates(UnionType $unionType) : ?Node
{
@ -392,4 +385,30 @@ final class UnionTypeMapper implements TypeMapperInterface
}
return \true;
}
private function narrowIntegerType(UnionType $unionType) : ?Identifier
{
foreach ($unionType->getTypes() as $unionedType) {
if (!$unionedType->isInteger()->yes()) {
return null;
}
}
return new Identifier('int');
}
/**
* @return PhpParserUnionType|null|\PhpParser\Node\Identifier
*/
private function narrowBoolType(UnionType $unionType, PhpParserUnionType $phpParserUnionType)
{
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
// maybe all one type
if ($this->boolUnionTypeAnalyzer->isBoolUnionType($unionType)) {
return new Identifier('bool');
}
return null;
}
if ($this->hasObjectAndStaticType($phpParserUnionType)) {
return null;
}
return $phpParserUnionType;
}
}

View File

@ -5,6 +5,7 @@ namespace Rector\StaticTypeMapper;
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
@ -78,7 +79,7 @@ final class StaticTypeMapper
}
/**
* @param TypeKind::* $typeKind
* @return Name|ComplexType|null
* @return Name|ComplexType|Identifier|null
*/
public function mapPHPStanTypeToPhpParserNode(Type $phpStanType, string $typeKind) : ?Node
{

View File

@ -263,7 +263,7 @@ final class AnonymousFunctionFactory
return $params;
}
/**
* @return \PhpParser\Node\Name|\PhpParser\Node\ComplexType|null
* @return \PhpParser\Node\Name|\PhpParser\Node\ComplexType|\PhpParser\Node\Identifier|null
*/
private function resolveParamType(ParameterReflection $parameterReflection)
{

View File

@ -0,0 +1,56 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Matcher;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\TypeDeclaration\ValueObject\ReturnFalseAndReturnOther;
final class ReturnFalseAndReturnOtherMatcher
{
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
*/
private $valueResolver;
public function __construct(BetterNodeFinder $betterNodeFinder, ValueResolver $valueResolver)
{
$this->betterNodeFinder = $betterNodeFinder;
$this->valueResolver = $valueResolver;
}
public function match(ClassMethod $classMethod) : ?ReturnFalseAndReturnOther
{
// there are 2 returns, one with false, other with something else except true
/** @var Return_[] $returns */
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($classMethod, Return_::class);
if (\count($returns) !== 2) {
return null;
}
$firstReturn = $returns[0];
$secondReturn = $returns[1];
if (!$firstReturn->expr instanceof Expr) {
return null;
}
if (!$secondReturn->expr instanceof Expr) {
return null;
}
if ($this->valueResolver->isFalse($firstReturn->expr) && !$this->valueResolver->isTrueOrFalse($secondReturn->expr)) {
return new ReturnFalseAndReturnOther($firstReturn, $secondReturn);
}
if (!$this->valueResolver->isFalse($secondReturn->expr)) {
return null;
}
if ($this->valueResolver->isTrueOrFalse($firstReturn->expr)) {
return null;
}
return new ReturnFalseAndReturnOther($secondReturn, $firstReturn);
}
}

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\TypeDeclaration\NodeTypeAnalyzer;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\UnionType;
@ -44,7 +45,7 @@ final class PropertyTypeDecorator
$this->nodeFactory = $nodeFactory;
}
/**
* @param \PhpParser\Node\Name|\PhpParser\Node\ComplexType $typeNode
* @param \PhpParser\Node\Name|\PhpParser\Node\ComplexType|\PhpParser\Node\Identifier $typeNode
*/
public function decoratePropertyUnionType(UnionType $unionType, $typeNode, Property $property, PhpDocInfo $phpDocInfo, bool $changeVarTypeFallback = \true) : void
{

View File

@ -0,0 +1,129 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\Matcher\ReturnFalseAndReturnOtherMatcher;
use Rector\TypeDeclaration\ValueObject\ReturnFalseAndReturnOther;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\FalseReturnClassMethodToNullableRector\FalseReturnClassMethodToNullableRectorTest
*/
final class FalseReturnClassMethodToNullableRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard
*/
private $classMethodReturnTypeOverrideGuard;
/**
* @readonly
* @var \Rector\TypeDeclaration\Matcher\ReturnFalseAndReturnOtherMatcher
*/
private $returnFalseAndReturnOtherMatcher;
public function __construct(ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ReturnFalseAndReturnOtherMatcher $returnFalseAndReturnOtherMatcher)
{
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
$this->returnFalseAndReturnOtherMatcher = $returnFalseAndReturnOtherMatcher;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change class method that returns false as invalid state, to nullable', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @return false|int
*/
public function run(int $number)
{
if ($number === 10) {
return false;
}
return $number;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run(int $number): ?int
{
if ($number === 10) {
return null;
}
return $number;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node) : ?Node
{
// it already has a type
if ($node->returnType instanceof Node) {
return null;
}
if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node)) {
return null;
}
$returnFalseAndReturnOther = $this->returnFalseAndReturnOtherMatcher->match($node);
if (!$returnFalseAndReturnOther instanceof ReturnFalseAndReturnOther) {
return null;
}
$falseReturn = $returnFalseAndReturnOther->getFalseReturn();
$otherReturn = $returnFalseAndReturnOther->getOtherReturn();
$anotherType = $this->getType($otherReturn->expr);
if ($anotherType instanceof MixedType) {
return null;
}
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($anotherType, TypeKind::RETURN);
if (!$typeNode instanceof Name && !$typeNode instanceof Identifier) {
return null;
}
$node->returnType = new NullableType($typeNode);
$this->clearReturnPhpDocTagNode($node);
// update return node
$falseReturn->expr = $this->nodeFactory->createNull();
return $node;
}
private function clearReturnPhpDocTagNode(ClassMethod $classMethod) : void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (!$phpDocInfo instanceof PhpDocInfo) {
return;
}
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
if (!$returnTagValueNode instanceof ReturnTagValueNode) {
return;
}
if ($returnTagValueNode->description !== '') {
// keep as useful
return;
}
$phpDocInfo->removeByType(ReturnTagValueNode::class);
}
}

View File

@ -7,6 +7,7 @@ use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
@ -125,7 +126,7 @@ CODE_SAMPLE
return PhpVersionFeature::TYPED_PROPERTIES;
}
/**
* @return Name|ComplexType|null
* @return Identifier|Name|ComplexType|null
*/
private function matchPropertySingleTypeNode(PropertyFetch $propertyFetch) : ?Node
{

View File

@ -0,0 +1,32 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\ValueObject;
use PhpParser\Node\Stmt\Return_;
final class ReturnFalseAndReturnOther
{
/**
* @readonly
* @var \PhpParser\Node\Stmt\Return_
*/
private $falseReturn;
/**
* @readonly
* @var \PhpParser\Node\Stmt\Return_
*/
private $otherReturn;
public function __construct(Return_ $falseReturn, Return_ $otherReturn)
{
$this->falseReturn = $falseReturn;
$this->otherReturn = $otherReturn;
}
public function getFalseReturn() : Return_
{
return $this->falseReturn;
}
public function getOtherReturn() : Return_
{
return $this->otherReturn;
}
}

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'cf2805b27271b3c2340436f2db13edab5a49b339';
public const PACKAGE_VERSION = '394fa70c963bb059acd6aae4b998173bdfc6c80c';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-12-21 01:10:41';
public const RELEASE_DATE = '2022-12-20 21:36:17';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit8b601cda39cfb569aef0d4be1a0f90ac::getLoader();
return ComposerAutoloaderInitbba29df86f92e47d6e1e73b5c11f3ccf::getLoader();

View File

@ -2702,6 +2702,7 @@ return array(
'Rector\\TypeDeclaration\\Guard\\PropertyTypeOverrideGuard' => $baseDir . '/rules/TypeDeclaration/Guard/PropertyTypeOverrideGuard.php',
'Rector\\TypeDeclaration\\Helper\\PhpDocNullableTypeHelper' => $baseDir . '/rules/TypeDeclaration/Helper/PhpDocNullableTypeHelper.php',
'Rector\\TypeDeclaration\\Matcher\\PropertyAssignMatcher' => $baseDir . '/rules/TypeDeclaration/Matcher/PropertyAssignMatcher.php',
'Rector\\TypeDeclaration\\Matcher\\ReturnFalseAndReturnOtherMatcher' => $baseDir . '/rules/TypeDeclaration/Matcher/ReturnFalseAndReturnOtherMatcher.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\AutowiredClassMethodOrPropertyAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/AutowiredClassMethodOrPropertyAnalyzer.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallTypesResolver' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallerParamMatcher' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php',
@ -2733,6 +2734,7 @@ return array(
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ArrayShapeFromConstantArrayReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ArrayShapeFromConstantArrayReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\FalseReturnClassMethodToNullableRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/FalseReturnClassMethodToNullableRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamAnnotationIncorrectNullableRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamAnnotationIncorrectNullableRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',
@ -2778,6 +2780,7 @@ return array(
'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.php',
'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => $baseDir . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php',
'Rector\\TypeDeclaration\\ValueObject\\NestedArrayType' => $baseDir . '/rules/TypeDeclaration/ValueObject/NestedArrayType.php',
'Rector\\TypeDeclaration\\ValueObject\\ReturnFalseAndReturnOther' => $baseDir . '/rules/TypeDeclaration/ValueObject/ReturnFalseAndReturnOther.php',
'Rector\\ValueObject\\ClassMethodWillChangeReturnType' => $vendorDir . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php',
'Rector\\VendorLocker\\NodeVendorLocker\\ClassMethodParamVendorLockResolver' => $baseDir . '/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php',
'Rector\\VendorLocker\\NodeVendorLocker\\ClassMethodReturnTypeOverrideGuard' => $baseDir . '/packages/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit8b601cda39cfb569aef0d4be1a0f90ac
class ComposerAutoloaderInitbba29df86f92e47d6e1e73b5c11f3ccf
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit8b601cda39cfb569aef0d4be1a0f90ac
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit8b601cda39cfb569aef0d4be1a0f90ac', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitbba29df86f92e47d6e1e73b5c11f3ccf', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit8b601cda39cfb569aef0d4be1a0f90ac', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitbba29df86f92e47d6e1e73b5c11f3ccf', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitbba29df86f92e47d6e1e73b5c11f3ccf::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitbba29df86f92e47d6e1e73b5c11f3ccf::$files;
$requireFile = static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac
class ComposerStaticInitbba29df86f92e47d6e1e73b5c11f3ccf
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2947,6 +2947,7 @@ class ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac
'Rector\\TypeDeclaration\\Guard\\PropertyTypeOverrideGuard' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Guard/PropertyTypeOverrideGuard.php',
'Rector\\TypeDeclaration\\Helper\\PhpDocNullableTypeHelper' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Helper/PhpDocNullableTypeHelper.php',
'Rector\\TypeDeclaration\\Matcher\\PropertyAssignMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Matcher/PropertyAssignMatcher.php',
'Rector\\TypeDeclaration\\Matcher\\ReturnFalseAndReturnOtherMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Matcher/ReturnFalseAndReturnOtherMatcher.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\AutowiredClassMethodOrPropertyAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/AutowiredClassMethodOrPropertyAnalyzer.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallTypesResolver' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallerParamMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php',
@ -2978,6 +2979,7 @@ class ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ArrayShapeFromConstantArrayReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ArrayShapeFromConstantArrayReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\FalseReturnClassMethodToNullableRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/FalseReturnClassMethodToNullableRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamAnnotationIncorrectNullableRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamAnnotationIncorrectNullableRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',
@ -3023,6 +3025,7 @@ class ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac
'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.php',
'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php',
'Rector\\TypeDeclaration\\ValueObject\\NestedArrayType' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/NestedArrayType.php',
'Rector\\TypeDeclaration\\ValueObject\\ReturnFalseAndReturnOther' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/ReturnFalseAndReturnOther.php',
'Rector\\ValueObject\\ClassMethodWillChangeReturnType' => __DIR__ . '/..' . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php',
'Rector\\VendorLocker\\NodeVendorLocker\\ClassMethodParamVendorLockResolver' => __DIR__ . '/../..' . '/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php',
'Rector\\VendorLocker\\NodeVendorLocker\\ClassMethodReturnTypeOverrideGuard' => __DIR__ . '/../..' . '/packages/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php',
@ -3055,9 +3058,9 @@ class ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit8b601cda39cfb569aef0d4be1a0f90ac::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitbba29df86f92e47d6e1e73b5c11f3ccf::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitbba29df86f92e47d6e1e73b5c11f3ccf::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitbba29df86f92e47d6e1e73b5c11f3ccf::$classMap;
}, null, ClassLoader::class);
}

View File

@ -1844,12 +1844,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-doctrine.git",
"reference": "0a8e8d9cc82356668cf03e10fb82f58d389db1c9"
"reference": "d5c39ae136f134e69af826d06628fd54c135ef6b"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-doctrine\/zipball\/0a8e8d9cc82356668cf03e10fb82f58d389db1c9",
"reference": "0a8e8d9cc82356668cf03e10fb82f58d389db1c9",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-doctrine\/zipball\/d5c39ae136f134e69af826d06628fd54c135ef6b",
"reference": "d5c39ae136f134e69af826d06628fd54c135ef6b",
"shasum": ""
},
"require": {
@ -1875,7 +1875,7 @@
"symplify\/rule-doc-generator": "^11.1",
"symplify\/vendor-patches": "^11.1"
},
"time": "2022-12-17T08:26:52+00:00",
"time": "2022-12-20T19:10:41+00:00",
"default-branch": true,
"type": "rector-extension",
"extra": {

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,7 @@ namespace Rector\RectorInstaller;
*/
final class GeneratedConfig
{
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 0a8e8d9'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main b1ca6d7'), 'rector/rector-php-parser' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-php-parser', 'relative_install_path' => '../../rector-php-parser', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 9ea5f62'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main a236bc6'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main eb02b35'));
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main d5c39ae'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main b1ca6d7'), 'rector/rector-php-parser' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-php-parser', 'relative_install_path' => '../../rector-php-parser', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 9ea5f62'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main a236bc6'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main eb02b35'));
private function __construct()
{
}

View File

@ -17,6 +17,6 @@ return static function (RectorConfig $rectorConfig) : void {
'*/Source/*',
'*/Fixture/*',
]);
$rectorConfig->sets([LevelSetList::UP_TO_PHP_81, SetList::DEAD_CODE, SetList::CODE_QUALITY, SetList::NAMING, SetList::TYPE_DECLARATION, SetList::TYPE_DECLARATION_STRICT, SetList::PRIVATIZATION]);
$rectorConfig->sets([LevelSetList::UP_TO_PHP_81, SetList::DEAD_CODE, SetList::CODE_QUALITY, SetList::NAMING, SetList::TYPE_DECLARATION, SetList::PRIVATIZATION]);
$rectorConfig->ruleWithConfiguration(StringClassNameToClassConstantRector::class, ['Doctrine\\*', 'Gedmo\\*', 'Knp\\*', 'DateTime', 'DateTimeInterface']);
};

View File

@ -5,6 +5,7 @@ namespace Rector\Doctrine\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\MixedType;
@ -107,7 +108,7 @@ CODE_SAMPLE
return $node;
}
/**
* @param \PhpParser\Node\Name|\PhpParser\Node\ComplexType $typeNode
* @param \PhpParser\Node\Name|\PhpParser\Node\ComplexType|\PhpParser\Node\Identifier $typeNode
*/
private function completePropertyTypeOrVarDoc(Type $propertyType, $typeNode, Property $property) : void
{