Updated Rector to commit 78d110af4c

78d110af4c [TypeDeclaration] Add ReturnTypeFromStrictNewArrayRector (#2572)
This commit is contained in:
Tomas Votruba 2022-06-26 11:48:44 +00:00
parent 68845a00bf
commit 2e275ef1ff
8 changed files with 191 additions and 16 deletions

View File

@ -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;
@ -19,4 +20,5 @@ use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodRe
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rules([AddClosureReturnTypeRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, ReturnTypeFromStrictTypedCallRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ArrayShapeFromConstantArrayReturnRector::class, ReturnTypeFromStrictBoolReturnExprRector::class]);
$rectorConfig->rule(ReturnTypeFromStrictNativeFuncCallRector::class);
$rectorConfig->rule(ReturnTypeFromStrictNewArrayRector::class);
};

View File

@ -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

View File

@ -0,0 +1,150 @@
<?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;
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
*/
private function isVariableOverriddenWithNonArray($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
* @return \PhpParser\Node\Expr\Variable|null
*/
private function matchArrayAssignedVariable(array $stmts)
{
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;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '80715e62b5da2ad57bd7bc0fbf4be78a12e5e6fa';
public const PACKAGE_VERSION = '78d110af4c8dc8df0b335cbbc857087cd260b55a';
/**
* @var string
*/
public const RELEASE_DATE = '2022-06-26 12:11:25';
public const RELEASE_DATE = '2022-06-26 13:43:47';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit9654d1f39e99e3f4b32bf14eaa6c8cf3::getLoader();
return ComposerAutoloaderInite8e0c158773724458547a61937887466::getLoader();

View File

@ -3026,6 +3026,7 @@ return array(
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictBoolReturnExprRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictNativeFuncCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeFuncCallRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictNewArrayRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php',
'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureReturnTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/Closure/AddClosureReturnTypeRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit9654d1f39e99e3f4b32bf14eaa6c8cf3
class ComposerAutoloaderInite8e0c158773724458547a61937887466
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit9654d1f39e99e3f4b32bf14eaa6c8cf3
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit9654d1f39e99e3f4b32bf14eaa6c8cf3', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInite8e0c158773724458547a61937887466', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit9654d1f39e99e3f4b32bf14eaa6c8cf3', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInite8e0c158773724458547a61937887466', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit9654d1f39e99e3f4b32bf14eaa6c8cf3::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInite8e0c158773724458547a61937887466::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit9654d1f39e99e3f4b32bf14eaa6c8cf3::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInite8e0c158773724458547a61937887466::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire9654d1f39e99e3f4b32bf14eaa6c8cf3($fileIdentifier, $file);
composerRequiree8e0c158773724458547a61937887466($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit9654d1f39e99e3f4b32bf14eaa6c8cf3
* @param string $file
* @return void
*/
function composerRequire9654d1f39e99e3f4b32bf14eaa6c8cf3($fileIdentifier, $file)
function composerRequiree8e0c158773724458547a61937887466($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit9654d1f39e99e3f4b32bf14eaa6c8cf3
class ComposerStaticInite8e0c158773724458547a61937887466
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -3327,6 +3327,7 @@ class ComposerStaticInit9654d1f39e99e3f4b32bf14eaa6c8cf3
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictBoolReturnExprRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictNativeFuncCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeFuncCallRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictNewArrayRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php',
'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureReturnTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Closure/AddClosureReturnTypeRector.php',
@ -3406,9 +3407,9 @@ class ComposerStaticInit9654d1f39e99e3f4b32bf14eaa6c8cf3
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit9654d1f39e99e3f4b32bf14eaa6c8cf3::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit9654d1f39e99e3f4b32bf14eaa6c8cf3::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit9654d1f39e99e3f4b32bf14eaa6c8cf3::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInite8e0c158773724458547a61937887466::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInite8e0c158773724458547a61937887466::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInite8e0c158773724458547a61937887466::$classMap;
}, null, ClassLoader::class);
}