Skip to content

Commit

Permalink
improve ClassMethodParamVendorLockResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 19, 2021
1 parent d2595f5 commit cc72d3b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,69 +59,36 @@ 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
if ($ancestorClassReflection === $classReflection) {
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
$classLike = $this->nodeRepository->findClassLike($ancestorClassReflection->getName());
if ($classLike === null) {
return true;
}
}

if ($classReflection->isClass()) {
return $this->methodReflectionContractAnalyzer->hasInterfaceContract($classReflection, $methodName);
}

if ($classReflection->isInterface()) {
return $this->methodReflectionContractAnalyzer->hasInterfaceContract($classReflection, $methodName);
}

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;
$classMethod = $classLike->getMethod($methodName);
if ($classMethod === null) {
continue;
}
}

if ($parentClassReflection->hasMethod($methodName)) {
// parent class method in external scope → it's not ok
// if not, look for it's parent parent
return true;
$paramType = $classMethod->params[$paramPosition]->type;
return $paramType !== null;
}

return null;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ final class LocalChildClass extends AbstractLocalParentClass
{
}

/**
* @param int $number
*/
public function changeToo(int $number)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@ interface ParentInterface
/**
* @param float|int|string $value
*/
public function __construct($value);
public function run($value);
}

interface IntermediateInterface extends ParentInterface
{
/**
* @param string $countryCode
*/
public function __construct($countryCode);
public function run($countryCode);
}

final class SkipInterfaceExtends implements IntermediateInterface
{
/**
* @param string $countryCode
*/
public function __construct($countryCode)
public function run(string $countryCode)
{
}
}
?>
4 changes: 1 addition & 3 deletions src/HttpKernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cc72d3b

Please sign in to comment.