github rectorphp/rector 2.9.0
Released Rector 2.9.0

41 minutes ago

Leaner rule set and safer rules ๐Ÿงน

This release removes ~100 rules deprecated in past releases. They already threw on use, were part of no set and were rarely used. If your rector.php still registers one of them, remove it from the config. The goal stays the same: a smaller set of valuable, reliable rules for everyday upgrades.

The rest of the release is about safer changes - more than 30 rules now skip or keep code where the change could alter behavior: side effects, evaluation order, loose comparisons, fall-through cases, nullable types and more.


New Features ๐Ÿฅณ

  • [CodeQuality] Add NarrowPreviousExceptionCodeArgRector, part of code-quality and named-args sets (#9052)
 try {
     $someCode = 1;
 } catch (Throwable $throwable) {
-    throw new \RuntimeException('ups', $throwable->getCode(), $throwable);
+    throw new \RuntimeException('ups', previous: $throwable);
 }
  • [Php84] Handle property fetch, static access, array access and invocation on NewMethodCallWithoutParenthesesRector (#9009, #9010) Thanks @alexndlm
-$message = (new Regex('some-pattern'))->message;
-$constant = (new Request())::METHOD_GET;
-$value = (new ArrayObject($items))['key'];
-$result = (new Invokable())();
+$message = new Regex('some-pattern')->message;
+$constant = new Request()::METHOD_GET;
+$value = new ArrayObject($items)['key'];
+$result = new Invokable()();
  • [Php71] Convert partial list() destruct in ListToArrayDestructRector (#9028)
-list(, $instanceId) = explode(':', $rate->id);
+[, $instanceId] = explode(':', $rate->id);
  • [TypeDeclaration] Allow void on final methods of non-final classes in AddVoidReturnTypeWhereNoReturnRector (#9051)
 abstract class SomeClass
 {
-    final protected function run()
+    final protected function run(): void
     {
         echo 1;
     }
 }
  • [TypeDeclaration] Use object return type for (object) cast in ReturnTypeFromReturnCastRector (#9021)
-public function run($data): \stdClass
+public function run($data): object
 {
     return (object) $data;
 }
  • [CodeQuality] Drop redundant bool cast under negation in UnnecessaryTernaryExpressionRector (#9024)
-return $order ? false : true;
+return !$order;
  • [dx] Report unnecessary skips in parallel runs too - ->reportUnusedSkips() now works with the default parallel run, not only with --debug or withoutParallel() (#8957)

Bugfixes ๐Ÿ›

Core

  • Keep the narrowed scope of a ternary branch when a changed node is refreshed (#9007) Thanks @SanderMuller
  • Fix class scope refresh skipping class attributes when class node is already in class (#9004) Thanks @APX403
  • [Core] Skip classes unknown to reflection instead of crashing in fetchClassLikeReflection() (#8965) Thanks @hmennen90
  • [Caching] Fix removed skip not invalidating cache after --clear-cache run (#8934)
  • [Printer] Wrap compound assign on the left of instanceof and binary ops (#9005) Thanks @odolbeau
  • [Php86] Handle crash on partial function application on PHP 8.6 scope set attribute (#9013)

DeadCode

  • Keep side effects of assigned value in RemoveUnusedPrivatePropertyRector (#9025)
  • Keep evaluation order of side effects and keep clone statement in RemoveDeadStmtRector (#9023, #9044)
  • Keep non-trailing constructor param in RemoveUnusedPromotedPropertyRector (#9022)
  • Keep empty constructor of extendable class in RemoveEmptyClassMethodRector (#9036)
  • Keep loops whose condition changes state in RemoveDeadLoopRector (#9041)
  • Keep empty branch that guards later branches in RemoveDeadIfBlockRector (#9042)
  • Skip case reached by fall-through in RemoveDuplicatedCaseInSwitchRector (#9020)
  • Skip BooleanAnd in trait on RemoveAlwaysTrueIfConditionRector (#9003) Thanks @odolbeau
  • Keep PHP_VERSION_ID check that can fail on the minimum version in RemovePhpVersionIdCheckRector (#9054)
  • Compare loosely on == and != in UnwrapFutureCompatibleIfPhpVersionRector (#9053)

CodeQuality

  • Invert ordering comparisons only on int operands (#9059)
  • Skip uninitialized typed array property in SimplifyEmptyCheckOnEmptyArrayRector (#9060)
  • Check negated operand type in BooleanNotIdenticalToNotIdenticalRector (#9043)
  • Keep original class name in InlineIsAInstanceOfRector (#9032)
  • Skip self-assign result in SimplifyBoolIdenticalTrueRector (#9029)
  • Keep loose comparison in SingularSwitchToIfRector (#9019)
  • Unwrap only percent-free string literal in UnwrapSprintfOneArgumentRector (#9018)
  • Preserve assignment parentheses in NegatedAndsToPositiveOrsRector (#9011)

CodingStyle, TypeDeclaration, EarlyReturn, Privatization

  • [CodingStyle] Keep string callable global when shadowed in FunctionFirstClassCallableRector (#9057)
  • [CodingStyle] Require object in ClosureFromCallableToFirstClassCallableRector (#9045)
  • [TypeDeclaration] Ignore docblock-narrowed args in AddMethodCallBasedStrictParamTypeRector (#9048)
  • [EarlyReturn] Skip several ifs assigning the returned variable in PreparedValueToEarlyReturnRector (#9033)
  • [Privatization] Fix invalid private protected(set) on asymmetric visibility property on PrivatizeFinalClassPropertyRector (#8935)

PHP sets

  • [Php86] Fold to clamp() only when min <= max is proven in MinMaxToClampRector (#9058)
  • [Php85] Fix negative value on ChrArgModuloRector (#8963)
  • [Php81] Skip array returned as array type in ArrayToFirstClassCallableRector (#9035)
  • [Php80] Keep property type safe in ClassPropertyAssignToConstructorPromotionRector (#9049)
  • [Php80] Skip int cast of string subject in ChangeSwitchToMatchRector (#9056)
  • [Php80] Map empty operator to < in VersionCompareFuncCallArgumentRector (#9055)
  • [Php73] Require non-empty array in ArrayKeysToArrayKeyFirstLastRector (#9046)
  • [Php71/Php73] Skip call expression in IsCountableRector and IsIterableRector (#9038)
  • [Php70] Skip seeded files and unordered bounds in RandomFunctionRector (#9016)
  • [Php70] Treat unescaped { as literal in EregToPcreTransformer (#8964) Thanks @hmennen90

Removed ๐Ÿ’€

Rules deprecated in 2.8 are now removed. Each already threw an exception on use.

  • Remove ~100 deprecated rules across Arguments, Assert, CodeQuality, CodingStyle, DeadCode, EarlyReturn, Naming, Php70-Php85, Removing, Renaming, Strict, Transform, TypeDeclaration, TypeDeclarationDocblocks, Unambiguous and Visibility (#8937, #8938, #8939, #8940, #8941, #8942, #8943, #8944, #8945, #8946, #8947, #8948, #8949, #8950, #8951, #8958, #8961, #8967, #8968, #8969, #8970, #8977 - #8999)
  • [Set] Remove deprecated SetList::PHP_*, SetList::NAMING, SetList::EARLY_RETURN, SetList::INSTANCEOF and SetList::IF constants (#8955, #8972, #8973)
  • [Set] Remove deprecated empty gmagick-to-imagick set (#8952)
  • [Configuration] Remove deprecated withSetProviders() (#8954)
  • [ChangesReporting] Remove deprecated github and gitlab output formats (#8953)
  • Remove deprecated attributeClass param from PhpAttributeGroupFactory::createArgsFromItems() (#9000)

Deprecations โš ๏ธ

  • Deprecate LevelSetList in favor of withPhpSets() / withPhpLevel() (#9001)
return RectorConfig::configure()
    ->withPhpSets();
  • [Renaming] Deprecate RenameStringRector, replaced by dedicated rule (#8974)
  • [Transform] Deprecate StaticCallToNewRector, WrapReturnRector and AttributeKeyToClassConstFetchRector (#8962, #8960, #8956)
  • [CodingStyle] Deprecate FuncCallToConstFetchRector and remove php_sapi_name()/pi() to constant conversion (#8959)
  • [CodingStyle] Deprecate NewlineAfterStatementRector, as it better fits a coding standard tool (#8975)

Internals โš™๏ธ

  • Build parallel worker command line from Configuration (#9031)
  • Type hint RectorStyle instead of SymfonyStyle in services (#9030)
  • Resolve bootstrap CLI options without symfony/console ArgvInput (#9027)
  • Bump dev dependencies to latest versions (#8976)
  • Delete leftover file e (#8936) Thanks @staabm

rector-symfony ๐ŸŽต

  • [Symfony52] Add EmailModeToConstantRector (#1105)
  • [Symfony51] Add ResponseCreateToNewRector (#1107)
  • [Symfony51] Add PreviousAdminRoleToIsImpersonatorRector to replace RenameStringRector config (#1129)
  • [Symfony42] Add GetExtendedTypesReturnArrayRector (#1106)
  • [Symfony73] Keep named arguments next to options array in ConstraintOptionsToNamedArgumentsRector (#1133)
  • [CodeQuality] Gate EventSubscriberToAttributeRector on symfony/framework-bundle, not event-dispatcher alone (#1132) Thanks @bbrala
  • Remove 23 deprecated rules across CodeQuality, Configs, DependencyInjection, JMS, SwiftMailer, Symfony30, Symfony43, Symfony62 and Symfony73 (#1108 - #1128, #1130, #1131)

rector-phpunit ๐ŸŸข

  • [PHPUnit60] Skip tests in classes with asserting parent lifecycle methods in AddDoesNotPerformAssertionToNonAssertingTestRector (#834)
  • Remove 7 deprecated rules: SuffixMockObjectPropertyRector, ConfiguredMockEntityToSetterObjectRector, AllowMockObjectsWithoutExpectationsAttributeRector, ReplaceTestFunctionPrefixWithAttributeRector, RemoveNamedArgsInDataProviderRector, WillReturnCallbackFallbackToThrowRector and BehatPHPUnitAssertToWebmozartRector (#835 - #841)

rector-doctrine ๐Ÿ—ƒ๏ธ

  • [CodeQuality] Add ColumnTypeToTypesConstantRector (#549)
  • Remove 7 deprecated rules and classes: AddArrayResultColumnNamesRector, RemoveEmptyTableAttributeRector, YamlToAttributeDoctrineMappingRector, CodeQualityEnumDoctrineClass, GetRepositoryServiceLocatorToRepositoryServiceInjectionRector, MoveCurrentDateTimeDefaultInEntityToConstructorRector and TypeNullableEntityFromDocblockRector (#550 - #556)

rector-downgrade-php โฌ‡๏ธ

  • [DowngradePhp84] Handle property fetch, static access, array access and invocation on DowngradeNewMethodCallWithoutParenthesesRector (#424)

Don't miss a new rector release

NewReleases is sending notifications on new releases.