public function testExtractMethodData() { $classReflection = new \Zend\Code\Reflection\ClassReflection('\\Magento\\Webapi\\Test\\Unit\\Model\\Config\\TestServiceForClassReflector'); /** @var $methodReflection \Zend\Code\Reflection\MethodReflection */ $methodReflection = $classReflection->getMethods()[0]; $methodData = $this->_classReflector->extractMethodData($methodReflection); $expectedResponse = $this->_getSampleReflectionData(); $this->assertEquals($expectedResponse, $methodData); }
/** * Reflect methods in given class and set retrieved data into reader. * * @param string $className * @param array $methods * @return array <pre>array( * $firstMethod => array( * 'documentation' => $methodDocumentation, * 'interface' => array( * 'in' => array( * 'parameters' => array( * $firstParameter => array( * 'type' => $type, * 'required' => $isRequired, * 'documentation' => $parameterDocumentation * ), * ... * ) * ), * 'out' => array( * 'parameters' => array( * $firstParameter => array( * 'type' => $type, * 'required' => $isRequired, * 'documentation' => $parameterDocumentation * ), * ... * ) * ) * ) * ), * ... * )</pre> */ public function reflectClassMethods($className, $methods) { $data = []; $classReflection = new \Zend\Code\Reflection\ClassReflection($className); /** @var \Zend\Code\Reflection\MethodReflection $methodReflection */ foreach ($classReflection->getMethods() as $methodReflection) { $methodName = $methodReflection->getName(); if (array_key_exists($methodName, $methods)) { $data[$methodName] = $this->extractMethodData($methodReflection); } } return $data; }
/** * Retrieve class full documentation description. * * @param string $className * @return string */ public function extractClassDescription($className) { $classReflection = new \Zend\Code\Reflection\ClassReflection($className); $docBlock = $classReflection->getDocBlock(); if (!$docBlock) { return ''; } return $this->_typeProcessor->getDescription($docBlock); }
/** * @3491 */ public function testPropertyDocBlockWillLoadFromReflection() { $reflectionClass = new \Zend\Code\Reflection\ClassReflection('\\ZendTest\\Code\\Generator\\TestAsset\\TestClassWithManyProperties'); $reflProp = $reflectionClass->getProperty('fooProperty'); $cgProp = PropertyGenerator::fromReflection($reflProp); $this->assertEquals('fooProperty', $cgProp->getName()); $docBlock = $cgProp->getDocBlock(); $this->assertInstanceOf('Zend\\Code\\Generator\\DocBlockGenerator', $docBlock); $tags = $docBlock->getTags(); $this->assertInternalType('array', $tags); $this->assertEquals(1, count($tags)); $tag = array_shift($tags); $this->assertInstanceOf('Zend\\Code\\Generator\\DocBlock\\Tag', $tag); $this->assertEquals('var', $tag->getName()); }
/** * @param string $method * @return \Zend\Reflection\ReflectionParameter */ protected function getFirstReflectionParameter($method) { $reflectionClass = new \Zend\Code\Reflection\ClassReflection('ZendTest\\Code\\Generator\\TestAsset\\ParameterClass'); $method = $reflectionClass->getMethod($method); $params = $method->getParameters(); return array_shift($params); }
/** * Save file methods information to sysmap cache * * @param string $fileName */ protected function getControllerMap($module, $alias, $className) { $class = new \Zend\Code\Reflection\ClassReflection($className); if ($class->isSubclassOf('\\Zend\\Mvc\\Controller\\ActionController')) { $controller = new \Zend\Config\Config(array(), true); $controller->level = 2; if ('' != $class->getDocComment()) { $controller->longDescr = $class->getDocblock()->getLongDescription(); $controller->shortDescr = $class->getDocblock()->getShortDescription(); } $toDashFilter = new \Zend\Filter\Word\CamelCaseToDash(); $controller->name = $alias; $controller->module = $module; $controller->hash = md5($controller->module . '.' . $controller->name . '.*'); $actions = array(); foreach ($class->getMethods() as $method) { if ($method instanceof \Zend\Code\Reflection\MethodReflection) { $methodName = $method->getName(); if (strstr($method->getName(), $this->_actionSuffix) && !in_array($method->getName(), $this->skipedActions)) { $addIt = true; $action = new \Zend\Config\Config(array(), true); $action->level = 3; $action->name = strtolower($toDashFilter->filter(str_replace($this->_actionSuffix, '', $methodName))); $action->hash = md5($controller->module . '.' . $controller->name . '.' . $action->name); if ($method->getDocBlock() !== false) { $docBlock = $method->getDocblock(); $action->shortDescr = $docBlock->getShortDescription(); $action->longDescr = $docBlock->getLongDescription(); foreach ($docBlock->getTags() as $tag) { if ($tag->getName() == $this->_paramFormTag) { $action->{$this->_paramFormTag} = trim(str_replace('@' . $this->_paramFormTag, '', $tag->returnValue(0))); } if ($tag->getName() == $this->_hiddenFormTag) { $addIt = false; } } } if ($addIt) { $actions[$action->name] = $action; } } } } $controller->_childrens = $actions; } else { return false; } return $controller; }
/** * @group ZF-6444 */ public function testPropertyWillLoadFromReflection() { $reflectionClass = new \Zend\Code\Reflection\ClassReflection('\\ZendTest\\Code\\Generator\\TestAsset\\TestClassWithManyProperties'); // test property 1 $reflProp = $reflectionClass->getProperty('_bazProperty'); $cgProp = PropertyGenerator::fromReflection($reflProp); $this->assertEquals('_bazProperty', $cgProp->getName()); $this->assertEquals(array(true, false, true), $cgProp->getDefaultValue()->getValue()); $this->assertEquals('private', $cgProp->getVisibility()); $reflProp = $reflectionClass->getProperty('_bazStaticProperty'); // test property 2 $cgProp = PropertyGenerator::fromReflection($reflProp); $this->assertEquals('_bazStaticProperty', $cgProp->getName()); $this->assertEquals(\ZendTest\Code\Generator\TestAsset\TestClassWithManyProperties::FOO, $cgProp->getDefaultValue()->getValue()); $this->assertTrue($cgProp->isStatic()); $this->assertEquals('private', $cgProp->getVisibility()); }
/** * @group 5193 */ public function testTypehintsWithNamespaceInNamepsacedClassReturnTypewithBackslash() { require_once __DIR__ . '/TestAsset/NamespaceTypeHintClass.php'; $reflClass = new \Zend\Code\Reflection\ClassReflection('Namespaced\\TypeHint\\Bar'); $params = $reflClass->getMethod('method')->getParameters(); $param = ParameterGenerator::fromReflection($params[0]); $this->assertEquals('\\OtherNamespace\\ParameterClass', $param->getType()); }
/** * Analyze $type and save type properties into the registry. * * @param string $type * @return \Praxigento\Core\Reflection\Data\Property[] array with type properties or empty array for simple types. */ public function register($type) { $typeNorm = $this->_toolsType->normalizeType($type); $isSimple = $this->_typeProcessor->isTypeSimple($typeNorm); if (!isset($this->_registry[$typeNorm])) { if (!$isSimple) { /* analyze properties for complex type */ $this->_registry[$typeNorm] = []; /* process annotated methods */ /** @var \Zend\Code\Reflection\ClassReflection $reflection */ $reflection = new \Zend\Code\Reflection\ClassReflection($typeNorm); $docBlock = $reflection->getDocBlock(); if ($docBlock) { $this->_processDocBlock($typeNorm, $docBlock); } /* process normal methods (not annotated) */ $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); $this->_processMethods($typeNorm, $methods); } else { /* this is simple type w/o props */ $this->_registry[$typeNorm] = []; } } return $this->_registry[$typeNorm]; }
/** * Analyze given type and return methods meta data (name, return type, description, etc.) * * @param string $type * @return \Praxigento\Core\Reflection\Data\Method[] */ public function getMethods($type) { $result = []; $typeNorm = $this->_toolsType->normalizeType($type); /** @var \Zend\Code\Reflection\ClassReflection $reflection */ $reflection = new \Zend\Code\Reflection\ClassReflection($typeNorm); /** @var \Zend\Code\Reflection\DocBlockReflection $docBlock */ $docBlock = $reflection->getDocBlock(); $annotatedMethods = $this->_processClassDocBlock($docBlock); /* process normal methods (not annotated) */ $publicMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); $generalMethods = $this->_processClassMethods($publicMethods); $merged = array_merge($generalMethods, $annotatedMethods); /* convert results to array form according Magento requirements to be saved in the cache */ /** @var \Praxigento\Core\Reflection\Data\Method $item */ foreach ($merged as $item) { $methodName = $item->getName(); $entry = ['type' => $item->getType(), 'isRequired' => $item->getIsRequired(), 'description' => $item->getDescription(), 'parameterCount' => $item->getParameterCount()]; $result[$methodName] = $entry; } return $result; }
public function discover() { $annotationReader = $this->container->get('annotation_reader'); /* @var $annotationReader \Doctrine\Common\Annotations\AnnotationReader */ foreach ($this->services as $serviceName) { $service = $this->container->get($serviceName); $rService = new \Zend\Code\Reflection\ClassReflection($service); $jsonrpcMethods[$serviceName]['service'] = $serviceName; $jsonrpcMethods[$serviceName]['description'] = !$rService->getDocBlock() ?: $rService->getDocBlock()->getShortDescription(); foreach ($rService->getMethods(\ReflectionMethod::IS_PUBLIC) as $rMethod) { if (strpos($rMethod->getName(), '__') !== 0) { $currentMethod = []; $_tagsParam = !$rMethod->getDocBlock() ? [] : $rMethod->getDocBlock()->getTags('param'); $_tagsReturn = !$rMethod->getDocBlock() ? null : $rMethod->getDocBlock()->getTag('return'); /* @var $_tagsParam ParamTag[] */ /* @var $_tagsReturn ReturnTag */ $currentMethod['method'] = "{$serviceName}:" . $rMethod->getName(); $currentMethod['description'] = !$rMethod->getDocBlock() ?: $rMethod->getDocBlock()->getShortDescription(); $currentMethod['parameters'] = []; if (!empty($_tagsParam)) { foreach ($_tagsParam as $_tagParam) { if (strpos($_tagParam->getVariableName(), '$') === 0) { $_name = substr($_tagParam->getVariableName(), 1); } else { $_name = $_tagParam->getVariableName(); } $currentMethod['parameters'][$_name] = ['description' => $_tagParam->getDescription(), 'types' => count($_tagParam->getTypes()) == 1 ? $_tagParam->getTypes()[0] : $_tagParam->getTypes()]; } } foreach ($rMethod->getParameters() as $rParameter) { if (!isset($currentMethod['parameters'][$rParameter->getName()])) { $currentMethod['parameters'][$rParameter->getName()] = []; } $currentMethod['parameters'][$rParameter->getName()] = array_merge($currentMethod['parameters'][$rParameter->getName()], ['nullable' => $rParameter->allowsNull(), 'optional' => $rParameter->isOptional()]); if ($rParameter->isDefaultValueAvailable()) { $currentMethod['parameters'][$rParameter->getName()]['default'] = $rParameter->getDefaultValue(); } } if ($_tagsReturn) { $currentMethod['return'] = ['description' => $_tagsReturn->getDescription(), 'types' => count($_tagsReturn->getTypes()) == 1 ? $_tagsReturn->getTypes()[0] : $_tagsReturn->getTypes()]; } $jsonrpcMethods[$serviceName]['methods'][$rMethod->getName()] = $currentMethod; } } } return new Response(json_encode($jsonrpcMethods), 200, ['Content-Type' => 'application/json']); }