Skip to content

Commit

Permalink
Code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
K0nias committed Aug 4, 2023
1 parent 439740f commit fb893e6
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function refactor(Node $node): ?Node
return $this->nodeFactory->createClassConstFetch($className, $enumCaseName);
}

private function isEnumConstant(string $className, string $constant)
private function isEnumConstant(string $className, string $constant): bool
{
$classReflection = $this->reflectionProvider->getClass($className);

Expand Down Expand Up @@ -189,21 +189,42 @@ private function refactorEqualsMethodCall(MethodCall $methodCall): ?Identical
return new Identical($expr, $right);
}

private function isCallerClassEnum(StaticCall|MethodCall $node): bool
{
if ($node instanceof StaticCall) {
return $this->isObjectType($node->class, new ObjectType('MyCLabs\Enum\Enum'));
}

return $this->isObjectType($node->var, new ObjectType('MyCLabs\Enum\Enum'));
}

private function getNonEnumReturnTypeExpr(Node $node): null|ClassConstFetch|Expr
{
if (! $node instanceof StaticCall && ! $node instanceof MethodCall) {
return null;
}

if ($this->isObjectType($node->class ?? $node->var, new ObjectType('MyCLabs\Enum\Enum'))) {
if ($this->isCallerClassEnum($node)) {
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}

$classReflection = $this->reflectionProvider->getClass($this->getName($node->class ?? $node->var));
if ($node instanceof StaticCall) {
$className = $this->getName($node->class);
}

if ($node instanceof MethodCall) {
$className = $this->getName($node->var);
}

if ($className === null) {
return null;
}

$classReflection = $this->reflectionProvider->getClass($className);
// method self::getValidEnumExpr process enum static methods from constants
if ($classReflection->hasConstant($this->getName($node->name))) {
if ($classReflection->hasConstant($methodName)) {
return null;
}
}
Expand Down

0 comments on commit fb893e6

Please sign in to comment.