From 484e0735cea4458f06145b90993b7e74044059b6 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 19 Mar 2021 23:22:15 +0100 Subject: [PATCH] improve ClassMethodParamVendorLockResolver --- .../ClassMethodParamVendorLockResolver.php | 60 ++++--------------- .../local_scope_with_parent_class.php.inc | 3 - .../Fixture/skip_interface_extends.php.inc | 7 +-- src/HttpKernel/RectorKernel.php | 4 +- 4 files changed, 16 insertions(+), 58 deletions(-) diff --git a/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php b/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php index 5dfd3b92096e..0d2536a64c1b 100644 --- a/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php +++ b/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php @@ -59,6 +59,10 @@ public function isVendorLocked(ClassMethod $classMethod, int $paramPosition): bo return false; } + if ($classMethod->isMagic()) { + return true; + } + $methodName = $this->nodeNameResolver->getName($classMethod); foreach ($classReflection->getAncestors() as $ancestorClassReflection) { // skip self @@ -66,62 +70,22 @@ public function isVendorLocked(ClassMethod $classMethod, int $paramPosition): bo continue; } - if (! $classReflection->hasNativeMethod($methodName)) { + if (! $ancestorClassReflection->hasNativeMethod($methodName)) { continue; } - } -// if (! $this->classReflectionAncestorAnalyzer->hasAncestors($classReflection)) { -// return false; -// } - - - if ($classReflection->getParentClass() !== false) { - $vendorLock = $this->isParentClassVendorLocking( - $classReflection->getParentClass(), - $paramPosition, - $methodName - ); - if ($vendorLock !== null) { - return $vendorLock; + // class is vendor, its locking us + if (! $this->nodeRepository->findClassLike($ancestorClassReflection->getName())) { + return true; } - } - if ($classReflection->isClass()) { - return $this->methodReflectionContractAnalyzer->hasInterfaceContract($classReflection, $methodName); - } + $classLike = $this->nodeRepository->findClassLike($ancestorClassReflection->getName()); + $classMethod = $classLike->getMethod($methodName); - if ($classReflection->isInterface()) { - return $this->methodReflectionContractAnalyzer->hasInterfaceContract($classReflection, $methodName); + $paramType = $classMethod->params[$paramPosition]->type; + return $paramType !== null; } return false; } - - private function isParentClassVendorLocking( - ClassReflection $parentClassReflection, - int $paramPosition, - string $methodName - ): ?bool { - $parentClass = $this->nodeRepository->findClass($parentClassReflection->getName()); - if ($parentClass !== null) { - $parentClassMethod = $parentClass->getMethod($methodName); - // parent class method in local scope → it's ok - if ($parentClassMethod !== null) { - // parent method has no type → we cannot change it here - if (! isset($parentClassMethod->params[$paramPosition])) { - return false; - } - return $parentClassMethod->params[$paramPosition]->type === null; - } - } - - if ($parentClassReflection->hasMethod($methodName)) { - // parent class method in external scope → it's not ok - // if not, look for it's parent parent - return true; - } - - return null; - } } diff --git a/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/local_scope_with_parent_class.php.inc b/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/local_scope_with_parent_class.php.inc index b36ff770cc74..51c98425a70c 100644 --- a/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/local_scope_with_parent_class.php.inc +++ b/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/local_scope_with_parent_class.php.inc @@ -41,9 +41,6 @@ final class LocalChildClass extends AbstractLocalParentClass { } - /** - * @param int $number - */ public function changeToo(int $number) { } diff --git a/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/skip_interface_extends.php.inc b/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/skip_interface_extends.php.inc index f835cb46ae78..2ad952b3877e 100644 --- a/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/skip_interface_extends.php.inc +++ b/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/skip_interface_extends.php.inc @@ -6,7 +6,7 @@ interface ParentInterface /** * @param float|int|string $value */ - public function __construct($value); + public function run($value); } interface IntermediateInterface extends ParentInterface @@ -14,7 +14,7 @@ interface IntermediateInterface extends ParentInterface /** * @param string $countryCode */ - public function __construct($countryCode); + public function run($countryCode); } final class SkipInterfaceExtends implements IntermediateInterface @@ -22,8 +22,7 @@ final class SkipInterfaceExtends implements IntermediateInterface /** * @param string $countryCode */ - public function __construct($countryCode) + public function run(string $countryCode) { } } -?> diff --git a/src/HttpKernel/RectorKernel.php b/src/HttpKernel/RectorKernel.php index 567db38a20bc..48f3e747035d 100644 --- a/src/HttpKernel/RectorKernel.php +++ b/src/HttpKernel/RectorKernel.php @@ -86,14 +86,12 @@ public function setConfigs(array $configs): void */ public function registerBundles(): iterable { - $bundles = [ + return [ new ConsoleColorDiffBundle(), new ComposerJsonManipulatorBundle(), new SkipperBundle(), new SimplePhpDocParserBundle(), ]; - - return $bundles; } protected function build(ContainerBuilder $containerBuilder): void