diff --git a/config/set/code-quality.php b/config/set/code-quality.php index 8038b05b837c..60e686b01191 100644 --- a/config/set/code-quality.php +++ b/config/set/code-quality.php @@ -69,6 +69,7 @@ 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 @@ '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]); }; diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 8b95129f29ae..ef1c4daa5922 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 353 Rules Overview +# 355 Rules Overview
@@ -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()`
+### 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); + } + } +``` + +
+ ### 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; ```
@@ -6738,6 +6761,25 @@ Add strict type declaration based on returned constants
+### 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; + } + } +``` + +
+ ### ReturnTypeFromStrictNativeCallRector Add strict return type based native function or class method return diff --git a/rules/CodeQuality/Rector/Ternary/NumberCompareToMaxFuncCallRector.php b/rules/CodeQuality/Rector/Ternary/NumberCompareToMaxFuncCallRector.php new file mode 100644 index 000000000000..c76839ef7c30 --- /dev/null +++ b/rules/CodeQuality/Rector/Ternary/NumberCompareToMaxFuncCallRector.php @@ -0,0 +1,91 @@ + 100 ? $value : 100; + } +} +CODE_SAMPLE +, <<<'CODE_SAMPLE' +class SomeClass +{ + public function run($value) + { + return max($value, 100); + } +} +CODE_SAMPLE +)]); + } + /** + * @return array> + */ + 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(); + } +} diff --git a/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php b/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php index 9da1c350c559..91801477f5bb 100644 --- a/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php +++ b/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php @@ -87,15 +87,15 @@ public function refactor(Node $node) : ?Node } 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) { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php index 8f690043abdc..a28a4181ac24 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php @@ -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; diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 3cc06271e542..0b0f15c53833 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 = '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 */ diff --git a/vendor/autoload.php b/vendor/autoload.php index b84cdcb9e507..0f1c34c09d6f 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInite42025df4221b1aa4e90629306b7a463::getLoader(); +return ComposerAutoloaderInit38b31399dff74dd2a0290c76af352a7d::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index a43a3e8b3f73..88e13101d79a 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -1084,6 +1084,7 @@ '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', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 764d9994051a..15cc588d3450 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInite42025df4221b1aa4e90629306b7a463 +class ComposerAutoloaderInit38b31399dff74dd2a0290c76af352a7d { private static $loader; @@ -22,17 +22,17 @@ public static function getLoader() 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; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 142e44914500..c41c9071a5db 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -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); }