Skip to content

Commit

Permalink
Fix #288: Improve handling of trait inheritance - precedence, prevent…
Browse files Browse the repository at this point in the history
… methods' and properties' duplication
  • Loading branch information
arogachev authored Sep 15, 2022
1 parent 9bff4a3 commit 6674a3e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yii Framework 2 apidoc extension Change Log
3.0.6 under development
-----------------------

- no changes in this release.
- Bug #288: Improve handling of trait inheritance - precedence, prevent methods' and properties' duplication (arogachev)


3.0.5 April 21, 2022
Expand Down
65 changes: 45 additions & 20 deletions models/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,7 @@ public function updateReferences()
}
// update implementedBy and usedBy for traits
foreach ($this->classes as $class) {
foreach ($class->traits as $trait) {
if (isset($this->traits[$trait])) {
$trait = $this->traits[$trait];
$trait->usedBy[] = $class->name;
$class->properties = array_merge($trait->properties, $class->properties);
$class->methods = array_merge($trait->methods, $class->methods);
}
}
$this->handleTraitInheritance($class);
}
foreach ($this->interfaces as $interface) {
foreach ($interface->parentInterfaces as $pInterface) {
Expand All @@ -140,22 +133,24 @@ public function updateReferences()
foreach ($this->classes as $class) {
$this->inheritDocs($class);
}
// inherit properties, methods, constants and events from subclasses
// inherit properties, methods, constants and events from parent classes
foreach ($this->classes as $class) {
$this->handleClassInheritance($class);
}
// update implementedBy and usedBy for interfaces
foreach ($this->classes as $class) {
foreach ($class->interfaces as $interface) {
if (isset($this->interfaces[$interface])) {
$this->interfaces[$interface]->implementedBy[] = $class->name;
if ($class->isAbstract) {
// add not implemented interface methods
foreach ($this->interfaces[$interface]->methods as $method) {
if (!isset($class->methods[$method->name])) {
$class->methods[$method->name] = $method;
}
}
if (!isset($this->interfaces[$interface])) {
continue;
}
$this->interfaces[$interface]->implementedBy[] = $class->name;
if (!$class->isAbstract) {
continue;
}
// add not implemented interface methods
foreach ($this->interfaces[$interface]->methods as $method) {
if (!isset($class->methods[$method->name])) {
$class->methods[$method->name] = $method;
}
}
}
Expand Down Expand Up @@ -202,6 +197,33 @@ protected function updateSubclassInheritance($class)
}
}

/**
* @param ClassDoc $class
*/
protected function handleTraitInheritance($class)
{
foreach ($class->traits as $traitName) {
if (!isset($this->traits[$traitName])) {
continue;
}

$trait = $this->traits[$traitName];
$trait->usedBy[] = $class->name;

foreach ($trait->properties as $property) {
if (!isset($class->properties[$property->name])) {
$class->properties[$property->name] = $property;
}
}

foreach ($trait->methods as $method) {
if (!isset($class->methods[$method->name])) {
$class->methods[$method->name] = $method;
}
}
}
}

/**
* @param ClassDoc $class
*/
Expand All @@ -219,11 +241,14 @@ protected function handleClassInheritance($class)

foreach ($attrNames as $attrName) {
foreach ($parent->$attrName as $item) {
if (isset($class->$attrName[$item->name])) {
if (
isset($class->$attrName[$item->name]) &&
!isset($this->traits[$class->$attrName[$item->name]->definedBy])
) {
continue;
}

$class->$attrName += [$item->name => $item];
$class->$attrName[$item->name] = $item;
}
}
}
Expand Down

0 comments on commit 6674a3e

Please sign in to comment.