/** * Analyse $class for extract self content * * @return $this */ protected function reflect() { $reflection = $this->getClass(); // docblock $docblock = new \phpDocumentor\Reflection\DocBlock($reflection->getDocComment()); $this->setSummary($docblock->getShortDescription()); $this->setDescription($docblock->getLongDescription()); // NAMESPACE $namespace = $reflection->getNamespaceName(); if (!empty($namespace)) { $this->setNamespace($namespace); } // ALIAS $file = $reflection->getFileName(); $this->setFilename($file); // lecture du fichier if (file_exists($file)) { $file = file($file); //Première lignes du fichier foreach (array_slice($file, 0, $reflection->getStartLine()) as $line) { // analyse de partern d'alias if (preg_match('#use (?<class>[^\\s^;]+)(\\s+as\\s+(?<alias>[^\\s^;]+))?\\s*;?#', $line, $match)) { $class = $match['class']; $alias = empty($match['alias']) ? collect(explode('\\', $class))->last() : $match['alias']; $this->addAlias($alias, $class); } } } // PARENT CLASS $parent = $reflection->getParentClass(); if (!empty($parent)) { $this->setParent($parent->getName()); } // CONSTANT $this->setConstants($reflection->getConstants()); // PROPERTIES foreach ($reflection->getProperties() as $property) { $this->addProperty(Property::fromReflection($property)); } // METHODS foreach ($reflection->getImmediateMethods() as $method) { $this->addMethod(Method::fromReflection($method)); } return $this; }
/** * Rendu d'une methode * * @param Method $method * @return string */ public function method(Method $method) { $content = ''; // parameters $parameters = []; foreach ($method->getParameters() as $parameter) { $type = $parameter->getType(); $name = $parameter->getName(); $method->addTagParam($name, $type); $p = ''; if (!is_null($type)) { $p .= $type . ' '; } $p .= '$' . $name; if ($parameter->hasDefault()) { $p .= ' = ' . $this->render('value', $parameter->getDefault()); } $parameters[] = $p; } $content .= $this->render('docblock', $method); $content .= sprintf('%sfunction %s(%s)', $this->render('modifier', $method), $method->getName(), implode(', ', $parameters)) . PHP_EOL; $content .= '{' . PHP_EOL . "\t" . str_replace(PHP_EOL, PHP_EOL . "\t", $method->getBody()) . PHP_EOL . '}' . PHP_EOL; return $content; }