diff --git a/config/set/php70.php b/config/set/php70.php index f763f5b1410..2d5703b2acc 100644 --- a/config/set/php70.php +++ b/config/set/php70.php @@ -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, ]); }; diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index cab0c7384ff..96bde2ae9b0 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 403 Rules Overview +# 404 Rules Overview
@@ -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`.
+### 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'; + } + } +``` + +
+ ### 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]; + }); ```
diff --git a/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php b/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php new file mode 100644 index 00000000000..eed47ac4814 --- /dev/null +++ b/rules/Php70/Rector/StmtsAwareInterface/IfIssetToCoalescingRector.php @@ -0,0 +1,118 @@ +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> + */ + 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]; + } +} diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index b6ecec2b1ec..c97cd157515 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -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 */ diff --git a/vendor/autoload.php b/vendor/autoload.php index 20d4c1793c5..3a711f239f0 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit4237e9a11ab771a28d5408c9c41af241::getLoader(); +return ComposerAutoloaderInitffaf51aaa75e8d7fc0afb2a0fce8027f::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index ee3abf4b870..43db52b9266 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -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', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 7cd1110fe82..6aad0190816 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.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; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index fbed7fdfeaf..58ff3e6cd0c 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -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); }