packagist rector/rector 0.12.0
Released Rector 0.12 with PHPStan 1.0 and lighter Kernel

latest releases: dev-main, 1.1.0, 1.0.5...
2 years ago

I'm proud to share the biggest Rector release yet built on work of dozen contributors!

Main focus of this release is on type-completing rules, PHP 7-8 upgrade rules, moving from node attributes to parent node relations and upgrade to PHPStan 1.0.

New Features πŸŽ‰

  • Upgrade to Symplify not using symfony/http-kernel (#1119) - Rector is now 10 Symfony packages lighter on every download
  • [Php80] Add Php8ResourceReturnToObjectRector (#1068), Thanks @samsonasik!
  • [Php81] Add Php81ResourceReturnToObjectRector (#1114), Thanks @samsonasik!
  • Extract ObjectReference enum for "self", "static" and "parent" keywords (#1056)
  • [TypeDeclaration] Add TypedPropertyFromStrictGetterMethodReturnTypeRector (#1048)
  • [DowngradePhp54] Add DowngradeStaticClosureRector (#1038), Thanks @Lctrs!
  • [DowngradePhp56] Add DowngradeExponentialOperatorRector (#1181), Thanks @villfa!
  • [DowngradePhp56] Add DowngradeExponentialAssignmentOperatorRector (#1142), Thanks @fezfez!
  • [DowngradePhp74] handle static arrow functions to static closures (#1036), Thanks @Lctrs!
  • [DowngradePhp74] Add DowngradePreviouslyImplementedInterfaceRector (#1159), Thanks @samsonasik!
  • [DowngradePhp74] Handle nested uses on ArrowFunctionToAnonymousFunctionRector (#1040), Thanks @samsonasik!
  • [DowngradePhp81][DowngradePhp80] Add DowngradePhp81ResourceReturnToObjectRector and DowngradePhp80ResourceReturnToObjectRector (#1190), Thanks @samsonasik!
  • [DowngradePhp80/81] Refactor Downgrade Resource Return to Object to use BooleanOr check (#1193), Thanks @samsonasik!
  • Add mask support for skip to StringClassNameToClassConstantRector (#1102)
 use \Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;

 $services->set(StringClassNameToClassConstantRector::class);
    ->call('configure', [[
        StringClassNameToClassConstantRector::CLASSES_TO_SKIP => [
-    		'Nette\Utils\String',
-    		'Nette\Neon\Neon',
-    		'Nette\Application\UI\Presenter',
+    		'Nette\*'
		],
    ]]);

Bugfixes πŸ›

  • Fix inverseJoinColumns Doctrine annotation class mapping (#1142), Thanks @fezfez!
  • Fix type without class name mapping without substracted type (#1104)
  • [DeadCode] Skip Route annotation on RemoveDelegatingParentCallRector (#1138), Thanks @samsonasik!
  • [TypeDeclaration] Skip nullable self referencing param and return on AddArrayParamDocTypeRector and ReturnTypeDeclarationRector (#1183), Thanks @samsonasik!
  • [TypeDeclaratoin] Skip variadic param (rectorphp/rector-src@04354c2)
  • Skip union if parent type in ClassMethodParamTypeCompleter (#1103)
  • [DeadCode] Skip dynamic fetch method call on RemoveUnusedPrivateMethodRector (#1128), Thanks @samsonasik!
  • [Renaming] Handle rename property too when parent property renamed (#1031), #6758, Thanks @samsonasik!
  • [Php56] Skip AddDefaultValueForUndefinedVariableRector on has isset check (#1028), #6760, Thanks @samsonasik!
  • Check for ClassLike instance in PropertyFetchFinder (#1033), Thanks @canvural!
  • [AnonymousFunctionFactory] Do not pass variables defined in foreach or parameters list to closure uses (#1038), Thanks @Lctrs!
  • [Php71] Skip typed array property on CountOnNullRector (#1049), Thanks @samsonasik!
  • [Php73] Skip JsonThrowOnErrorRector on passed exact value (#1051), Thanks @samsonasik!
  • [Php80] Skip union callable on ClassPropertyAssignToConstructorPromotionRector (#1101), Thanks @samsonasik!
  • [CodeQuality] Skip Variable may be immutable via New_ or Clone_ on SimplifyUselessVariableRector (#1060), Thanks @samsonasik!
  • [DowngradePhp74] fix uses for nested arrow functions with same param names (#1090), Thanks @Lctrs!
  • [DeadCode] Handle bool var if cond return true next return bool var on RemoveDuplicatedIfReturnRector (#1136), Thanks @samsonasik!
  • fix intersection type with single type (#1086)
  • [TypeDeclaration] Skip ternary on ParamTypeByMethodCallTypeRector (#1131), Thanks @zingimmick!

PHPStan 1.0 Upgrade ⛺️

This Rector version depends on PHPStan more then ever. We're using its php-parser (many of them actually), class name visitor resolver and static/this/self types. That's why we had to take time to make sure the upgrade to PHPStan 1.0 is stable.

  • [PHPStan 1.0] upgrade deprecated getNativeMethods() method (#1069)
  • Improve self/static/parent object reference (#1071)
  • [PHPStan 1.0] Make SelfType, StaticType and ThisType use ClassReflection over string (#1072)
  • SelfType and StaticType are now handled in ObjectTypeSpecifier (#1077)
  • [PHPStan 1.0] Use processNodes() over processNodeStmts() (#1111)
  • [PHPStan 1.0] use selectFromArgs() for function reflectoin to get passed by reference positoins (#1113)
  • [PHPStan 1.0] Remove sorted union types, now handled by default (#1112)
  • [PHPStan 1.0] resolve FunctionReflection getFileName() from main interface (#1117)
  • [PHPStan 1.0] Remove ReflectionWithFilename (#1118)
  • Fix issue of parsing PHP 8 sources on PHP 7.x (#1184), Thanks @ondrejmirtes!
  • [PHPStan 1.0] Remove node naming visitors and re-use PHPStan ones (#1144)
  • PHPStan\Reflection\ClassReflection::getFileName() now returns null|string (rectorphp/rector-src@9686861)
  • PHPStan\Reflection\ClassReflection::getParentClass now returns null|class reflection (rectorphp/rector-src@1cc7dc3)
  • NativeFunctionReflection has new parameter (rectorphp/rector-src@ca82769)
  • Remove ClearReturnNewByReferenceRector, PHPStan cannot handle it (rectorphp/rector-src@051c751)

PHPStan 1.0-Related Changes

Class names are now available in the node itself:

-$className = $node->getAttribute(AttributeKey::CLASS_NAME);

↓

 // in case of class like
+$className = $classLike->namespacedName

 // in case of anonymous class
+$className = $classLike->name

 // or widely used method in AbstractRector
 $className = $this->getName($classLike)

Deprecated/Removed ⚰️

  • [CodingStyle] Deprecate RemoveUnusedAliasRector, job rather for coding standard tool and opinonated (#1157)
  • Remove FileNodeVisitor, available in AbstractRector (#1145)
  • [CodeQuality] Remove FixClassCaseSensitivityNameRector, conflicting with name node removal and edge case already handled by PHPStan reports (#1160)
  • remove deprecated AttributeKey::FILE_INFO const attribute (rectorphp/rector-src@8f7fea0)
  • Remove dead classes (#1133)
  • Remove dead classes #2, remove unused PlatformAgnosticAssertions, remove ConsoleShowOutputFormatter (#1134)
  • [Defluent] Remove often-breaking set (#1062)

Attribute Keys Changes πŸ“ˆ

This is not about PHP 8.0 attributes, but about helper php-parser attributes that allow to store mixed data in any node.

  • Move from CLASS_NODE attribute to more reliable parent node finder (#1162)
  • Move from CLASS_METHOD node attribute to parent finder (#1167)
  • Deprecatd METHOD_NAME node (#1169)
  • Refactor CLASS_NAME to parent/scope name resolver (#1170)

These attributes were based on prepared node tree and utils attributes that always returned mixed. This proven as unreliable and required always checking for the correct type in fetched attribute. That's why these are no longer used:

  • AttributeKey::CLASS_NODE
  • AttributeKey::CLASS_NAME
  • AttributeKey::METHOD_NODE
  • AttributeKey::METHOD_NAME

How to Upgrade?

Use BetterNodeFinder service to get the parent node you need with generics support:

-$class = $node->getAttribute(AttributeKey::CLASS_NODE);
// mixed type

+$class = $this->betterNodeFinder->findParentType($node, Class_::class);
// Class_|null
-$classMethod = $node->getAttribute(AttributeKey::METHOD_NODE);
// mixed type

+$classMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
// Class_|null

Don't miss a new rector release

NewReleases is sending notifications on new releases.