Skip to content

Commit

Permalink
simplified #2
Browse files Browse the repository at this point in the history
  • Loading branch information
abbadon1334 committed Jul 11, 2019
1 parent 8798cbc commit 10a9d24
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 77 deletions.
41 changes: 41 additions & 0 deletions docs/api/JuliusHaertl/PHPDocToRst/Builder/PhpDomainBuilder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,44 @@ Methods
.. rst-class:: private

.. php:method:: private processMethodArgumentDocs( $method, $params)
:Parameters:
* **$method** (:any:`phpDocumentor\\Reflection\\Php\\Method <phpDocumentor\\Reflection\\Php\\Method>`)
* **$params** (array)


:Returns: string



.. rst-class:: private

.. php:method:: private processMethodArgumentTypes( $method)
:Parameters:
* **$method** (:any:`phpDocumentor\\Reflection\\Php\\Method <phpDocumentor\\Reflection\\Php\\Method>`)


:Returns: string



.. rst-class:: private

.. php:method:: private processMethodArgumentType( $argument, $args)
:Parameters:
* **$argument** (:any:`phpDocumentor\\Reflection\\Php\\Argument <phpDocumentor\\Reflection\\Php\\Argument>`)
* **$args** (string)


:Returns: string



182 changes: 105 additions & 77 deletions src/Builder/PhpDomainBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,77 +216,71 @@ public function addDocBlockDescription($element)
*/
protected function addDocblockTag($tagName, DocBlock $docBlock)
{
$inclusion_tag_name = [
'return',
'var',
'throws',
'since',
'deprecated',
'see',
'license',
];

$tags = $docBlock->getTagsByName($tagName);

if(!in_array($tagName,$inclusion_tag_name))
{
return '';
}

if(in_array($tagName,$inclusion_tag_name) && count($tags) === 0)
{
return '';
}

switch ($tagName) {
case 'return':
if (count($tags) === 0) {
return;
}
/** @var Return_ $return */
$return = $tags[0];
$this->addMultiline(':Returns: ' . self::typesToRst($return->getType()) . ' ' . RstBuilder::escape($return->getDescription()),
true);
break;
case 'var':
if (count($tags) === 0) {
return;
}
/** @var DocBlock\Tags\Var_ $return */
$return = $tags[0];
$this->addMultiline(':Type: ' . self::typesToRst($return->getType()) . ' ' . RstBuilder::escape($return->getDescription()),
true);
break;
case 'throws':
if (count($tags) === 0) {
return;
}
/** @var Throws $tag */
foreach ($tags as $tag) {
$this->addMultiline(':Throws: ' . self::typesToRst($tag->getType()) . ' ' . RstBuilder::escape($tag->getDescription()),
true);
}
break;
case 'since':
if (count($tags) === 0) {
return;
}
/** @var Since $return */
$return = $tags[0];
$this->addMultiline(':Since: ' . $return->getVersion() . ' ' . RstBuilder::escape($return->getDescription()),
true);
break;
case 'deprecated':
if (count($tags) === 0) {
return;
}
/** @var Deprecated $return */
$return = $tags[0];
$this->addMultiline(':Deprecated: ' . $return->getVersion() . ' ' . RstBuilder::escape($return->getDescription()),
true);
break;
case 'see':
if (count($tags) === 0) {
return;
}
/** @var See $return */
$return = $tags[0];
$this->addMultiline(':See: ' . self::typesToRst($return->getReference()) . ' ' . RstBuilder::escape($return->getDescription()),
true);
break;
case 'license':
if (count($tags) === 0) {
return;
}
/** @var DocBlock\Tags\BaseTag $return */
$return = $tags[0];
$this->addMultiline(':License: ' . RstBuilder::escape($return->getDescription()), true);
break;
case 'param':
// param handling is done by subclasses since it is more that docbook parsing
break;
default:
//echo 'Tag handling not defined for: ' . $tag . PHP_EOL;
break;
}

}
Expand Down Expand Up @@ -455,29 +449,6 @@ private function addMethod(Method $method)
}
$deprecated = $docBlock->getTagsByName('deprecated');
}
$args = '';
/** @var Argument $argument */
foreach ($method->getArguments() as $argument) {
// This will work after https://github.com/phpDocumentor/Reflection/pull/109 is merged
foreach ($argument->getType() as $type) {
$args .= self::escape($type) . '|';
}
$args = substr($args, 0, -1) . ' ';
if ($argument->isVariadic()) {
$args .= '...';
}
if ($argument->isByReference()) {
$args .= '&';
}
$args .= '$' . $argument->getName();
$default = $argument->getDefault();
if ($default !== NULL) {
$default = $default === '' ? '""' : $default;
$args .= '=' . self::escape($default);
}
$args .= ', ';
}
$args = substr($args, 0, -2);

$modifiers = $method->getVisibility();
$modifiers .= $method->isAbstract() ? ' abstract' : '';
Expand All @@ -486,37 +457,13 @@ private function addMethod(Method $method)
$deprecated = count($deprecated) > 0 ? ' deprecated' : '';
$this->addLine('.. rst-class:: ' . $modifiers . $deprecated)->addLine();
$this->indent();

$args = $this->processMethodArgumentTypes($method);
$this->beginPhpDomain('method', $modifiers . ' ' . $method->getName() . '(' . $args . ')');
$this->addDocBlockDescription($method);
$this->addLine();
if (!empty($params)) {
$parameterDetails = '';
foreach ($method->getArguments() as $argument) {
if (!array_key_exists($argument->getName(), $params)) {
continue;
}
/** @var Param $param */
$param = $params[$argument->getName()];
if ($param !== NULL) {
$typString = $param->getType();
// Remove first \ to allow references
if (0 === strpos($typString, '\\')) {
$typString = substr($typString, 1);
}
$paramItem = '* ';
$paramItem .= '**';
if($argument->isVariadic())
{
$paramItem .= '...';
}
$paramItem .= '$' . $argument->getName() . '** ';
if ($typString !== NULL) {
$paramItem .= '(' . self::typesToRst($typString) . ') ';
}
$paramItem .= ' ' . $param->getDescription();
$parameterDetails .= $paramItem . PHP_EOL;
}
}
$parameterDetails = $this->processMethodArgumentDocs($method, $params);
$this->addFieldList('Parameters', $parameterDetails);
}
if ($docBlock !== NULL) {
Expand All @@ -528,5 +475,86 @@ private function addMethod(Method $method)
$this->unindent();
}

/**
* @param Method $method
* @param array $params
*
* @return string
*/
private function processMethodArgumentDocs(Method $method, array $params): string
{
$parameterDetails = '';
foreach ($method->getArguments() as $argument) {
if (!array_key_exists($argument->getName(), $params)) {
continue;
}
/** @var Param $param */
$param = $params[$argument->getName()];
if ($param !== NULL) {
$typString = $param->getType();
// Remove first \ to allow references
if (0 === strpos($typString, '\\')) {
$typString = substr($typString, 1);
}
$paramItem = '* ';
$paramItem .= '**';
if ($argument->isVariadic()) {
$paramItem .= '...';
}
$paramItem .= '$' . $argument->getName() . '** ';
if ($typString !== NULL) {
$paramItem .= '(' . self::typesToRst($typString) . ') ';
}
$paramItem .= ' ' . $param->getDescription();
$parameterDetails .= $paramItem . PHP_EOL;
}
}
return $parameterDetails;
}

/**
* @param Method $method
*
* @return string
*/
private function processMethodArgumentTypes(Method $method): string
{
$args = '';
/** @var Argument $argument */
foreach ($method->getArguments() as $argument) {
$args= $this->processMethodArgumentType($argument, $args);
}
$args = substr($args, 0, -2);
return $args;
}

/**
* @param Argument $argument
* @param string $args
*
* @return string
*/
private function processMethodArgumentType(Argument $argument, string $args): string
{
foreach ($argument->getType() as $type) {
$args .= self::escape($type) . '|';
}
$args = substr($args, 0, -1) . ' ';
if ($argument->isVariadic()) {
$args .= '...';
}
if ($argument->isByReference()) {
$args .= '&';
}
$args .= '$' . $argument->getName();
$default = $argument->getDefault();
if ($default !== NULL) {
$default = $default === '' ? '""' : $default;
$args .= '=' . self::escape($default);
}
$args .= ', ';
return $args;
}


}

0 comments on commit 10a9d24

Please sign in to comment.