/** * 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()]; }