Skip to content

Commit

Permalink
[TypeDeclaration] Add FalseReturnClassMethodToNullableRector (#3229)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 20, 2022
1 parent cf2805b commit 394fa70
Show file tree
Hide file tree
Showing 32 changed files with 476 additions and 75 deletions.
47 changes: 34 additions & 13 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 413 Rules Overview
# 414 Rules Overview

<br>

Expand Down Expand Up @@ -64,7 +64,7 @@

- [Transform](#transform) (34)

- [TypeDeclaration](#typedeclaration) (37)
- [TypeDeclaration](#typedeclaration) (38)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -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;
}
```

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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;
Expand All @@ -30,7 +31,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
/**
* @param TType $type
* @param TypeKind::* $typeKind
* @return Name|ComplexType|null
* @return Name|ComplexType|Identifier|null
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node;
}
3 changes: 2 additions & 1 deletion packages/PHPStanStaticTypeMapper/PHPStanStaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
/**
* @param TypeKind::* $typeKind
*/
public function mapToPhpParserNode(Type $type, string $typeKind): Name | ComplexType | null
public function mapToPhpParserNode(Type $type, string $typeKind): Name | ComplexType | Identifier | null
{
foreach ($this->typeMappers as $typeMapper) {
if (! is_a($type, $typeMapper->getNodeClass(), true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -49,6 +49,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('string');
return new Identifier('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -49,6 +49,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('string');
return new Identifier('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -49,6 +49,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('string');
return new Identifier('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -49,6 +49,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('string');
return new Identifier('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -124,7 +124,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
return new Name('array');
return new Identifier('array');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -60,10 +60,10 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -55,6 +55,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('string');
return new Identifier('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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;
Expand Down Expand Up @@ -66,7 +66,7 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('string');
return new Identifier('string');
}

private function resolveGenericObjectType(GenericClassStringType $genericClassStringType): ObjectType|Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -39,6 +39,6 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
return new Name('array');
return new Identifier('array');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -39,6 +39,6 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
return new Name('array');
return new Identifier('array');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -49,6 +49,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('int');
return new Identifier('int');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -39,6 +39,6 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
return new Name('array');
return new Identifier('array');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -46,6 +46,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('string');
return new Identifier('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
Expand Down Expand Up @@ -49,6 +49,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('string');
return new Identifier('string');
}
}
Loading

0 comments on commit 394fa70

Please sign in to comment.