mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit a8fdf00925e9b46f3372da01d90d52780d1e802a
a8fdf00925
[PHP 7.0] Add IfIssetToCoalescingRector (#3878)
This commit is contained in:
parent
39a43e0003
commit
5a424135e9
|
@ -18,6 +18,7 @@ use Rector\Php70\Rector\If_\IfToSpaceshipRector;
|
|||
use Rector\Php70\Rector\List_\EmptyListRector;
|
||||
use Rector\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector;
|
||||
use Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector;
|
||||
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
|
||||
use Rector\Php70\Rector\Switch_\ReduceMultipleDefaultSwitchRector;
|
||||
use Rector\Php70\Rector\Ternary\TernaryToNullCoalescingRector;
|
||||
use Rector\Php70\Rector\Ternary\TernaryToSpaceshipRector;
|
||||
|
@ -43,5 +44,6 @@ return static function (RectorConfig $rectorConfig) : void {
|
|||
ThisCallOnStaticMethodToStaticCallRector::class,
|
||||
BreakNotInLoopOrSwitchToReturnRector::class,
|
||||
RenameMktimeWithoutArgsToTimeRector::class,
|
||||
IfIssetToCoalescingRector::class,
|
||||
]);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# 403 Rules Overview
|
||||
# 404 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -34,7 +34,7 @@
|
|||
|
||||
- [Php56](#php56) (2)
|
||||
|
||||
- [Php70](#php70) (18)
|
||||
- [Php70](#php70) (19)
|
||||
|
||||
- [Php71](#php71) (9)
|
||||
|
||||
|
@ -4675,6 +4675,31 @@ Change typehint from `Exception` to `Throwable`.
|
|||
|
||||
<br>
|
||||
|
||||
### IfIssetToCoalescingRector
|
||||
|
||||
Change if with isset and return to coalesce
|
||||
|
||||
- class: [`Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector`](../rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
private $items = [];
|
||||
|
||||
public function resolve($key)
|
||||
{
|
||||
- if (isset($this->items[$key])) {
|
||||
- return $this->items[$key];
|
||||
- }
|
||||
-
|
||||
- return 'fallback value';
|
||||
+ return $this->items[$key] ?? 'fallback value';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### IfToSpaceshipRector
|
||||
|
||||
Changes if/else to spaceship <=> where useful
|
||||
|
@ -4682,20 +4707,14 @@ Changes if/else to spaceship <=> where useful
|
|||
- class: [`Rector\Php70\Rector\If_\IfToSpaceshipRector`](../rules/Php70/Rector/If_/IfToSpaceshipRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
usort($languages, function ($a, $b) {
|
||||
- if ($a[0] === $b[0]) {
|
||||
- return 0;
|
||||
- }
|
||||
usort($languages, function ($first, $second) {
|
||||
-if ($first[0] === $second[0]) {
|
||||
- return 0;
|
||||
-}
|
||||
-
|
||||
- return ($a[0] < $b[0]) ? 1 : -1;
|
||||
+ return $b[0] <=> $a[0];
|
||||
});
|
||||
}
|
||||
}
|
||||
-return ($first[0] < $second[0]) ? 1 : -1;
|
||||
+return $second[0] <=> $first[0];
|
||||
});
|
||||
```
|
||||
|
||||
<br>
|
||||
|
|
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php70\Rector\StmtsAwareInterface;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
||||
use PhpParser\Node\Expr\Isset_;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector\IfIssetToCoalescingRectorTest
|
||||
*/
|
||||
final class IfIssetToCoalescingRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Change if with isset and return to coalesce', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
private $items = [];
|
||||
|
||||
public function resolve($key)
|
||||
{
|
||||
if (isset($this->items[$key])) {
|
||||
return $this->items[$key];
|
||||
}
|
||||
|
||||
return 'fallback value';
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
private $items = [];
|
||||
|
||||
public function resolve($key)
|
||||
{
|
||||
return $this->items[$key] ?? 'fallback value';
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [StmtsAwareInterface::class];
|
||||
}
|
||||
/**
|
||||
* @param StmtsAwareInterface $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
foreach ($node->stmts as $key => $stmt) {
|
||||
if (!$stmt instanceof Return_) {
|
||||
continue;
|
||||
}
|
||||
if (!$stmt->expr instanceof Expr) {
|
||||
continue;
|
||||
}
|
||||
$previousStmt = $node->stmts[$key - 1] ?? null;
|
||||
if (!$previousStmt instanceof If_) {
|
||||
continue;
|
||||
}
|
||||
if (!$previousStmt->cond instanceof Isset_) {
|
||||
continue;
|
||||
}
|
||||
$ifOnlyStmt = $this->matchBareIfOnlyStmt($previousStmt);
|
||||
if (!$ifOnlyStmt instanceof Return_) {
|
||||
continue;
|
||||
}
|
||||
if (!$ifOnlyStmt->expr instanceof Expr) {
|
||||
continue;
|
||||
}
|
||||
$ifIsset = $previousStmt->cond;
|
||||
if (!$this->nodeComparator->areNodesEqual($ifOnlyStmt->expr, $ifIsset->vars[0])) {
|
||||
continue;
|
||||
}
|
||||
unset($node->stmts[$key - 1]);
|
||||
$stmt->expr = new Coalesce($ifOnlyStmt->expr, $stmt->expr);
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::NULL_COALESCE;
|
||||
}
|
||||
private function matchBareIfOnlyStmt(If_ $if) : ?Stmt
|
||||
{
|
||||
if ($if->else instanceof Else_) {
|
||||
return null;
|
||||
}
|
||||
if ($if->elseifs !== []) {
|
||||
return null;
|
||||
}
|
||||
if (\count($if->stmts) !== 1) {
|
||||
return null;
|
||||
}
|
||||
return $if->stmts[0];
|
||||
}
|
||||
}
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'f2348ae8c75cafec524ea972ff6af912e8ce7c11';
|
||||
public const PACKAGE_VERSION = 'a8fdf00925e9b46f3372da01d90d52780d1e802a';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-05-17 09:08:49';
|
||||
public const RELEASE_DATE = '2023-05-17 10:43:49';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit4237e9a11ab771a28d5408c9c41af241::getLoader();
|
||||
return ComposerAutoloaderInitffaf51aaa75e8d7fc0afb2a0fce8027f::getLoader();
|
||||
|
|
|
@ -2190,6 +2190,7 @@ return array(
|
|||
'Rector\\Php70\\Rector\\List_\\EmptyListRector' => $baseDir . '/rules/Php70/Rector/List_/EmptyListRector.php',
|
||||
'Rector\\Php70\\Rector\\MethodCall\\ThisCallOnStaticMethodToStaticCallRector' => $baseDir . '/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php',
|
||||
'Rector\\Php70\\Rector\\StaticCall\\StaticCallOnNonStaticToInstanceCallRector' => $baseDir . '/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php',
|
||||
'Rector\\Php70\\Rector\\StmtsAwareInterface\\IfIssetToCoalescingRector' => $baseDir . '/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php',
|
||||
'Rector\\Php70\\Rector\\Switch_\\ReduceMultipleDefaultSwitchRector' => $baseDir . '/rules/Php70/Rector/Switch_/ReduceMultipleDefaultSwitchRector.php',
|
||||
'Rector\\Php70\\Rector\\Ternary\\TernaryToNullCoalescingRector' => $baseDir . '/rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php',
|
||||
'Rector\\Php70\\Rector\\Ternary\\TernaryToSpaceshipRector' => $baseDir . '/rules/Php70/Rector/Ternary/TernaryToSpaceshipRector.php',
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit4237e9a11ab771a28d5408c9c41af241
|
||||
class ComposerAutoloaderInitffaf51aaa75e8d7fc0afb2a0fce8027f
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInit4237e9a11ab771a28d5408c9c41af241
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit4237e9a11ab771a28d5408c9c41af241', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitffaf51aaa75e8d7fc0afb2a0fce8027f', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit4237e9a11ab771a28d5408c9c41af241', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitffaf51aaa75e8d7fc0afb2a0fce8027f', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit4237e9a11ab771a28d5408c9c41af241::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitffaf51aaa75e8d7fc0afb2a0fce8027f::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit4237e9a11ab771a28d5408c9c41af241::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitffaf51aaa75e8d7fc0afb2a0fce8027f::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit4237e9a11ab771a28d5408c9c41af241
|
||||
class ComposerStaticInitffaf51aaa75e8d7fc0afb2a0fce8027f
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -2432,6 +2432,7 @@ class ComposerStaticInit4237e9a11ab771a28d5408c9c41af241
|
|||
'Rector\\Php70\\Rector\\List_\\EmptyListRector' => __DIR__ . '/../..' . '/rules/Php70/Rector/List_/EmptyListRector.php',
|
||||
'Rector\\Php70\\Rector\\MethodCall\\ThisCallOnStaticMethodToStaticCallRector' => __DIR__ . '/../..' . '/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php',
|
||||
'Rector\\Php70\\Rector\\StaticCall\\StaticCallOnNonStaticToInstanceCallRector' => __DIR__ . '/../..' . '/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php',
|
||||
'Rector\\Php70\\Rector\\StmtsAwareInterface\\IfIssetToCoalescingRector' => __DIR__ . '/../..' . '/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php',
|
||||
'Rector\\Php70\\Rector\\Switch_\\ReduceMultipleDefaultSwitchRector' => __DIR__ . '/../..' . '/rules/Php70/Rector/Switch_/ReduceMultipleDefaultSwitchRector.php',
|
||||
'Rector\\Php70\\Rector\\Ternary\\TernaryToNullCoalescingRector' => __DIR__ . '/../..' . '/rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php',
|
||||
'Rector\\Php70\\Rector\\Ternary\\TernaryToSpaceshipRector' => __DIR__ . '/../..' . '/rules/Php70/Rector/Ternary/TernaryToSpaceshipRector.php',
|
||||
|
@ -3106,9 +3107,9 @@ class ComposerStaticInit4237e9a11ab771a28d5408c9c41af241
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit4237e9a11ab771a28d5408c9c41af241::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit4237e9a11ab771a28d5408c9c41af241::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit4237e9a11ab771a28d5408c9c41af241::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitffaf51aaa75e8d7fc0afb2a0fce8027f::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitffaf51aaa75e8d7fc0afb2a0fce8027f::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitffaf51aaa75e8d7fc0afb2a0fce8027f::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue