mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
[TypeDeclaration] Kick off ReturnTypeFromStrictReturnExprRector (#2563)
This commit is contained in:
parent
564c127680
commit
42707709f7
|
@ -1,4 +1,4 @@
|
|||
# 518 Rules Overview
|
||||
# 519 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -94,7 +94,7 @@
|
|||
|
||||
- [Transform](#transform) (36)
|
||||
|
||||
- [TypeDeclaration](#typedeclaration) (26)
|
||||
- [TypeDeclaration](#typedeclaration) (27)
|
||||
|
||||
- [Visibility](#visibility) (3)
|
||||
|
||||
|
@ -11894,6 +11894,30 @@ Add return type to function like with return new
|
|||
|
||||
<br>
|
||||
|
||||
### ReturnTypeFromStrictReturnExprRector
|
||||
|
||||
Add strict return type based on returned strict expr type
|
||||
|
||||
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return $this->first() && true;
|
||||
}
|
||||
|
||||
- public function first()
|
||||
+ public function first(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### ReturnTypeFromStrictTypedCallRector
|
||||
|
||||
Add return type from strict return type of call
|
||||
|
|
|
@ -7,6 +7,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeR
|
|||
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
|
||||
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
|
||||
|
@ -15,14 +16,17 @@ use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRec
|
|||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->rule(AddClosureReturnTypeRector::class);
|
||||
$rectorConfig->rule(ReturnTypeFromStrictTypedPropertyRector::class);
|
||||
$rectorConfig->rule(TypedPropertyFromStrictConstructorRector::class);
|
||||
$rectorConfig->rule(ParamTypeFromStrictTypedPropertyRector::class);
|
||||
$rectorConfig->rule(ReturnTypeFromStrictTypedCallRector::class);
|
||||
$rectorConfig->rule(AddVoidReturnTypeWhereNoReturnRector::class);
|
||||
$rectorConfig->rule(ReturnTypeFromReturnNewRector::class);
|
||||
$rectorConfig->rule(TypedPropertyFromStrictGetterMethodReturnTypeRector::class);
|
||||
$rectorConfig->rule(AddMethodCallBasedStrictParamTypeRector::class);
|
||||
$rectorConfig->rule(ArrayShapeFromConstantArrayReturnRector::class);
|
||||
$rectorConfig->rules([
|
||||
AddClosureReturnTypeRector::class,
|
||||
ReturnTypeFromStrictTypedPropertyRector::class,
|
||||
TypedPropertyFromStrictConstructorRector::class,
|
||||
ParamTypeFromStrictTypedPropertyRector::class,
|
||||
ReturnTypeFromStrictTypedCallRector::class,
|
||||
AddVoidReturnTypeWhereNoReturnRector::class,
|
||||
ReturnTypeFromReturnNewRector::class,
|
||||
TypedPropertyFromStrictGetterMethodReturnTypeRector::class,
|
||||
AddMethodCallBasedStrictParamTypeRector::class,
|
||||
ArrayShapeFromConstantArrayReturnRector::class,
|
||||
ReturnTypeFromStrictReturnExprRector::class,
|
||||
]);
|
||||
};
|
||||
|
|
|
@ -19,7 +19,6 @@ use Rector\Set\ValueObject\SetList;
|
|||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->sets([
|
||||
// SetList::RECTOR_CONFIG,
|
||||
LevelSetList::UP_TO_PHP_81,
|
||||
SetList::CODE_QUALITY,
|
||||
SetList::DEAD_CODE,
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture;
|
||||
|
||||
final class SkipNestedReturn
|
||||
{
|
||||
public function run(array $values)
|
||||
{
|
||||
foreach ($values as $value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture;
|
||||
|
||||
final class SkipVoidReturn
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return $this->first() && true;
|
||||
}
|
||||
|
||||
public function first()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(): bool
|
||||
{
|
||||
return $this->first() && true;
|
||||
}
|
||||
|
||||
public function first(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ReturnTypeFromStrictReturnExprRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->rule(ReturnTypeFromStrictReturnExprRector::class);
|
||||
};
|
|
@ -57,8 +57,6 @@ final class DefaultParameterValueResolver
|
|||
private function resolveConstantBooleanType(ConstantBooleanType $constantBooleanType): ConstFetch
|
||||
{
|
||||
$value = $constantBooleanType->describe(VerbosityLevel::value());
|
||||
$name = new Name($value);
|
||||
|
||||
return new ConstFetch($name);
|
||||
return new ConstFetch(new Name($value));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,149 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
||||
use PhpParser\Node\Expr\BinaryOp\Equal;
|
||||
use PhpParser\Node\Expr\BinaryOp\Identical;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotEqual;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Expr\Empty_;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\ReturnTypeFromStrictReturnExprRectorTest
|
||||
*/
|
||||
final class ReturnTypeFromStrictReturnExprRector extends AbstractRector
|
||||
{
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add strict return type based on returned strict expr type', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return $this->first() && true;
|
||||
}
|
||||
|
||||
public function first()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return $this->first() && true;
|
||||
}
|
||||
|
||||
public function first(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->returnType !== null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->hasSingleStrictReturn($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$node->returnType = new Identifier('bool');
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function isStrictBoolExpr(Expr $expr): bool
|
||||
{
|
||||
// detect strict type here :)
|
||||
if ($expr instanceof Empty_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($expr instanceof BooleanAnd) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($expr instanceof BooleanOr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($expr instanceof Equal) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($expr instanceof NotEqual) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($expr instanceof Identical) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($expr instanceof NotIdentical) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $expr instanceof ConstFetch && in_array($expr->name->toLowerString(), ['true', 'false'], true);
|
||||
}
|
||||
|
||||
private function hasSingleStrictReturn(ClassMethod $classMethod): bool
|
||||
{
|
||||
if ($classMethod->stmts === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($classMethod->stmts as $stmt) {
|
||||
if (! $stmt instanceof Return_) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// we need exact expr return
|
||||
if (! $stmt->expr instanceof Expr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->isStrictBoolExpr($stmt->expr)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user