mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit 6db4c0e0ae74867ec12b2a4fa7cf934a4a60a742
6db4c0e0ae
[Php74] Add ParenthesizeNestedTernaryRector (#2859)
This commit is contained in:
parent
3a10283d3a
commit
c152c5642c
|
@ -18,6 +18,7 @@ use Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector;
|
|||
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
|
||||
use Rector\Php74\Rector\Property\TypedPropertyRector;
|
||||
use Rector\Php74\Rector\StaticCall\ExportToReflectionFunctionRector;
|
||||
use Rector\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector;
|
||||
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rule(TypedPropertyRector::class);
|
||||
|
@ -42,4 +43,5 @@ return static function (RectorConfig $rectorConfig) : void {
|
|||
$rectorConfig->rule(RestoreDefaultNullToNullableTypePropertyRector::class);
|
||||
$rectorConfig->rule(CurlyToSquareBracketArrayStringRector::class);
|
||||
$rectorConfig->rule(MoneyFormatToNumberFormatRector::class);
|
||||
$rectorConfig->rule(ParenthesizeNestedTernaryRector::class);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# 400 Rules Overview
|
||||
# 401 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -44,7 +44,7 @@
|
|||
|
||||
- [Php73](#php73) (9)
|
||||
|
||||
- [Php74](#php74) (14)
|
||||
- [Php74](#php74) (15)
|
||||
|
||||
- [Php80](#php80) (19)
|
||||
|
||||
|
@ -5694,6 +5694,19 @@ Use null coalescing operator ??=
|
|||
|
||||
<br>
|
||||
|
||||
### ParenthesizeNestedTernaryRector
|
||||
|
||||
Add parentheses to nested ternary
|
||||
|
||||
- class: [`Rector\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector`](../rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php)
|
||||
|
||||
```diff
|
||||
-$value = $a ? $b : $a ?: null;
|
||||
+$value = ($a ? $b : $a) ?: null;
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RealToFloatTypeCastRector
|
||||
|
||||
Change deprecated (real) to (float)
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php74\Rector\Ternary;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Php74\Tokenizer\ParenthesizedNestedTernaryAnalyzer;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @changelog https://www.php.net/manual/en/migration74.deprecated.php
|
||||
* @changelog https://3v4l.org/vhdlJ
|
||||
* @see \Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\ParenthesizeNestedTernaryRectorTest
|
||||
*/
|
||||
final class ParenthesizeNestedTernaryRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Php74\Tokenizer\ParenthesizedNestedTernaryAnalyzer
|
||||
*/
|
||||
private $parenthesizedNestedTernaryAnalyzer;
|
||||
public function __construct(ParenthesizedNestedTernaryAnalyzer $parenthesizedNestedTernaryAnalyzer)
|
||||
{
|
||||
$this->parenthesizedNestedTernaryAnalyzer = $parenthesizedNestedTernaryAnalyzer;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::DEPRECATE_NESTED_TERNARY;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add parentheses to nested ternary', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
$value = $a ? $b : $a ?: null;
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
$value = ($a ? $b : $a) ?: null;
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Ternary::class];
|
||||
}
|
||||
/**
|
||||
* @param Ternary $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$parentNode instanceof Ternary) {
|
||||
return null;
|
||||
}
|
||||
if ($this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->file, $parentNode)) {
|
||||
return null;
|
||||
}
|
||||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php74\Tokenizer;
|
||||
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use Rector\Core\ValueObject\Application\File;
|
||||
final class ParenthesizedNestedTernaryAnalyzer
|
||||
{
|
||||
public function isParenthesized(File $file, Ternary $ternary) : bool
|
||||
{
|
||||
$oldTokens = $file->getOldTokens();
|
||||
$startTokenPos = $ternary->getStartTokenPos();
|
||||
return isset($oldTokens[$startTokenPos]) && $oldTokens[$startTokenPos] === '(';
|
||||
}
|
||||
}
|
|
@ -17,12 +17,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '86ccf3f6854c78c6b7b06b8471d922724cb6542b';
|
||||
public const PACKAGE_VERSION = '6db4c0e0ae74867ec12b2a4fa7cf934a4a60a742';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-08-30 16:15:10';
|
||||
public const RELEASE_DATE = '2022-08-31 09:04:33';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -320,6 +320,10 @@ final class PhpVersionFeature
|
|||
* @var int
|
||||
*/
|
||||
public const EXPORT_TO_REFLECTION_FUNCTION = \Rector\Core\ValueObject\PhpVersion::PHP_74;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public const DEPRECATE_NESTED_TERNARY = \Rector\Core\ValueObject\PhpVersion::PHP_74;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit9925dc93bdd89dc64690fe2737f1c505::getLoader();
|
||||
return ComposerAutoloaderInitc1cc5cd24cd4152a38120c2b0c19c808::getLoader();
|
||||
|
|
|
@ -2276,7 +2276,9 @@ return array(
|
|||
'Rector\\Php74\\Rector\\Property\\RestoreDefaultNullToNullableTypePropertyRector' => $baseDir . '/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php',
|
||||
'Rector\\Php74\\Rector\\Property\\TypedPropertyRector' => $baseDir . '/rules/Php74/Rector/Property/TypedPropertyRector.php',
|
||||
'Rector\\Php74\\Rector\\StaticCall\\ExportToReflectionFunctionRector' => $baseDir . '/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php',
|
||||
'Rector\\Php74\\Rector\\Ternary\\ParenthesizeNestedTernaryRector' => $baseDir . '/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php',
|
||||
'Rector\\Php74\\Tokenizer\\FollowedByCurlyBracketAnalyzer' => $baseDir . '/rules/Php74/Tokenizer/FollowedByCurlyBracketAnalyzer.php',
|
||||
'Rector\\Php74\\Tokenizer\\ParenthesizedNestedTernaryAnalyzer' => $baseDir . '/rules/Php74/Tokenizer/ParenthesizedNestedTernaryAnalyzer.php',
|
||||
'Rector\\Php74\\TypeAnalyzer\\ObjectTypeAnalyzer' => $baseDir . '/rules/Php74/TypeAnalyzer/ObjectTypeAnalyzer.php',
|
||||
'Rector\\Php80\\AttributeDecorator\\JMSAccessTypeAttributeDecorator' => $baseDir . '/rules/Php80/AttributeDecorator/JMSAccessTypeAttributeDecorator.php',
|
||||
'Rector\\Php80\\AttributeDecorator\\SensioParamConverterAttributeDecorator' => $baseDir . '/rules/Php80/AttributeDecorator/SensioParamConverterAttributeDecorator.php',
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit9925dc93bdd89dc64690fe2737f1c505
|
||||
class ComposerAutoloaderInitc1cc5cd24cd4152a38120c2b0c19c808
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,19 +22,19 @@ class ComposerAutoloaderInit9925dc93bdd89dc64690fe2737f1c505
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit9925dc93bdd89dc64690fe2737f1c505', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitc1cc5cd24cd4152a38120c2b0c19c808', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit9925dc93bdd89dc64690fe2737f1c505', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitc1cc5cd24cd4152a38120c2b0c19c808', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit9925dc93bdd89dc64690fe2737f1c505::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitc1cc5cd24cd4152a38120c2b0c19c808::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit9925dc93bdd89dc64690fe2737f1c505::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInitc1cc5cd24cd4152a38120c2b0c19c808::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire9925dc93bdd89dc64690fe2737f1c505($fileIdentifier, $file);
|
||||
composerRequirec1cc5cd24cd4152a38120c2b0c19c808($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
|
@ -46,7 +46,7 @@ class ComposerAutoloaderInit9925dc93bdd89dc64690fe2737f1c505
|
|||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequire9925dc93bdd89dc64690fe2737f1c505($fileIdentifier, $file)
|
||||
function composerRequirec1cc5cd24cd4152a38120c2b0c19c808($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit9925dc93bdd89dc64690fe2737f1c505
|
||||
class ComposerStaticInitc1cc5cd24cd4152a38120c2b0c19c808
|
||||
{
|
||||
public static $files = array (
|
||||
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
|
||||
|
@ -2556,7 +2556,9 @@ class ComposerStaticInit9925dc93bdd89dc64690fe2737f1c505
|
|||
'Rector\\Php74\\Rector\\Property\\RestoreDefaultNullToNullableTypePropertyRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php',
|
||||
'Rector\\Php74\\Rector\\Property\\TypedPropertyRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/Property/TypedPropertyRector.php',
|
||||
'Rector\\Php74\\Rector\\StaticCall\\ExportToReflectionFunctionRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php',
|
||||
'Rector\\Php74\\Rector\\Ternary\\ParenthesizeNestedTernaryRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php',
|
||||
'Rector\\Php74\\Tokenizer\\FollowedByCurlyBracketAnalyzer' => __DIR__ . '/../..' . '/rules/Php74/Tokenizer/FollowedByCurlyBracketAnalyzer.php',
|
||||
'Rector\\Php74\\Tokenizer\\ParenthesizedNestedTernaryAnalyzer' => __DIR__ . '/../..' . '/rules/Php74/Tokenizer/ParenthesizedNestedTernaryAnalyzer.php',
|
||||
'Rector\\Php74\\TypeAnalyzer\\ObjectTypeAnalyzer' => __DIR__ . '/../..' . '/rules/Php74/TypeAnalyzer/ObjectTypeAnalyzer.php',
|
||||
'Rector\\Php80\\AttributeDecorator\\JMSAccessTypeAttributeDecorator' => __DIR__ . '/../..' . '/rules/Php80/AttributeDecorator/JMSAccessTypeAttributeDecorator.php',
|
||||
'Rector\\Php80\\AttributeDecorator\\SensioParamConverterAttributeDecorator' => __DIR__ . '/../..' . '/rules/Php80/AttributeDecorator/SensioParamConverterAttributeDecorator.php',
|
||||
|
@ -3170,9 +3172,9 @@ class ComposerStaticInit9925dc93bdd89dc64690fe2737f1c505
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit9925dc93bdd89dc64690fe2737f1c505::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit9925dc93bdd89dc64690fe2737f1c505::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit9925dc93bdd89dc64690fe2737f1c505::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitc1cc5cd24cd4152a38120c2b0c19c808::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitc1cc5cd24cd4152a38120c2b0c19c808::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitc1cc5cd24cd4152a38120c2b0c19c808::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue