public function testToString() { $classReflection = new ClassReflection('ZendTest\\Code\\Reflection\\TestAsset\\TestSampleClass5'); $classDocBlock = $classReflection->getDocBlock(); $expectedString = 'DocBlock [ /* DocBlock */ ] {' . PHP_EOL . PHP_EOL . ' - Tags [3] {' . PHP_EOL . ' DocBlock Tag [ * @author ]' . PHP_EOL . ' DocBlock Tag [ * @method ]' . PHP_EOL . ' DocBlock Tag [ * @property ]' . PHP_EOL . ' }' . PHP_EOL . '}' . PHP_EOL; $this->assertEquals($expectedString, (string) $classDocBlock); }
public function testTagDescriptionIsReturned() { $classReflection = new Reflection\ClassReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass5'); $authorTag = $classReflection->getDocBlock()->getTag('author'); $this->assertEquals('Ralph Schindler <*****@*****.**>', $authorTag->getContent()); }
/** * Build a Code Generation Php Object from a Class Reflection * * @param ClassReflection $classReflection * @return TraitGenerator */ public static function fromReflection(ClassReflection $classReflection) { // class generator $cg = new static($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock())); } // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) { $properties[] = PropertyGenerator::fromReflection($reflectionProperty); } } $cg->addProperties($properties); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } $cg->addMethods($methods); return $cg; }
/** * Build a Code Generation Php Object from a Class Reflection * * @param ClassReflection $classReflection * @return InterfaceGenerator */ public static function fromReflection(ClassReflection $classReflection) { if (!$classReflection->isInterface()) { throw new Exception\InvalidArgumentException(sprintf('Class %s is not a interface', $classReflection->getName())); } // class generator $cg = new static($classReflection->getName()); $methods = []; $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock())); } // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } foreach ($classReflection->getConstants() as $name => $value) { $cg->addConstant($name, $value); } $cg->addMethods($methods); return $cg; }
/** * @param ClassReflection $class * @return bool */ public function isSatisfiedBy(ClassReflection $class) { $docBlock = $class->getDocBlock(); if ($docBlock) { if ($docBlock->getTags('Annotation')) { return true; } } return false; }
/** * Cache declared interfaces and classes to a single file * * @param \Zend\Mvc\MvcEvent $e * @return void */ public function cache($e) { $request = $e->getRequest(); if ($request instanceof ConsoleRequest || $request->getQuery()->get('EDPSUPERLUMINAL_CACHE', null) === null) { return; } if (file_exists(ZF_CLASS_CACHE)) { $this->reflectClassCache(); $code = file_get_contents(ZF_CLASS_CACHE); } else { $code = "<?php\n"; } $classes = array_merge(get_declared_interfaces(), get_declared_classes()); foreach ($classes as $class) { // Skip non-Zend classes if (0 !== strpos($class, 'Zend')) { continue; } // Skip the autoloader factory and this class if (in_array($class, array('Zend\\Loader\\AutoloaderFactory', __CLASS__))) { continue; } if ($class === 'Zend\\Loader\\SplAutoloader') { continue; } // Skip any classes we already know about if (in_array($class, $this->knownClasses)) { continue; } $this->knownClasses[] = $class; $class = new ClassReflection($class); // Skip any Annotation classes $docBlock = $class->getDocBlock(); if ($docBlock) { if ($docBlock->getTags('Annotation')) { continue; } } // Skip ZF2-based autoloaders if (in_array('Zend\\Loader\\SplAutoloader', $class->getInterfaceNames())) { continue; } // Skip internal classes or classes from extensions // (this shouldn't happen, as we're only caching Zend classes) if ($class->isInternal() || $class->getExtensionName()) { continue; } $code .= static::getCacheCode($class); } file_put_contents(ZF_CLASS_CACHE, $code); // minify the file file_put_contents(ZF_CLASS_CACHE, php_strip_whitespace(ZF_CLASS_CACHE)); }
/** * Build a Code Generation Php Object from a Class Reflection * * @param ClassReflection $classReflection * @return ClassGenerator */ public static function fromReflection(ClassReflection $classReflection) { $cg = new static($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock())); } $cg->setAbstract($classReflection->isAbstract()); // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } /* @var \Zend\Code\Reflection\ClassReflection $parentClass */ $parentClass = $classReflection->getParentClass(); $interfaces = $classReflection->getInterfaces(); if ($parentClass) { $cg->setExtendedClass($parentClass->getName()); $interfaces = array_diff($interfaces, $parentClass->getInterfaces()); } $interfaceNames = array(); foreach ($interfaces as $interface) { /* @var \Zend\Code\Reflection\ClassReflection $interface */ $interfaceNames[] = $interface->getName(); } $cg->setImplementedInterfaces($interfaceNames); $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) { $properties[] = PropertyGenerator::fromReflection($reflectionProperty); } } $cg->addProperties($properties); $constants = array(); foreach ($classReflection->getConstants() as $name => $value) { $constants[] = array('name' => $name, 'value' => $value); } $cg->addConstants($constants); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } $cg->addMethods($methods); return $cg; }
/** * Retrieve complex type information from class public properties. * * @param string $class * @return array * @throws \InvalidArgumentException * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _processComplexType($class) { $typeName = $this->translateTypeName($class); $this->_types[$typeName] = []; if ($this->isArrayType($class)) { $this->register($this->getArrayItemType($class)); } else { if (!(class_exists($class) || interface_exists($class))) { throw new \InvalidArgumentException(sprintf('Could not load the "%s" class as parameter type.', $class)); } $reflection = new ClassReflection($class); $docBlock = $reflection->getDocBlock(); $this->_types[$typeName]['documentation'] = $docBlock ? $this->getDescription($docBlock) : ''; /** @var \Zend\Code\Reflection\MethodReflection $methodReflection */ foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $methodReflection) { if ($methodReflection->class === "Magento\\Framework\\Model\\AbstractModel") { continue; } $this->_processMethod($methodReflection, $typeName); } } return $this->_types[$typeName]; }
/** * Retrieve complex type information from class public properties. * * @param string $class * @return array * @throws InvalidArgumentException */ protected function _processComplexType($class) { $typeName = $this->_helper->translateTypeName($class); $this->_types[$typeName] = array(); if ($this->_helper->isArrayType($class)) { $this->process($this->_helper->getArrayItemType($class)); } else { if (!class_exists($class)) { throw new InvalidArgumentException(sprintf('Could not load the "%s" class as parameter type.', $class)); } $reflection = new ClassReflection($class); $docBlock = $reflection->getDocBlock(); $this->_types[$typeName]['documentation'] = $docBlock ? $this->_getDescription($docBlock) : ''; $defaultProperties = $reflection->getDefaultProperties(); /** @var \Zend\Code\Reflection\PropertyReflection $property */ foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { $this->_processProperty($property, $defaultProperties, $typeName); } } return $this->_types[$typeName]; }
/** * Copied from ClassGenerator::fromReflection and tweaked slightly * @param ClassReflection $classReflection * * @return ClassGenerator */ public function getGeneratorFromReflection(ClassReflection $classReflection) { // class generator $cg = new ClassGenerator($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $docblock = DocBlockGenerator::fromReflection($classReflection->getDocBlock()); $docblock->setIndentation(Generator::$indentation); $cg->setDocBlock($docblock); } $cg->setAbstract($classReflection->isAbstract()); // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } /* @var \Zend\Code\Reflection\ClassReflection $parentClass */ $parentClass = $classReflection->getParentClass(); if ($parentClass) { $cg->setExtendedClass('\\' . ltrim($parentClass->getName(), '\\')); $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces()); } else { $interfaces = $classReflection->getInterfaces(); } $interfaceNames = array(); foreach ($interfaces as $interface) { /* @var \Zend\Code\Reflection\ClassReflection $interface */ $interfaceNames[] = $interface->getName(); } $cg->setImplementedInterfaces($interfaceNames); $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) { $property = PropertyGenerator::fromReflection($reflectionProperty); $property->setIndentation(Generator::$indentation); $properties[] = $property; } } $cg->addProperties($properties); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $method = MethodGenerator::fromReflection($reflectionMethod); $method->setBody(preg_replace("/^\\s+/m", '', $method->getBody())); $method->setIndentation(Generator::$indentation); $methods[] = $method; } } $cg->addMethods($methods); return $cg; }