Updated Rector to commit 42707709f7

42707709f7 [TypeDeclaration] Kick off ReturnTypeFromStrictReturnExprRector (#2563)
This commit is contained in:
Tomas Votruba 2022-06-25 14:38:11 +00:00
parent a0db66f4be
commit c10f29b7bb
9 changed files with 170 additions and 28 deletions

View File

@ -8,6 +8,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,5 @@ use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
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]);
};

View File

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

View File

@ -53,7 +53,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));
}
}

View File

@ -0,0 +1,125 @@
<?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;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '564c12768040c63675985f005471a32a603e75d7';
public const PACKAGE_VERSION = '42707709f7ad4ab383d737c0bd6510e6c3609f1d';
/**
* @var string
*/
public const RELEASE_DATE = '2022-06-25 13:16:16';
public const RELEASE_DATE = '2022-06-25 14:32:05';
/**
* @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 ComposerAutoloaderInit0759fe3532392fe14d15b38f1e201370::getLoader();
return ComposerAutoloaderInit93f0bbaa77876060210357e31273c54f::getLoader();

View File

@ -3022,6 +3022,7 @@ return array(
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnAnnotationIncorrectNullableRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnAnnotationIncorrectNullableRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictReturnExprRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector.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 ComposerAutoloaderInit0759fe3532392fe14d15b38f1e201370
class ComposerAutoloaderInit93f0bbaa77876060210357e31273c54f
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit0759fe3532392fe14d15b38f1e201370
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit0759fe3532392fe14d15b38f1e201370', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit93f0bbaa77876060210357e31273c54f', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit0759fe3532392fe14d15b38f1e201370', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit93f0bbaa77876060210357e31273c54f', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit0759fe3532392fe14d15b38f1e201370::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit93f0bbaa77876060210357e31273c54f::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit0759fe3532392fe14d15b38f1e201370::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInit93f0bbaa77876060210357e31273c54f::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire0759fe3532392fe14d15b38f1e201370($fileIdentifier, $file);
composerRequire93f0bbaa77876060210357e31273c54f($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit0759fe3532392fe14d15b38f1e201370
* @param string $file
* @return void
*/
function composerRequire0759fe3532392fe14d15b38f1e201370($fileIdentifier, $file)
function composerRequire93f0bbaa77876060210357e31273c54f($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 ComposerStaticInit0759fe3532392fe14d15b38f1e201370
class ComposerStaticInit93f0bbaa77876060210357e31273c54f
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -3323,6 +3323,7 @@ class ComposerStaticInit0759fe3532392fe14d15b38f1e201370
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnAnnotationIncorrectNullableRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnAnnotationIncorrectNullableRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictReturnExprRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector.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',
@ -3401,9 +3402,9 @@ class ComposerStaticInit0759fe3532392fe14d15b38f1e201370
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit0759fe3532392fe14d15b38f1e201370::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit0759fe3532392fe14d15b38f1e201370::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit0759fe3532392fe14d15b38f1e201370::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit93f0bbaa77876060210357e31273c54f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit93f0bbaa77876060210357e31273c54f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit93f0bbaa77876060210357e31273c54f::$classMap;
}, null, ClassLoader::class);
}