Zend\Code\Generator\ClassGenerator::fromReflection PHP Method

fromReflection() public static method

Build a Code Generation Php Object from a Class Reflection
public static fromReflection ( Zend\Code\Reflection\ClassReflection $classReflection ) : self
$classReflection Zend\Code\Reflection\ClassReflection
return self
    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 = [];
        foreach ($interfaces as $interface) {
            /* @var \Zend\Code\Reflection\ClassReflection $interface */
            $interfaceNames[] = $interface->getName();
        }
        $cg->setImplementedInterfaces($interfaceNames);
        $properties = [];
        foreach ($classReflection->getProperties() as $reflectionProperty) {
            if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) {
                $properties[] = PropertyGenerator::fromReflection($reflectionProperty);
            }
        }
        $cg->addProperties($properties);
        $constants = [];
        foreach ($classReflection->getConstants() as $name => $value) {
            $constants[] = ['name' => $name, 'value' => $value];
        }
        $cg->addConstants($constants);
        $methods = [];
        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;
    }

Usage Example

 /**
  * @param  FileReflection $fileReflection
  * @return FileGenerator
  */
 public static function fromReflection(FileReflection $fileReflection)
 {
     $file = new static();
     $file->setSourceContent($fileReflection->getContents());
     $file->setSourceDirty(false);
     $uses = $fileReflection->getUses();
     foreach ($fileReflection->getClasses() as $class) {
         $phpClass = ClassGenerator::fromReflection($class);
         $phpClass->setContainingFileGenerator($file);
         foreach ($uses as $fileUse) {
             $phpClass->addUse($fileUse['use'], $fileUse['as']);
         }
         $file->setClass($phpClass);
     }
     $namespace = $fileReflection->getNamespace();
     if ($namespace != '') {
         $file->setNamespace($namespace);
     }
     if ($uses) {
         $file->setUses($uses);
     }
     if ($fileReflection->getDocComment() != '') {
         $docBlock = $fileReflection->getDocBlock();
         $file->setDocBlock(DocBlockGenerator::fromReflection($docBlock));
     }
     return $file;
 }
All Usage Examples Of Zend\Code\Generator\ClassGenerator::fromReflection