mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 08:20:53 +00:00
[TypeDeclaration] Add ReturnTypeFromStrictNewArrayRector (#2572)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
80715e62b5
commit
78d110af4c
|
@ -1,4 +1,4 @@
|
|||
# 519 Rules Overview
|
||||
# 520 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -94,7 +94,7 @@
|
|||
|
||||
- [Transform](#transform) (36)
|
||||
|
||||
- [TypeDeclaration](#typedeclaration) (28)
|
||||
- [TypeDeclaration](#typedeclaration) (29)
|
||||
|
||||
- [Visibility](#visibility) (3)
|
||||
|
||||
|
@ -11908,6 +11908,27 @@ Add strict return type based native function return
|
|||
|
||||
<br>
|
||||
|
||||
### ReturnTypeFromStrictNewArrayRector
|
||||
|
||||
Add strict return array type based on created empty array and returned
|
||||
|
||||
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
- public function run()
|
||||
+ public function run(): array
|
||||
{
|
||||
$values = [];
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### ReturnTypeFromStrictTypedCallRector
|
||||
|
||||
Add return type from strict return type of call
|
||||
|
|
|
@ -10,6 +10,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnR
|
|||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
|
||||
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
|
||||
|
@ -32,4 +33,5 @@ return static function (RectorConfig $rectorConfig): void {
|
|||
ReturnTypeFromStrictBoolReturnExprRector::class,
|
||||
]);
|
||||
$rectorConfig->rule(ReturnTypeFromStrictNativeFuncCallRector::class);
|
||||
$rectorConfig->rule(ReturnTypeFromStrictNewArrayRector::class);
|
||||
};
|
||||
|
|
|
@ -35,7 +35,7 @@ return static function (RectorConfig $rectorConfig): void {
|
|||
$rectorConfig->ruleWithConfiguration(
|
||||
PreferThisOrSelfMethodCallRector::class,
|
||||
[
|
||||
'PHPUnit\Framework\TestCase' => PreferenceSelfThis::PREFER_THIS(),
|
||||
'PHPUnit\Framework\TestCase' => PreferenceSelfThis::PREFER_THIS,
|
||||
]
|
||||
);
|
||||
|
||||
|
|
|
@ -13,8 +13,8 @@ use Rector\Tests\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector\
|
|||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig
|
||||
->ruleWithConfiguration(PreferThisOrSelfMethodCallRector::class, [
|
||||
SomeAbstractTestCase::class => PreferenceSelfThis::PREFER_SELF(),
|
||||
BeLocalClass::class => PreferenceSelfThis::PREFER_THIS(),
|
||||
TestCase::class => PreferenceSelfThis::PREFER_SELF(),
|
||||
SomeAbstractTestCase::class => PreferenceSelfThis::PREFER_SELF,
|
||||
BeLocalClass::class => PreferenceSelfThis::PREFER_THIS,
|
||||
TestCase::class => PreferenceSelfThis::PREFER_SELF,
|
||||
]);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;
|
||||
|
||||
final class ReturnOverrideWithArray
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$values = [];
|
||||
|
||||
$values = [1000];
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;
|
||||
|
||||
final class ReturnOverrideWithArray
|
||||
{
|
||||
public function run(): array
|
||||
{
|
||||
$values = [];
|
||||
|
||||
$values = [1000];
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;
|
||||
|
||||
final class SkipOverride
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$values = [];
|
||||
|
||||
$values = 100;
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;
|
||||
|
||||
final class SkipTwoReturns
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$values = [];
|
||||
|
||||
if (mt_rand(1, 2)) {
|
||||
return $values;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$values = [];
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(): array
|
||||
{
|
||||
$values = [];
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ReturnTypeFromStrictNewArrayRectorTest 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\ReturnTypeFromStrictNewArrayRector;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->rule(ReturnTypeFromStrictNewArrayRector::class);
|
||||
};
|
|
@ -0,0 +1,177 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Expr\Yield_;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\ReturnTypeFromStrictNewArrayRectorTest
|
||||
*/
|
||||
final class ReturnTypeFromStrictNewArrayRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add strict return array type based on created empty array and returned', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$values = [];
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(): array
|
||||
{
|
||||
$values = [];
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class, Function_::class, Closure::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Function_|Closure $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->returnType !== null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 1. is variable instantiated with array
|
||||
$stmts = $node->stmts;
|
||||
if ($stmts === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$variable = $this->matchArrayAssignedVariable($stmts);
|
||||
if (! $variable instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. skip yields
|
||||
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, [Yield_::class])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Return_[] $returns */
|
||||
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($node, Return_::class);
|
||||
if (count($returns) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isVariableOverriddenWithNonArray($node, $variable)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$onlyReturn = $returns[0];
|
||||
if (! $onlyReturn->expr instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->nodeNameResolver->areNamesEqual($onlyReturn->expr, $variable)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. always returns array
|
||||
$node->returnType = new Identifier('array');
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
public function provideMinPhpVersion(): int
|
||||
{
|
||||
return PhpVersion::PHP_70;
|
||||
}
|
||||
|
||||
private function isVariableOverriddenWithNonArray(
|
||||
ClassMethod|Function_|Closure $functionLike,
|
||||
Variable $variable
|
||||
): bool {
|
||||
// is variable overriden?
|
||||
/** @var Assign[] $assigns */
|
||||
$assigns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($functionLike, Assign::class);
|
||||
foreach ($assigns as $assign) {
|
||||
if (! $assign->var instanceof Variable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $this->nodeNameResolver->areNamesEqual($assign->var, $variable)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $assign->expr instanceof Array_) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
*/
|
||||
private function matchArrayAssignedVariable(array $stmts): Variable|null
|
||||
{
|
||||
foreach ($stmts as $stmt) {
|
||||
if (! $stmt instanceof Expression) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $stmt->expr instanceof Assign) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$assign = $stmt->expr;
|
||||
if (! $assign->var instanceof Variable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $assign->expr instanceof Array_) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $assign->var;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user