Skip to content

Commit

Permalink
implement @inheritdoc for properties
Browse files Browse the repository at this point in the history
also fixes it for methods
  • Loading branch information
cebe committed Feb 6, 2016
1 parent 9cfac2c commit bab23f2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
14 changes: 14 additions & 0 deletions models/BaseDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public function removeTag($name)
}
}

/**
* Get the first tag of a given name
* @param string $name
* @return \phpDocumentor\Reflection\DocBlock\Tag
*/
public function getFirstTag($name)
{
foreach ($this->tags as $i => $tag) {
if (strtolower($tag->getName()) == $name) {
return $this->tags[$i];
}
}
}


/**
* @param \phpDocumentor\Reflection\BaseReflector $reflector
Expand Down
67 changes: 62 additions & 5 deletions models/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,35 @@ protected function updateSubInterfaceInheritance($interface)
*/
protected function inheritDocs($class)
{
// TODO also for properties?
// inherit for properties
foreach ($class->properties as $p) {
if ($p->hasTag('inheritdoc')) {
$inheritedProperty = $this->inheritPropertyRecursive($p, $class);
if (!$inheritedProperty) {
$this->errors[] = [
'line' => $p->startLine,
'file' => $class->sourceFile,
'message' => "Method {$p->name} has no parent to inherit from in {$class->name}.",
];
continue;
}

// set all properties that are empty.
foreach (['shortDescription', 'description', 'type', 'types'] as $property) {
if (empty($p->$property) || is_string($p->$property) && trim($p->$property) === '') {
$p->$property = $inheritedProperty->$property;
}
}
// descriptions will be concatenated.
$p->description = trim($p->description) . "\n\n"
. trim($inheritedProperty->description) . "\n\n"
. $p->getFirstTag('inheritdoc')->getContent();

$p->removeTag('inheritdoc');
}
}

// inherit for methods
foreach ($class->methods as $m) {
if ($m->hasTag('inheritdoc')) {
$inheritedMethod = $this->inheritMethodRecursive($m, $class);
Expand All @@ -211,14 +239,17 @@ protected function inheritDocs($class)
];
continue;
}
foreach (['shortDescription', 'description', 'return', 'returnType', 'returnTypes', 'exceptions'] as $property) {
// set all properties that are empty. descriptions will be concatenated.
// set all properties that are empty.
foreach (['shortDescription', 'return', 'returnType', 'returnTypes', 'exceptions'] as $property) {
if (empty($m->$property) || is_string($m->$property) && trim($m->$property) === '') {
$m->$property = $inheritedMethod->$property;
} elseif ($property == 'description') {
$m->$property = rtrim($m->$property) . "\n\n" . ltrim($inheritedMethod->$property);
}
}
// descriptions will be concatenated.
$m->description = trim($m->description) . "\n\n"
. trim($inheritedMethod->description) . "\n\n"
. $m->getFirstTag('inheritdoc')->getContent();

foreach ($m->params as $i => $param) {
if (!isset($inheritedMethod->params[$i])) {
$this->errors[] = [
Expand Down Expand Up @@ -269,6 +300,32 @@ private function inheritMethodRecursive($method, $class)
return reset($methods);
}

/**
* @param PropertyDoc $method
* @param ClassDoc $class
* @return mixed
*/
private function inheritPropertyRecursive($method, $class)
{
$inheritanceCandidates = array_merge(
$this->getParents($class),
$this->getInterfaces($class)
);

$properties = [];
foreach($inheritanceCandidates as $candidate) {
if (isset($candidate->properties[$method->name])) {
$cproperty = $candidate->properties[$method->name];
if ($cproperty->hasTag('inheritdoc')) {
$this->inheritDocs($candidate);
}
$properties[] = $cproperty;
}
}

return reset($properties);
}

/**
* @param ClassDoc $class
* @return array
Expand Down

0 comments on commit bab23f2

Please sign in to comment.