Skip to content

Commit

Permalink
[TypeDeclaration] Kick off ReturnTypeFromStrictReturnExprRector (#2563)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 25, 2022
1 parent 564c127 commit 4270770
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 16 deletions.
28 changes: 26 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 518 Rules Overview
# 519 Rules Overview

<br>

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

- [Transform](#transform) (36)

- [TypeDeclaration](#typedeclaration) (26)
- [TypeDeclaration](#typedeclaration) (27)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions config/set/type-declaration-strict.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -15,14 +16,17 @@
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,
]);
};
1 change: 0 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
// SetList::RECTOR_CONFIG,
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture;

final class SkipVoidReturn
{
public function run()
{
return;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

?>
Original file line number Diff line number Diff line change
@@ -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';
}
}
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ private function resolveValueFromType(ConstantType $constantType): ConstFetch |
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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 4270770

Please sign in to comment.