public function testSetAndGetReturn() { $d = DocblockGenerator::factory('This is the description'); $d->setReturn('void'); $r = $d->getReturn(); $this->assertEquals('void', $r['type']); }
/** * Render method * * @param boolean $ret * @return mixed */ public function render($ret = false) { $this->output = null !== $this->namespace ? $this->namespace->render(true) . PHP_EOL : null; $this->output .= null !== $this->docblock ? $this->docblock->render(true) : null; $this->output .= 'interface ' . $this->name; if (null !== $this->parent) { $this->output .= ' extends ' . $this->parent; } $this->output .= PHP_EOL . '{' . PHP_EOL; $this->output .= $this->formatMethods() . PHP_EOL; $this->output .= '}' . PHP_EOL; if ($ret) { return $this->output; } else { echo $this->output; } }
/** * Render property * * @param boolean $ret * @return mixed */ public function render($ret = false) { $static = null; if ($this->visibility != 'const') { $varDeclaration = ' $'; if ($this->static) { $static = ' static'; } } else { $varDeclaration = ' '; } if (null === $this->docblock) { $this->docblock = new DocblockGenerator(null, $this->indent); } $this->docblock->setTag('var', $this->type); $this->output = PHP_EOL . $this->docblock->render(true); $this->output .= $this->indent . $this->visibility . $static . $varDeclaration . $this->name; if (null !== $this->value) { if ($this->type == 'array') { $val = count($this->value) == 0 ? 'array()' : $this->formatArrayValues(); $this->output .= ' = ' . $val . PHP_EOL; } else { if ($this->type == 'integer' || $this->type == 'int' || $this->type == 'float') { $this->output .= ' = ' . $this->value . ';'; } else { if ($this->type == 'boolean') { $val = $this->value ? 'true' : 'false'; $this->output .= " = " . $val . ";"; } else { $this->output .= " = '" . $this->value . "';"; } } } } else { $val = $this->type == 'array' ? 'array()' : 'null'; $this->output .= ' = ' . $val . ';'; } if ($ret) { return $this->output; } else { echo $this->output; } }
/** * Render method * * @param boolean $ret * @return mixed */ public function render($ret = false) { $args = $this->formatArguments(); $this->output = PHP_EOL . (null !== $this->docblock ? $this->docblock->render(true) : null); if ($this->closure) { $this->output .= $this->indent . '$' . $this->name . ' = function(' . $args . ')'; } else { $this->output .= $this->indent . 'function ' . $this->name . '(' . $args . ')'; } $this->output .= PHP_EOL . $this->indent . '{' . PHP_EOL; $this->output .= $this->body . PHP_EOL; $this->output .= $this->indent . '}'; if ($this->closure) { $this->output .= ';'; } $this->output .= PHP_EOL; if ($ret) { return $this->output; } else { echo $this->output; } }
/** * Render method * * @param boolean $ret * @return mixed */ public function render($ret = false) { $abstract = $this->abstract ? 'abstract ' : null; $this->output = null !== $this->namespace ? $this->namespace->render(true) . PHP_EOL : null; $this->output .= null !== $this->docblock ? $this->docblock->render(true) : null; $this->output .= $abstract . 'class ' . $this->name; if (null !== $this->parent) { $this->output .= ' extends ' . $this->parent; } if (null !== $this->interface) { $this->output .= ' implements ' . $this->interface; } $this->output .= PHP_EOL . '{'; $this->output .= $this->formatProperties() . PHP_EOL; $this->output .= $this->formatMethods() . PHP_EOL; $this->output .= '}' . PHP_EOL; if ($ret) { return $this->output; } else { echo $this->output; } }
/** * Render method * * @param boolean $ret * @return mixed */ public function render($ret = false) { $this->output = '<?php' . PHP_EOL; $this->output .= null !== $this->docblock ? $this->docblock->render(true) . PHP_EOL : null; if (null !== $this->namespace) { $this->output .= $this->namespace->render(true) . PHP_EOL; } if (null !== $this->code) { $this->output .= $this->code->render(true) . PHP_EOL; } if (null !== $this->body) { $this->output .= PHP_EOL . $this->body . PHP_EOL . PHP_EOL; } if ($this->close) { $this->output .= '?>' . PHP_EOL; } if ($ret) { return $this->output; } else { echo $this->output; } }
/** * Render method * * @param boolean $ret * @return mixed */ public function render($ret = false) { $final = $this->final ? 'final ' : null; $abstract = $this->abstract ? 'abstract ' : null; $static = $this->static ? ' static' : null; $args = $this->formatArguments(); $this->output = PHP_EOL . (null !== $this->docblock ? $this->docblock->render(true) : null); $this->output .= $this->indent . $final . $abstract . $this->visibility . $static . ' function ' . $this->name . '(' . $args . ')'; if (!$this->abstract && !$this->interface) { $this->output .= PHP_EOL . $this->indent . '{' . PHP_EOL; $this->output .= $this->body . PHP_EOL; $this->output .= $this->indent . '}'; } else { $this->output .= ';'; } $this->output .= PHP_EOL; if ($ret) { return $this->output; } else { echo $this->output; } }
/** * Render property * * @param boolean $ret * @return mixed */ public function render($ret = false) { $this->docblock = new DocblockGenerator(null, $this->indent); $this->docblock->setTag('namespace'); $this->output = $this->docblock->render(true); $this->output .= $this->indent . 'namespace ' . $this->namespace . ';' . PHP_EOL; if (count($this->use) > 0) { $this->output .= PHP_EOL; foreach ($this->use as $ns => $as) { $this->output .= $this->indent . 'use '; $this->output .= $ns; if (null !== $as) { $this->output .= ' as ' . $as; } $this->output .= ';' . PHP_EOL; } } if ($ret) { return $this->output; } else { echo $this->output; } }
/** * Get methods * * @return void */ protected function getClassMethods() { // Detect and set methods $methods = $this->getMethods(); if (count($methods) > 0) { foreach ($methods as $value) { $methodExport = \ReflectionMethod::export($value->class, $value->name, true); // Get the method docblock if (strpos($methodExport, '/*') !== false && strpos($methodExport, '*/') !== false) { $docBlock = substr($methodExport, strpos($methodExport, '/*')); $docBlock = substr($docBlock, 0, strpos($methodExport, '*/') + 2); } else { $docBlock = null; } $method = $this->getMethod($value->name); $visibility = 'public'; if ($method->isPublic()) { $visibility = 'public'; } else { if ($method->isProtected()) { $visibility = 'protected'; } else { if ($method->isPrivate()) { $visibility = 'private'; } } } $mthd = new Generator\MethodGenerator($value->name, $visibility, $method->isStatic()); if (null !== $docBlock && strpos($docBlock, '/*') !== false) { $mthd->setDocblock(Generator\DocblockGenerator::parse($docBlock, $mthd->getIndent())); } $mthd->setFinal($method->isFinal())->setAbstract($method->isAbstract()); // Get the method parameters if (stripos($methodExport, 'Parameter') !== false) { $matches = array(); preg_match_all('/Parameter \\#(.*)\\]/m', $methodExport, $matches); if (isset($matches[0][0])) { foreach ($matches[0] as $param) { $name = null; $value = null; $type = null; // Get name $name = substr($param, strpos($param, '$')); $name = trim(substr($name, 0, strpos($name, ' '))); // Get value if (strpos($param, '=') !== false) { $value = trim(substr($param, strpos($param, '=') + 1)); $value = trim(substr($value, 0, strpos($value, ' '))); $value = str_replace('NULL', 'null', $value); } // Get type $type = substr($param, strpos($param, '>') + 1); $type = trim(substr($type, 0, strpos($type, '$'))); if ($type == '') { $type = null; } $mthd->addArgument($name, $value, $type); } } } // Get method body if (strpos($methodExport, '@@') !== false && file_exists($this->getFilename())) { $lineNums = substr($methodExport, strpos($methodExport, '@@ ') + 3); $start = trim(substr($lineNums, strpos($lineNums, ' '))); $end = substr($start, strpos($start, ' - ') + 3); $start = substr($start, 0, strpos($start, ' ')); $end = (int) $end; if (is_numeric($start) && is_numeric($end)) { $classLines = file($this->getFilename()); $body = null; $start = $start + 1; $end = $end - 1; for ($i = $start; $i < $end; $i++) { if (isset($classLines[$i])) { if (substr($classLines[$i], 0, 8) == ' ') { $body .= substr($classLines[$i], 8); } else { if (substr($classLines[$i], 0, 4) == ' ') { $body .= substr($classLines[$i], 4); } else { if (substr($classLines[$i], 0, 2) == "\t\t") { $body .= substr($classLines[$i], 2); } else { if (substr($classLines[$i], 0, 1) == "\t") { $body .= substr($classLines[$i], 1); } else { $body .= $classLines[$i]; } } } } } } $mthd->setBody(rtrim($body), false); } } $this->generator->code()->addMethod($mthd); } } }