示例#1
0
 /**
  * @param  MethodReflection $reflectionMethod
  * @return MethodGenerator
  */
 public static function fromReflection(MethodReflection $reflectionMethod)
 {
     $method = new static();
     $declaringClass = $reflectionMethod->getDeclaringClass();
     $method->setSourceContent($reflectionMethod->getContents(false));
     $method->setSourceDirty(false);
     if ($reflectionMethod->getDocComment() != '') {
         $method->setDocBlock(DocBlockGenerator::fromReflection($reflectionMethod->getDocBlock()));
     }
     $method->setFinal($reflectionMethod->isFinal());
     if ($reflectionMethod->isPrivate()) {
         $method->setVisibility(self::VISIBILITY_PRIVATE);
     } elseif ($reflectionMethod->isProtected()) {
         $method->setVisibility(self::VISIBILITY_PROTECTED);
     } else {
         $method->setVisibility(self::VISIBILITY_PUBLIC);
     }
     $method->setInterface($declaringClass->isInterface());
     $method->setStatic($reflectionMethod->isStatic());
     $method->setName($reflectionMethod->getName());
     foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
         $method->setParameter(ParameterGenerator::fromReflection($reflectionParameter));
     }
     $method->setBody(static::clearBodyIndention($reflectionMethod->getBody()));
     return $method;
 }
示例#2
0
 /**
  * Get parameter annotations from DocBlock
  *
  * @return \Zend\Code\Reflection\DocBlock\ParamTag[]
  */
 protected function getParameterTags()
 {
     if (null !== $this->parameterAnnotations) {
         return $this->parameterAnnotations;
     }
     $rDocBlock = $this->reflection->getDocBlock();
     if ($rDocBlock instanceof DocBlockReflection) {
         $params = $rDocBlock->getTags('param');
     } else {
         $params = array();
     }
     $this->parameterAnnotations = $params;
     return $this->parameterAnnotations;
 }
 /**
  * @override enforces generation of \ProxyManager\Generator\MethodGenerator
  *
  * {@inheritDoc}
  */
 public static function fromReflection(MethodReflection $reflectionMethod)
 {
     /* @var $method self */
     $method = new static();
     $method->setSourceContent($reflectionMethod->getContents(false));
     $method->setSourceDirty(false);
     if ($reflectionMethod->getDocComment() != '') {
         $method->setDocBlock(DocBlockGenerator::fromReflection($reflectionMethod->getDocBlock()));
     }
     $method->setFinal($reflectionMethod->isFinal());
     $method->setVisibility(self::extractVisibility($reflectionMethod));
     foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
         $method->setParameter(ParameterGenerator::fromReflection($reflectionParameter));
     }
     $method->setStatic($reflectionMethod->isStatic());
     $method->setName($reflectionMethod->getName());
     $method->setBody($reflectionMethod->getBody());
     $method->setReturnsReference($reflectionMethod->returnsReference());
     return $method;
 }
 /**
  * Retrieve method full documentation description.
  *
  * @param \Zend\Code\Reflection\MethodReflection $method
  * @return string
  */
 protected function extractMethodDescription(\Zend\Code\Reflection\MethodReflection $method)
 {
     $methodReflection = new MethodReflection($method->getDeclaringClass()->getName(), $method->getName());
     $docBlock = $methodReflection->getDocBlock();
     if (!$docBlock) {
         throw new \LogicException('The docBlock of the method ' . $method->getDeclaringClass()->getName() . '::' . $method->getName() . ' is empty.');
     }
     return $this->_typeProcessor->getDescription($docBlock);
 }
 /**
  * Get possible method exceptions
  *
  * @param \Zend\Code\Reflection\MethodReflection $methodReflection
  * @return array
  */
 public function getExceptions($methodReflection)
 {
     $exceptions = [];
     $methodDocBlock = $methodReflection->getDocBlock();
     if ($methodDocBlock->hasTag('throws')) {
         $throwsTypes = $methodDocBlock->getTags('throws');
         if (is_array($throwsTypes)) {
             /** @var $throwsType \Zend\Code\Reflection\DocBlock\Tag\ThrowsTag */
             foreach ($throwsTypes as $throwsType) {
                 $exceptions = array_merge($exceptions, $throwsType->getTypes());
             }
         }
     }
     return $exceptions;
 }
示例#6
0
    /**
     * Identify getter return type by its reflection.
     *
     * @param \Zend\Code\Reflection\MethodReflection $methodReflection
     * @return array <pre>array(
     *     'type' => <string>$type,
     *     'isRequired' => $isRequired,
     *     'description' => $description
     * )</pre>
     * @throws \InvalidArgumentException
     */
    public function getGetterReturnType($methodReflection)
    {
        $methodDocBlock = $methodReflection->getDocBlock();
        if (!$methodDocBlock) {
            throw new \InvalidArgumentException(
                "Each getter must have description with @return annotation. "
                . "See {$methodReflection->getDeclaringClass()->getName()}::{$methodReflection->getName()}()"
            );
        }
        $returnAnnotations = $methodDocBlock->getTags('return');
        if (empty($returnAnnotations)) {
            throw new \InvalidArgumentException(
                "Getter return type must be specified using @return annotation. "
                . "See {$methodReflection->getDeclaringClass()->getName()}::{$methodReflection->getName()}()"
            );
        }
        /** @var \Zend\Code\Reflection\DocBlock\Tag\ReturnTag $returnAnnotation */
        $returnAnnotation = current($returnAnnotations);
        $returnType = $returnAnnotation->getType();
        /*
         * Adding this code as a workaround since \Zend\Code\Reflection\DocBlock\Tag\ReturnTag::initialize does not
         * detect and return correct type for array of objects in annotation.
         * eg @return \Magento\Webapi\Service\Entity\SimpleData[] is returned with type
         * \Magento\Webapi\Service\Entity\SimpleData instead of \Magento\Webapi\Service\Entity\SimpleData[]
         */
        $escapedReturnType = str_replace('[]', '\[\]', $returnType);
        $escapedReturnType = str_replace('\\', '\\\\', $escapedReturnType);

        if (preg_match("/.*\\@return\\s+({$escapedReturnType}).*/i", $methodDocBlock->getContents(), $matches)) {
            $returnType = $matches[1];
        }
        $isRequired = preg_match("/.*\@return\s+\S+\|null.*/i", $methodDocBlock->getContents(), $matches)
            ? false
            : true;
        return [
            'type' => $returnType,
            'isRequired' => $isRequired,
            'description' => $returnAnnotation->getDescription(),
            'parameterCount' => $methodReflection->getNumberOfRequiredParameters()
        ];
    }
示例#7
0
 /**
  * Retrieve method full documentation description.
  *
  * @param ReflectionMethod $method
  * @return string
  */
 protected function extractMethodDescription(ReflectionMethod $method)
 {
     $methodReflection = new MethodReflection($method->getDeclaringClass()->getName(), $method->getName());
     $docBlock = $methodReflection->getDocBlock();
     return $this->_typeProcessor->getDescription($docBlock);
 }