Updated Rector to commit 093bcb63a19db3fcdab8264897c584212d755463

093bcb63a1 [CodeQuality] Add NumberCompareToMaxFuncCallRector (#4914)
This commit is contained in:
Tomas Votruba 2023-09-05 13:21:31 +00:00
parent f4213d1e8b
commit 2ae279bf07
10 changed files with 161 additions and 25 deletions

View File

@ -69,6 +69,7 @@ use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperator
use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector;
use Rector\CodeQuality\Rector\Switch_\SwitchTrueToIfRector;
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
use Rector\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector;
use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector;
use Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector;
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
@ -107,5 +108,5 @@ return static function (RectorConfig $rectorConfig) : void {
'mbstrrpos' => 'mb_strrpos',
'mbsubstr' => 'mb_substr',
]);
$rectorConfig->rules([CombinedAssignRector::class, SimplifyEmptyArrayCheckRector::class, ReplaceMultipleBooleanNotRector::class, ForeachToInArrayRector::class, SimplifyForeachToCoalescingRector::class, SimplifyFuncGetArgsCountRector::class, SimplifyInArrayValuesRector::class, SimplifyStrposLowerRector::class, GetClassToInstanceOfRector::class, SimplifyArraySearchRector::class, SimplifyConditionsRector::class, SimplifyIfNotNullReturnRector::class, SimplifyIfReturnBoolRector::class, SimplifyUselessVariableRector::class, UnnecessaryTernaryExpressionRector::class, RemoveExtraParametersRector::class, SimplifyDeMorganBinaryRector::class, SimplifyTautologyTernaryRector::class, SingleInArrayToCompareRector::class, SimplifyIfElseToTernaryRector::class, JoinStringConcatRector::class, ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class, ExplicitBoolCompareRector::class, CombineIfRector::class, UseIdenticalOverEqualWithSameTypeRector::class, SimplifyBoolIdenticalTrueRector::class, SimplifyRegexPatternRector::class, BooleanNotIdenticalToNotIdenticalRector::class, StrvalToTypeCastRector::class, FloatvalToTypeCastRector::class, CallableThisArrayToAnonymousFunctionRector::class, AndAssignsToSeparateLinesRector::class, CompactToVariablesRector::class, CompleteDynamicPropertiesRector::class, IsAWithStringWithThirdArgumentRector::class, StrlenZeroToIdenticalEmptyStringRector::class, ThrowWithPreviousExceptionRector::class, RemoveSoleValueSprintfRector::class, ShortenElseIfRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class, IntvalToTypeCastRector::class, BoolvalToTypeCastRector::class, ArrayKeyExistsTernaryThenValueToCoalescingRector::class, AbsolutizeRequireAndIncludePathRector::class, ChangeArrayPushToArrayAssignRector::class, ForRepeatedCountToOwnVariableRector::class, ForeachItemsAssignToEmptyArrayToAssignRector::class, InlineIfToExplicitIfRector::class, UnusedForeachValueToArrayKeysRector::class, CommonNotEqualRector::class, SetTypeToCastRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class, IssetOnPropertyObjectToPropertyExistsRector::class, NewStaticToNewSelfRector::class, UnwrapSprintfOneArgumentRector::class, SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SimplifyIfNullableReturnRector::class, FuncGetArgsToVariadicParamRector::class, CallUserFuncToMethodCallRector::class, CallUserFuncWithArrowFunctionToInlineRector::class, CountArrayToEmptyArrayComparisonRector::class, FlipTypeControlToUseExclusiveTypeRector::class, InlineArrayReturnAssignRector::class, InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, InlineConstructorDefaultToPropertyRector::class, TernaryEmptyArrayArrayDimFetchToCoalesceRector::class, OptionalParametersAfterRequiredRector::class, SimplifyEmptyCheckOnEmptyArrayRector::class, SwitchTrueToIfRector::class, CleanupUnneededNullsafeOperatorRector::class, DisallowedEmptyRuleFixerRector::class, ConvertStaticPrivateConstantToSelfRector::class, LocallyCalledStaticMethodToNonStaticRector::class]);
$rectorConfig->rules([CombinedAssignRector::class, SimplifyEmptyArrayCheckRector::class, ReplaceMultipleBooleanNotRector::class, ForeachToInArrayRector::class, SimplifyForeachToCoalescingRector::class, SimplifyFuncGetArgsCountRector::class, SimplifyInArrayValuesRector::class, SimplifyStrposLowerRector::class, GetClassToInstanceOfRector::class, SimplifyArraySearchRector::class, SimplifyConditionsRector::class, SimplifyIfNotNullReturnRector::class, SimplifyIfReturnBoolRector::class, SimplifyUselessVariableRector::class, UnnecessaryTernaryExpressionRector::class, RemoveExtraParametersRector::class, SimplifyDeMorganBinaryRector::class, SimplifyTautologyTernaryRector::class, SingleInArrayToCompareRector::class, SimplifyIfElseToTernaryRector::class, JoinStringConcatRector::class, ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class, ExplicitBoolCompareRector::class, CombineIfRector::class, UseIdenticalOverEqualWithSameTypeRector::class, SimplifyBoolIdenticalTrueRector::class, SimplifyRegexPatternRector::class, BooleanNotIdenticalToNotIdenticalRector::class, StrvalToTypeCastRector::class, FloatvalToTypeCastRector::class, CallableThisArrayToAnonymousFunctionRector::class, AndAssignsToSeparateLinesRector::class, CompactToVariablesRector::class, CompleteDynamicPropertiesRector::class, IsAWithStringWithThirdArgumentRector::class, StrlenZeroToIdenticalEmptyStringRector::class, ThrowWithPreviousExceptionRector::class, RemoveSoleValueSprintfRector::class, ShortenElseIfRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class, IntvalToTypeCastRector::class, BoolvalToTypeCastRector::class, ArrayKeyExistsTernaryThenValueToCoalescingRector::class, AbsolutizeRequireAndIncludePathRector::class, ChangeArrayPushToArrayAssignRector::class, ForRepeatedCountToOwnVariableRector::class, ForeachItemsAssignToEmptyArrayToAssignRector::class, InlineIfToExplicitIfRector::class, UnusedForeachValueToArrayKeysRector::class, CommonNotEqualRector::class, SetTypeToCastRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class, IssetOnPropertyObjectToPropertyExistsRector::class, NewStaticToNewSelfRector::class, UnwrapSprintfOneArgumentRector::class, SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SimplifyIfNullableReturnRector::class, FuncGetArgsToVariadicParamRector::class, CallUserFuncToMethodCallRector::class, CallUserFuncWithArrowFunctionToInlineRector::class, CountArrayToEmptyArrayComparisonRector::class, FlipTypeControlToUseExclusiveTypeRector::class, InlineArrayReturnAssignRector::class, InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, InlineConstructorDefaultToPropertyRector::class, TernaryEmptyArrayArrayDimFetchToCoalesceRector::class, OptionalParametersAfterRequiredRector::class, SimplifyEmptyCheckOnEmptyArrayRector::class, SwitchTrueToIfRector::class, CleanupUnneededNullsafeOperatorRector::class, DisallowedEmptyRuleFixerRector::class, ConvertStaticPrivateConstantToSelfRector::class, LocallyCalledStaticMethodToNonStaticRector::class, NumberCompareToMaxFuncCallRector::class]);
};

View File

@ -1,4 +1,4 @@
# 353 Rules Overview
# 355 Rules Overview
<br>
@ -6,7 +6,7 @@
- [Arguments](#arguments) (4)
- [CodeQuality](#codequality) (70)
- [CodeQuality](#codequality) (71)
- [CodingStyle](#codingstyle) (30)
@ -54,7 +54,7 @@
- [Transform](#transform) (22)
- [TypeDeclaration](#typedeclaration) (41)
- [TypeDeclaration](#typedeclaration) (42)
- [Visibility](#visibility) (3)
@ -862,6 +862,25 @@ Change unsafe new `static()` to new `self()`
<br>
### NumberCompareToMaxFuncCallRector
Ternary number compare to `max()` call
- class: [`Rector\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector`](../rules/CodeQuality/Rector/Ternary/NumberCompareToMaxFuncCallRector.php)
```diff
class SomeClass
{
public function run($value)
{
- return $value > 100 ? $value : 100;
+ return max($value, 100);
}
}
```
<br>
### OptionalParametersAfterRequiredRector
Move required parameters after optional ones
@ -993,8 +1012,9 @@ Simplify bool value compare to true or false
public function run(bool $value, string $items)
{
- $match = in_array($value, $items, TRUE) === TRUE;
- $match = in_array($value, $items, TRUE) !== FALSE;
+ $match = in_array($value, $items, TRUE);
- $match = in_array($value, $items, TRUE) !== FALSE;
+ $match = in_array($value, $items, TRUE);
}
}
@ -1714,8 +1734,11 @@ Convert enscaped {$string} to more readable sprintf or concat, if no mask is use
- class: [`Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector`](../rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php)
```diff
-echo "Unsupported format {$format}";
+echo sprintf('Unsupported format %s', $format);
-echo "Unsupported format {$format} - use another";
+echo sprintf('Unsupported format %s - use another', $format);
-echo "Try {$allowed}";
+echo 'Try ' . $allowed;
```
<br>
@ -6738,6 +6761,25 @@ Add strict type declaration based on returned constants
<br>
### ReturnTypeFromStrictFluentReturnRector
Add return type from strict return `$this`
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php)
```diff
final class SomeClass
{
- public function run()
+ public function run(): self
{
return $this;
}
}
```
<br>
### ReturnTypeFromStrictNativeCallRector
Add strict return type based native function or class method return

View File

@ -0,0 +1,91 @@
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\Ternary;
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\Ternary;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector\NumberCompareToMaxFuncCallRectorTest
*/
final class NumberCompareToMaxFuncCallRector extends AbstractRector
{
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Ternary number compare to max() call', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
{
return $value > 100 ? $value : 100;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
{
return max($value, 100);
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Ternary::class];
}
/**
* @param Ternary $node
*/
public function refactor(Node $node) : ?Node
{
if (!$node->cond instanceof BinaryOp) {
return null;
}
$binaryOp = $node->cond;
if (!$this->areIntegersCompared($binaryOp)) {
return null;
}
if ($binaryOp instanceof Smaller || $binaryOp instanceof SmallerOrEqual) {
if (!$this->nodeComparator->areNodesEqual($binaryOp->left, $node->else)) {
return null;
}
if (!$this->nodeComparator->areNodesEqual($binaryOp->right, $node->if)) {
return null;
}
return $this->nodeFactory->createFuncCall('max', [$node->if, $node->else]);
}
if ($binaryOp instanceof Greater || $binaryOp instanceof GreaterOrEqual) {
if (!$this->nodeComparator->areNodesEqual($binaryOp->left, $node->if)) {
return null;
}
if (!$this->nodeComparator->areNodesEqual($binaryOp->right, $node->else)) {
return null;
}
return $this->nodeFactory->createFuncCall('max', [$node->if, $node->else]);
}
return null;
}
private function areIntegersCompared(BinaryOp $binaryOp) : bool
{
$leftType = $this->getType($binaryOp->left);
if (!$leftType->isInteger()->yes()) {
return \false;
}
$rightType = $this->getType($binaryOp->right);
return $rightType->isInteger()->yes();
}
}

View File

@ -87,15 +87,15 @@ CODE_SAMPLE
}
return $this->nodeFactory->createClassConstFetch($className, $enumCaseName);
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::ENUM;
}
private function isEnumConstant(string $className, string $constant) : bool
{
$classReflection = $this->reflectionProvider->getClass($className);
return $classReflection->hasConstant($constant);
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::ENUM;
}
private function refactorGetKeyMethodCall(MethodCall $methodCall) : ?ClassConstFetch
{
if (!$methodCall->var instanceof StaticCall) {

View File

@ -3,20 +3,20 @@
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\AssignOp\Coalesce as AssignOpCoalesce;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeTraverser;
use Rector\Core\Rector\AbstractRector;

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '8c5cfafd4429765fecd1f3db9546cdb9abef080f';
public const PACKAGE_VERSION = '093bcb63a19db3fcdab8264897c584212d755463';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-09-05 13:41:45';
public const RELEASE_DATE = '2023-09-05 13:17:38';
/**
* @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 ComposerAutoloaderInite42025df4221b1aa4e90629306b7a463::getLoader();
return ComposerAutoloaderInit38b31399dff74dd2a0290c76af352a7d::getLoader();

View File

@ -1084,6 +1084,7 @@ return array(
'Rector\\CodeQuality\\Rector\\Switch_\\SingularSwitchToIfRector' => $baseDir . '/rules/CodeQuality/Rector/Switch_/SingularSwitchToIfRector.php',
'Rector\\CodeQuality\\Rector\\Switch_\\SwitchTrueToIfRector' => $baseDir . '/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\ArrayKeyExistsTernaryThenValueToCoalescingRector' => $baseDir . '/rules/CodeQuality/Rector/Ternary/ArrayKeyExistsTernaryThenValueToCoalescingRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\NumberCompareToMaxFuncCallRector' => $baseDir . '/rules/CodeQuality/Rector/Ternary/NumberCompareToMaxFuncCallRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\SimplifyTautologyTernaryRector' => $baseDir . '/rules/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\SwitchNegatedTernaryRector' => $baseDir . '/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\TernaryEmptyArrayArrayDimFetchToCoalesceRector' => $baseDir . '/rules/CodeQuality/Rector/Ternary/TernaryEmptyArrayArrayDimFetchToCoalesceRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInite42025df4221b1aa4e90629306b7a463
class ComposerAutoloaderInit38b31399dff74dd2a0290c76af352a7d
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInite42025df4221b1aa4e90629306b7a463
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInite42025df4221b1aa4e90629306b7a463', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit38b31399dff74dd2a0290c76af352a7d', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInite42025df4221b1aa4e90629306b7a463', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit38b31399dff74dd2a0290c76af352a7d', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInite42025df4221b1aa4e90629306b7a463::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit38b31399dff74dd2a0290c76af352a7d::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInite42025df4221b1aa4e90629306b7a463::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit38b31399dff74dd2a0290c76af352a7d::$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 ComposerStaticInite42025df4221b1aa4e90629306b7a463
class ComposerStaticInit38b31399dff74dd2a0290c76af352a7d
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1304,6 +1304,7 @@ class ComposerStaticInite42025df4221b1aa4e90629306b7a463
'Rector\\CodeQuality\\Rector\\Switch_\\SingularSwitchToIfRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Switch_/SingularSwitchToIfRector.php',
'Rector\\CodeQuality\\Rector\\Switch_\\SwitchTrueToIfRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\ArrayKeyExistsTernaryThenValueToCoalescingRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Ternary/ArrayKeyExistsTernaryThenValueToCoalescingRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\NumberCompareToMaxFuncCallRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Ternary/NumberCompareToMaxFuncCallRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\SimplifyTautologyTernaryRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\SwitchNegatedTernaryRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php',
'Rector\\CodeQuality\\Rector\\Ternary\\TernaryEmptyArrayArrayDimFetchToCoalesceRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Ternary/TernaryEmptyArrayArrayDimFetchToCoalesceRector.php',
@ -2611,9 +2612,9 @@ class ComposerStaticInite42025df4221b1aa4e90629306b7a463
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInite42025df4221b1aa4e90629306b7a463::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInite42025df4221b1aa4e90629306b7a463::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInite42025df4221b1aa4e90629306b7a463::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit38b31399dff74dd2a0290c76af352a7d::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit38b31399dff74dd2a0290c76af352a7d::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit38b31399dff74dd2a0290c76af352a7d::$classMap;
}, null, ClassLoader::class);
}