Updated Rector to commit 76da9489fe16c5854d5fed29b81197940025e975

76da9489fe Make ConsecutiveNullCompareReturnsToNullCoalesceQueueRector use of StmtsAwareInterface (#3783)
This commit is contained in:
Tomas Votruba 2023-05-10 06:07:59 +00:00
parent 6da6d1d775
commit 2cad3fa15d
7 changed files with 69 additions and 116 deletions

View File

@ -1,4 +1,4 @@
# 415 Rules Overview
# 414 Rules Overview
<br>
@ -62,7 +62,7 @@
- [Strict](#strict) (6)
- [Transform](#transform) (31)
- [Transform](#transform) (30)
- [TypeDeclaration](#typedeclaration) (40)
@ -665,11 +665,11 @@ Change multiple null compares to ?? queue
{
public function run()
{
- if (null !== $this->orderItem) {
- if ($this->orderItem !== null) {
- return $this->orderItem;
- }
-
- if (null !== $this->orderItemUnit) {
- if ($this->orderItemUnit !== null) {
- return $this->orderItemUnit;
- }
-
@ -1383,12 +1383,12 @@ Changes foreach that returns set value to ??
```diff
-foreach ($this->oldToNewFunctions as $oldFunction => $newFunction) {
- if ($currentFunction === $oldFunction) {
- return $newFunction;
- innerForeachReturn $newFunction;
- }
-}
-
-return null;
+return $this->oldToNewFunctions[$currentFunction] ?? null;
-innerForeachReturn null;
+innerForeachReturn $this->oldToNewFunctions[$currentFunction] ?? null;
```
<br>
@ -7907,46 +7907,6 @@ return static function (RectorConfig $rectorConfig): void {
<br>
### FileGetContentsAndJsonDecodeToStaticCallRector
Merge 2 function calls to static call
:wrench: **configure it!**
- class: [`Rector\Transform\Rector\FunctionLike\FileGetContentsAndJsonDecodeToStaticCallRector`](../rules/Transform/Rector/FunctionLike/FileGetContentsAndJsonDecodeToStaticCallRector.php)
```php
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Transform\Rector\FunctionLike\FileGetContentsAndJsonDecodeToStaticCallRector;
use Rector\Transform\ValueObject\StaticCallRecipe;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(FileGetContentsAndJsonDecodeToStaticCallRector::class, [
new StaticCallRecipe('FileLoader', 'loadJson'),
]);
};
```
```diff
final class SomeClass
{
public function load($filePath)
{
- $fileGetContents = file_get_contents($filePath);
- return json_decode($fileGetContents, true);
+ return FileLoader::loadJson($filePath);
}
}
```
<br>
### FuncCallToConstFetchRector
Changes use of function calls to use constants

View File

@ -6,12 +6,13 @@ namespace Rector\CodeQuality\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -20,14 +21,6 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class ConsecutiveNullCompareReturnsToNullCoalesceQueueRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var Node[]
*/
private $nodesToRemove = [];
/**
* @var Expr[]
*/
private $coalescingNodes = [];
/**
* @readonly
* @var \Rector\Core\NodeManipulator\IfManipulator
@ -44,11 +37,11 @@ class SomeClass
{
public function run()
{
if (null !== $this->orderItem) {
if ($this->orderItem !== null) {
return $this->orderItem;
}
if (null !== $this->orderItemUnit) {
if ($this->orderItemUnit !== null) {
return $this->orderItemUnit;
}
@ -72,71 +65,74 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [If_::class];
return [StmtsAwareInterface::class];
}
/**
* @param If_ $node
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node) : ?Node
{
$this->reset();
$currentNode = $node;
while ($currentNode instanceof Node) {
if ($currentNode instanceof If_) {
$comparedNode = $this->ifManipulator->matchIfNotNullReturnValue($currentNode);
if ($comparedNode instanceof Expr) {
$this->coalescingNodes[] = $comparedNode;
$this->nodesToRemove[] = $currentNode;
$currentNode = $currentNode->getAttribute(AttributeKey::NEXT_NODE);
continue;
}
return null;
}
if ($this->isReturnNull($currentNode)) {
$this->nodesToRemove[] = $currentNode;
break;
}
if ($node->stmts === null) {
return null;
}
$coalescingExprs = [];
$ifKeys = [];
foreach ($node->stmts as $key => $stmt) {
if (!$stmt instanceof If_) {
continue;
}
$comparedExpr = $this->ifManipulator->matchIfNotNullReturnValue($stmt);
if (!$comparedExpr instanceof Expr) {
continue;
}
$coalescingExprs[] = $comparedExpr;
$ifKeys[] = $key;
}
// at least 2 coalescing nodes are needed
if (\count($this->coalescingNodes) < 2) {
if (\count($coalescingExprs) < 2) {
return null;
}
$this->nodeRemover->removeNodes($this->nodesToRemove);
return $this->createReturnCoalesceNode($this->coalescingNodes);
// remove last return null
foreach ($node->stmts as $key => $stmt) {
if (\in_array($key, $ifKeys, \true)) {
unset($node->stmts[$key]);
continue;
}
if (!$this->isReturnNull($stmt)) {
continue;
}
unset($node->stmts[$key]);
}
$node->stmts[] = $this->createCealesceReturn($coalescingExprs);
return $node;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::NULL_COALESCE;
}
private function reset() : void
private function isReturnNull(Stmt $stmt) : bool
{
$this->coalescingNodes = [];
$this->nodesToRemove = [];
}
private function isReturnNull(Node $node) : bool
{
if (!$node instanceof Return_) {
if (!$stmt instanceof Return_) {
return \false;
}
if (!$node->expr instanceof Expr) {
if (!$stmt->expr instanceof Expr) {
return \false;
}
return $this->valueResolver->isNull($node->expr);
return $this->valueResolver->isNull($stmt->expr);
}
/**
* @param Expr[] $coalescingNodes
* @param Expr[] $coalescingExprs
*/
private function createReturnCoalesceNode(array $coalescingNodes) : Return_
private function createCealesceReturn(array $coalescingExprs) : Return_
{
/** @var Expr $left */
$left = \array_shift($coalescingNodes);
/** @var Expr $right */
$right = \array_shift($coalescingNodes);
$coalesceNode = new Coalesce($left, $right);
foreach ($coalescingNodes as $coalescingNode) {
$coalesceNode = new Coalesce($coalesceNode, $coalescingNode);
/** @var Expr $leftExpr */
$leftExpr = \array_shift($coalescingExprs);
/** @var Expr $rightExpr */
$rightExpr = \array_shift($coalescingExprs);
$coalesce = new Coalesce($leftExpr, $rightExpr);
foreach ($coalescingExprs as $coalescingExpr) {
$coalesce = new Coalesce($coalesce, $coalescingExpr);
}
return new Return_($coalesceNode);
return new Return_($coalesce);
}
}

View File

@ -90,10 +90,8 @@ CODE_SAMPLE
if (!$node->var instanceof Variable) {
return null;
}
/** @var string $oldVariableName */
$oldVariableName = $this->getName($node->var);
if (!\is_string($oldVariableName)) {
return null;
}
$type = $node->types[0];
$typeShortName = $this->nodeNameResolver->getShortName($type);
$aliasName = $this->aliasNameResolver->resolveByName($type);
@ -101,8 +99,7 @@ CODE_SAMPLE
$typeShortName = $aliasName;
}
$newVariableName = Strings::replace(\lcfirst($typeShortName), self::STARTS_WITH_ABBREVIATION_REGEX, static function (array $matches) : string {
$output = '';
$output .= isset($matches[1]) ? \strtolower((string) $matches[1]) : '';
$output = isset($matches[1]) ? \strtolower((string) $matches[1]) : '';
$output .= $matches[2] ?? '';
return $output . ($matches[3] ?? '');
});

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '89bc4d80f5dd4c7e81a616c8b58a921a1470a47b';
public const PACKAGE_VERSION = '76da9489fe16c5854d5fed29b81197940025e975';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-10 05:14:15';
public const RELEASE_DATE = '2023-05-10 06:03:12';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInited13d2d55a7019dfa6e7e16a14385fd2
class ComposerAutoloaderInit7c96cac68aa678588e9ac8ea243e6ab0
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInited13d2d55a7019dfa6e7e16a14385fd2
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInited13d2d55a7019dfa6e7e16a14385fd2', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit7c96cac68aa678588e9ac8ea243e6ab0', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInited13d2d55a7019dfa6e7e16a14385fd2', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit7c96cac68aa678588e9ac8ea243e6ab0', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit7c96cac68aa678588e9ac8ea243e6ab0::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit7c96cac68aa678588e9ac8ea243e6ab0::$files;
$requireFile = \Closure::bind(static function ($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 ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2
class ComposerStaticInit7c96cac68aa678588e9ac8ea243e6ab0
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3112,9 +3112,9 @@ class ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit7c96cac68aa678588e9ac8ea243e6ab0::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit7c96cac68aa678588e9ac8ea243e6ab0::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit7c96cac68aa678588e9ac8ea243e6ab0::$classMap;
}, null, ClassLoader::class);
}