public function testGenerationOfDocblock() { $docblockGenerator = new DocblockGenerator(); $docblockGenerator->setShortDescription('@var Foo this is foo bar'); $expected = '/**' . DocblockGenerator::LINE_FEED . ' * @var Foo this is foo bar' . DocblockGenerator::LINE_FEED . ' */' . DocblockGenerator::LINE_FEED; $this->assertEquals($expected, $docblockGenerator->generate()); }
/** * fromReflection() * * @param ReflectionProperty $reflectionProperty * @return PropertyGenerator */ public static function fromReflection(PropertyReflection $reflectionProperty) { $property = new self(); $property->setName($reflectionProperty->getName()); $allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties(); $property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]); if ($reflectionProperty->getDocComment() != '') { $property->setDocblock(DocblockGenerator::fromReflection($reflectionProperty->getDocComment())); } if ($reflectionProperty->isStatic()) { $property->setStatic(true); } if ($reflectionProperty->isPrivate()) { $property->setVisibility(self::VISIBILITY_PRIVATE); } elseif ($reflectionProperty->isProtected()) { $property->setVisibility(self::VISIBILITY_PROTECTED); } else { $property->setVisibility(self::VISIBILITY_PUBLIC); } $property->setSourceDirty(false); return $property; }
/** * fromReflection() * * @param \Zend\Reflection\ReflectionMethod $reflectionMethod * @return \MethodGenerator\Code\Generator\PhpMethod */ public static function fromReflection(MethodReflection $reflectionMethod) { $method = new self(); $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->setStatic($reflectionMethod->isStatic()); $method->setName($reflectionMethod->getName()); foreach ($reflectionMethod->getParameters() as $reflectionParameter) { $method->setParameter(ParameterGenerator::fromReflection($reflectionParameter)); } $method->setBody($reflectionMethod->getBody()); return $method; }
/** * fromReflection() * * @param FileReflection $fileReflection * @return FileGenerator */ public static function fromReflection(FileReflection $fileReflection) { $file = new self(); $file->setSourceContent($fileReflection->getContents()); $file->setSourceDirty(false); $body = $fileReflection->getContents(); foreach ($fileReflection->getClasses() as $class) { /* @var $class \Zend\Code\Reflection\ReflectionClass */ $phpClass = ClassGenerator::fromReflection($class); $phpClass->setContainingFileGenerator($file); $file->setClass($phpClass); $classStartLine = $class->getStartLine(true); $classEndLine = $class->getEndLine(); $bodyLines = explode("\n", $body); $bodyReturn = array(); for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) { if ($lineNum == $classStartLine) { $bodyReturn[] = str_replace('?', $class->getName(), '/* Zend_CodeGenerator_Php_File-ClassMarker: {?} */'); $lineNum = $classEndLine; } else { $bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion } } $body = implode("\n", $bodyReturn); unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine); } $namespace = $fileReflection->getNamespace(); if ($namespace != '') { $file->setNamespace($fileReflection->getNamespace()); } $uses = $fileReflection->getUses(); if ($uses) { $file->setUses($uses); } if ($fileReflection->getDocComment() != '') { /* @var $docblock \DocBlockReflection\Code\Reflection\ReflectionDocblock */ $docblock = $fileReflection->getDocblock(); $file->setDocblock(DocblockGenerator::fromReflection($docblock)); $bodyLines = explode("\n", $body); $bodyReturn = array(); for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) { if ($lineNum == $docblock->getStartLine()) { $bodyReturn[] = str_replace('?', $class->getName(), '/* Zend_CodeGenerator_Php_File-DocblockMarker */'); $lineNum = $docblock->getEndLine(); } else { $bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion } } $body = implode("\n", $bodyReturn); unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine); } $file->setBody($body); return $file; }
/** * * * @configkey name string [required] Class Name * @configkey filegenerator FileGenerator File generator that holds this class * @configkey namespacename string The namespace for this class * @configkey docblock string The docblock information * @configkey flags int Flags, one of ClassGenerator::FLAG_ABSTRACT ClassGenerator::FLAG_FINAL * @configkey extendedclass string Class which this class is extending * @configkey implementedinterfaces * @configkey properties * @configkey methods * * * @static * @throws Exception\InvalidArgumentException * @param array $array * @return ClassGenerator */ public static function fromArray(array $array) { if (!isset($array['name'])) { throw new Exception\InvalidArgumentException('Class generator requires that a name is provided for this object'); } $cg = new static($array['name']); foreach ($array as $name => $value) { // normalize key switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) { case 'containingfile': $cg->setContainingFileGenerator($value); break; case 'namespacename': $cg->setNamespaceName($value); break; case 'docblock': $cg->setDocblock(!$value instanceof DocblockGenerator ?: DocblockGenerator::fromArray($value)); break; case 'flags': $cg->setFlags($value); break; case 'extendedclass': $cg->setExtendedClass($value); break; case 'implementedinterfaces': $cg->setImplementedInterfaces($value); break; case 'properties': foreach ($value as $pValue) { $cg->setProperty(!$pValue instanceof PropertyGenerator ?: PropertyGenerator::fromArray($pValue)); } break; case 'methods': foreach ($value as $mValue) { $cg->setMethod(!$mValue instanceof MethodGenerator ?: MethodGenerator::fromArray($mValue)); } break; } } return $cg; }