Skip to content

Commit

Permalink
[TypeDeclaration] Add ReturnTypeFromStrictNativeFuncCallRector (#2570)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 26, 2022
1 parent e424660 commit 237f255
Show file tree
Hide file tree
Showing 22 changed files with 564 additions and 175 deletions.
27 changes: 23 additions & 4 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 519 Rules Overview
# 520 Rules Overview

<br>

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

- [Transform](#transform) (36)

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

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -11894,11 +11894,11 @@ Add return type to function like with return new

<br>

### ReturnTypeFromStrictReturnExprRector
### ReturnTypeFromStrictBoolReturnExprRector

Add strict return type based on returned strict expr type

- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector.php)
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php)

```diff
final class SomeClass
Expand All @@ -11913,6 +11913,25 @@ Add strict return type based on returned strict expr type

<br>

### ReturnTypeFromStrictNativeFuncCallRector

Add strict return type based native function return

- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeFuncCallRector.php)

```diff
final class SomeClass
{
- public function run()
+ public function run(): int
{
return strlen('value');
}
}
```

<br>

### ReturnTypeFromStrictTypedCallRector

Add return type from strict return type of call
Expand Down
7 changes: 5 additions & 2 deletions config/set/type-declaration-strict.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
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\ReturnTypeFromStrictBoolReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
Expand All @@ -27,6 +29,7 @@
TypedPropertyFromStrictGetterMethodReturnTypeRector::class,
AddMethodCallBasedStrictParamTypeRector::class,
ArrayShapeFromConstantArrayReturnRector::class,
ReturnTypeFromStrictReturnExprRector::class,
ReturnTypeFromStrictBoolReturnExprRector::class,
]);
$rectorConfig->rule(ReturnTypeFromStrictNativeFuncCallRector::class);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

final class MultiReturnsAllBool
{
Expand All @@ -18,7 +18,7 @@ final class MultiReturnsAllBool
-----
<?php

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

final class MultiReturnsAllBool
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

final class SkipMultiReturnsNoRoot
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

final class SkipNestedReturn
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

final class SkipRootReturnWithNestedReturn
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

final class SkipVoidReturn
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

final class SomeClass
{
Expand All @@ -19,7 +19,7 @@ final class SomeClass
-----
<?php

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

final class SomeClass
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

final class SomeClosure
{
public function run()
{
return function () {
return 100 && true;
};
}
}

?>
-----
<?php

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

final class SomeClosure
{
public function run()
{
return function (): bool {
return 100 && true;
};
}
}

?>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

final class VoidReturnInInnerFunction
{
Expand All @@ -23,7 +23,7 @@ final class VoidReturnInInnerFunction
-----
<?php

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

final class VoidReturnInInnerFunction
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

final class YieldParentInnerFunction
{
Expand All @@ -23,7 +23,7 @@ final class YieldParentInnerFunction
-----
<?php

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

final class YieldParentInnerFunction
{
Expand All @@ -42,4 +42,4 @@ final class YieldParentInnerFunction
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

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

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ReturnTypeFromStrictReturnExprRectorTest extends AbstractRectorTestCase
final class ReturnTypeFromStrictBoolReturnExprRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ReturnTypeFromStrictReturnExprRector::class);
$rectorConfig->rule(ReturnTypeFromStrictBoolReturnExprRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

final class SomeClass
{
public function run()
{
return strlen('value');
}
}

?>
-----
<?php

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

final class SomeClass
{
public function run(): int
{
return strlen('value');
}
}

?>
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\ReturnTypeFromStrictNativeFuncCallRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ReturnTypeFromStrictNativeFuncCallRectorTest 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,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ReturnTypeFromStrictNativeFuncCallRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnFilter;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Reflection\FunctionReflection;
use Rector\Core\Reflection\ReflectionResolver;

final class ExclusiveNativeFuncCallReturnMatcher
{
public function __construct(
private readonly ReflectionResolver $reflectionResolver,
) {
}

/**
* @param Return_[] $returns
* @return FuncCall[]|null
*/
public function match(array $returns): array|null
{
$funcCalls = [];

foreach ($returns as $return) {
// we need exact expr return
if (! $return->expr instanceof Expr) {
return null;
}

if (! $return->expr instanceof FuncCall) {
return null;
}

$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($return->expr);
if (! $functionReflection instanceof FunctionReflection) {
return null;
}

if (! $functionReflection->isBuiltin()) {
return null;
}

$funcCalls[] = $return->expr;
}

return $funcCalls;
}
}
Loading

0 comments on commit 237f255

Please sign in to comment.