Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
NaokiTsuchiya committed Dec 4, 2021
1 parent 24135c7 commit 10d13cc
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions src/CodeGenMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ public function getMethods(BindInterface $bind, CodeVisitor $code): array
{
$bindingMethods = array_keys($bind->getBindings());
$reflectionClass = $this->getReflectionClass($code);
$classMethods = $reflectionClass->getMethods();
$classMethods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
$methods = [];
foreach ($classMethods as $classMethod) {
$methodName = $classMethod->getName();
$isBindingMethod = in_array($methodName, $bindingMethods, true);
$isPublic = $classMethod->isPublic();
if ($isBindingMethod && $isPublic) {
$classMethodStmt = $this->getClassMethodStmt($classMethod, $reflectionClass, $code);
if ($isBindingMethod) {
$classMethodStmt = $this->getClassMethodStmt($reflectionClass, $classMethod, $code);
$methodInsideStatements = $this->getTemplateMethodNodeStmts($classMethodStmt->returnType);
// replace statements in the method
$classMethodStmt->stmts = $methodInsideStatements;
Expand Down Expand Up @@ -123,8 +122,8 @@ private function isReturnVoid(?NodeAbstract $returnType): bool

/** @param ReflectionClass<object> $sourceClass */
private function getClassMethodStmt(
ReflectionMethod $bindingMethod,
ReflectionClass $sourceClass,
ReflectionMethod $bindingMethod,
CodeVisitor $code
): ClassMethod {
foreach ($code->classMethod as $classMethod) {
Expand All @@ -133,27 +132,13 @@ private function getClassMethodStmt(
}
}

return $this->getParentClassMethodStmt($sourceClass, $bindingMethod);
}

/** @param ReflectionClass<object> $reflectionClass */
private function getParentClassMethodStmt(
ReflectionClass $reflectionClass,
ReflectionMethod $bindingMethod
): ClassMethod {
$parentClass = $reflectionClass->getParentClass();
$parentClass = $sourceClass->getParentClass();
if ($parentClass === false) {
throw new InvalidSourceClassException($reflectionClass->getName()); // @codeCoverageIgnore
throw new InvalidSourceClassException($sourceClass->getName()); // @codeCoverageIgnore
}

// find bindingMethod from parentClass
$code = ($this->visitorFactory)($parentClass);
foreach ($code->classMethod as $classMethod) {
if ($classMethod->name->name === $bindingMethod->getName()) {
return $classMethod;
}
}

return $this->getParentClassMethodStmt($parentClass, $bindingMethod);
return $this->getClassMethodStmt($parentClass, $bindingMethod, $code);
}
}

0 comments on commit 10d13cc

Please sign in to comment.