Get the full method body using reflection

@param string          $methodName
@param ReflectionClass $reflectionClass

@return string
353980
public function getMethodBody(string $methodName, ReflectionClass $reflectionClass): string
    {
        $method      = $reflectionClass->getMethod($methodName);
        $startLine   = $method->getStartLine() - 1;
        $length      = $method->getEndLine() - $startLine;
        $lines       = file($reflectionClass->getFileName());
        $methodLines = \array_slice($lines, $startLine, $length);

        return implode('', $methodLines);
    }
864508
final protected function getTypeFromReturnTypeHint(ReflectionMethod $method, string $fieldName): ?Type
    {
        $returnType = $method->getReturnType();
        if (!$returnType) {
            return null;
        }

        $returnTypeName = (string) $returnType;
        if (is_a($returnTypeName, Collection::class, true) || $returnTypeName === 'array') {
            $targetEntity = $this->getTargetEntity($fieldName);
            if (!$targetEntity) {
                throw new Exception('The method ' . $this->getMethodFullName($method) . ' is type hinted with a return type of `' . $returnTypeName . '`, but the entity contained in that collection could not be automatically detected. Either fix the type hint, fix the doctrine mapping, or specify the type with `@API\Field` annotation.');
            }

            $type = Type::listOf($this->getTypeFromRegistry($targetEntity, false));
            if (!$returnType->allowsNull()) {
                $type = Type::nonNull($type);
            }

            return $type;
        }

        return $this->reflectionTypeToType($returnType);
    }
467185
protected function getReflectionFunctionName(\ReflectionFunctionAbstract $reflection)
    {
        // Class method
        if ($reflection instanceof \ReflectionMethod) {
            return $reflection->getDeclaringClass()->getName() . '::' . $reflection->getName();
        }
        return $reflection->getName();
    }
1242909
protected function getFieldGetter(\ReflectionClass $reflectionClass, $fieldName)
    {
        foreach (array('get', 'is', 'has') as $prefix) {
            $getter = $prefix.ucfirst($fieldName);
            $reflectionMethod = $reflectionClass->getMethod($getter);

            if ($reflectionMethod && $reflectionMethod->isPublic()) {
                return $reflectionMethod;
            }
        }

        throw new EntityListException("The field $fieldName does not exist or is not accessible.");
    }
209338
private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName)
    {
        $annotationClass = "FOS\\RestBundle\\Controller\\Annotations\\$annotationName";

        if ($annotation = $this->annotationReader->getClassAnnotation($reflectionClass, $annotationClass)) {
            return $annotation;
        }
    }
457142
protected function buildMethod(ReflectionMethod $reflectionMethod) : Node\Stmt\ClassMethod
    {
        $methodBuilder = new Method($reflectionMethod->getName());

        if ($reflectionMethod->isPublic()) {
            $methodBuilder->makePublic();
        }

        if ($reflectionMethod->isProtected()) {
            $methodBuilder->makeProtected();
        }

        if ($reflectionMethod->isPrivate()) {
            $methodBuilder->makePrivate();
        }

        if ($reflectionMethod->isStatic()) {
            $methodBuilder->makeStatic();
        }

        if ($reflectionMethod->isAbstract()) {
            $methodBuilder->makeAbstract();
        }

        if ($reflectionMethod->isFinal()) {
            $methodBuilder->makeFinal();
        }

        if ($reflectionMethod->returnsReference()) {
            $methodBuilder->makeReturnByRef();
        }

        foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
            $methodBuilder->addParam($this->buildParameter($reflectionParameter));
        }

        // @todo should parse method body if possible (skipped for now)

        return $methodBuilder->getNode();
    }
400159
protected function buildMethod(ReflectionMethod $method)
    {
        $argString = "";
        $paramString = "";
        foreach ($method->getParameters() as $value) {
            $argString .= "\${$value->getName()}, ";
            $paramString .= $this->getParamString($value);
        }

        $argString = trim($argString, ", ");
        $paramString = trim($paramString, ", ");
        $replaces = array(
            $method->getDocComment(),
            $method->getName(),
            $paramString,
            $argString
        );
        return str_replace(self::$methodTplSearches, $replaces, $this->template->getMethodTemplate());
    }
1110868
public function create(ClassReflection $classReflection, \ReflectionMethod $methodReflection, string $methodWrapper = null): MethodReflection
    {
        $phpDocParameterTypes = [];
        $phpDocReturnType = null;
        if ($methodReflection->getDocComment() !== false) {
            $phpDocBlock = PhpDocBlock::resolvePhpDocBlockForMethod(
                Broker::getInstance(),
                $methodReflection->getDocComment(),
                $methodReflection->getDeclaringClass()->getName(),
                $methodReflection->getName(),
                $methodReflection->getFileName()
            );

            $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
                $phpDocBlock->getFile(),
                $phpDocBlock->getClass(),
                $phpDocBlock->getDocComment()
            );
            $phpDocParameterTypes = array_map(function (ParamTag $tag): Type {
                return $tag->getType();
            }, $resolvedPhpDoc->getParamTags());
            $phpDocReturnType = $resolvedPhpDoc->getReturnTag() !== null ? $resolvedPhpDoc->getReturnTag()->getType() : null;
        }

        if ($methodWrapper) {
            $methodReflection = new $methodWrapper($methodReflection);
        }

        return $this->methodReflectionFactory->create(
            $classReflection,
            $methodReflection,
            $phpDocParameterTypes,
            $phpDocReturnType
        );
    }
222826
static private function getReturnClass(ReflectionFunctionAbstract $reflection)
    {
        $type = $reflection->getReturnType();

        if (null === $type || $type->isBuiltin()) {
            return null;
        }

        return (string) $type;
    }
1042270
public function getTraitImplementingMethod(ReflectionClass $class, string $methodName): ReflectionClass
    {
        $traitsWithMethod = [];
        foreach ($class->getTraits() as $trait) {
            try {
                $trait->getMethod($methodName);
                $traitsWithMethod[] = $trait;
            } catch (\ReflectionException $e) {
            }
        }
        if (count($traitsWithMethod) > 1) {
            throw new \RuntimeException(
                'Found more than one trait implementing the method ' . $methodName . ' in ' .
                $class->getShortName()
            );
        }
        if ([] === $traitsWithMethod) {
            throw new \RuntimeException(
                'Failed finding trait implementing the method ' . $methodName . ' in ' .
                $class->getShortName()
            );
        }

        return current($traitsWithMethod);
    }
1442
public function getName()
    {
        if ($this->_name === null) {
            $this->_name = ((new \ReflectionClass($this))->getShortName());
        }
        
        return $this->_name;
    }
479932
private function getRouteName(\ReflectionMethod $method){
        $class  = $method->getDeclaringClass()->getName();
        $name   = substr(
            $class,
            strlen($this->kernel->getConfig()->getNameSpace() . "\\Controller\\"),
            -10 // Controller
        );

        $controller = implode("_", array_map("strtolower", explode("\\", $name)));
        $action     = $method->getName();

        if("Action" === substr($action, -6)){
            $action = substr($action, 0, -6);
        }

        return "{$controller}:{$action}";
    }
1158945
protected function findCommand(\ReflectionMethod $reflectionMethod)
    {
        $parameters = $reflectionMethod->getParameters();

        /** @var \ReflectionParameter $parameter */
        foreach ($parameters as $parameter) {
            $class = $parameter->getClass();

            if (null !== $class && true === $class->implementsInterface('RomaricDrigon\OrchestraBundle\Domain\Command\CommandInterface')) {
                return $class->getName();
            }
        }

        return null;
    }
1018607
protected function isGetMethod(\ReflectionMethod $method)
    {
        return 'get' === substr($method->name, 0, 3) && strlen($method->name) > 3 &&
            0 === $method->getNumberOfRequiredParameters();
    }
1036780
private static function getParameterTemplate(\ReflectionMethod $reflectionMethod): string
    {
        $template = '';
        $reflectionParameters = $reflectionMethod->getParameters();
        $paramCount = \count($reflectionParameters);
        foreach ($reflectionParameters as $reflectionParameter) {
            $paramCount--;
            // Parameter type
            $type = $reflectionParameter->getType();
            if ($type !== null) {
                $type = $type->__toString();
                $template .= " $type ";
            }

            // Parameter name
            $paramName = $reflectionParameter->getName();
            if ($reflectionParameter->isPassedByReference()) {
                $template .= " &\${$paramName} ";
            } elseif ($reflectionParameter->isVariadic()) {
                $template .= " ...\${$paramName} ";
            } else {
                $template .= " \${$paramName} ";
            }

            // Parameter default value
            if ($reflectionParameter->isOptional() && $reflectionParameter->isVariadic() === false) {
                $template .= self::getParameterDefaultValue($reflectionParameter);
            }

            if ($paramCount !== 0) {
                $template .= ',';
            }
        }

        return $template;
    }
603684
protected final function getReflection(string $className): ReflectionClass
   {
       try {
           $reflexion = new ReflectionClass($className);
       } catch (ReflectionException $e) {
           throw new NotFoundException(
               "Can't get reflection for " . $className . ": not found");
       }
       if (!$reflexion->isInstantiable()) {
           throw new ContainerException(
               "Can't get reflection for " . $className . ": not instanciable");
       }
       return $reflexion;
   }
684070
private function getRoutePattern($reflectionMethod)
    {
        $pattern = $reflectionMethod->getName();
        $params = $reflectionMethod->getParameters();

        foreach ($params as $param) {
            if ($param->getClass()) {
                $className = $param->getClass()->getShortName();

                if ($className != 'Request' && $className != 'Application') {
                    $pattern .= '/{'.$param->getName().'}';
                }
            } else {
                $pattern .= '/{'.$param->getName().'}';
            }
        }

        return $pattern;
    }