Skip to content

Commit 6674a3e

Browse files
authored
Fix #288: Improve handling of trait inheritance - precedence, prevent methods' and properties' duplication
1 parent 9bff4a3 commit 6674a3e

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Yii Framework 2 apidoc extension Change Log
44
3.0.6 under development
55
-----------------------
66

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

99

1010
3.0.5 April 21, 2022

models/Context.php

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,7 @@ public function updateReferences()
120120
}
121121
// update implementedBy and usedBy for traits
122122
foreach ($this->classes as $class) {
123-
foreach ($class->traits as $trait) {
124-
if (isset($this->traits[$trait])) {
125-
$trait = $this->traits[$trait];
126-
$trait->usedBy[] = $class->name;
127-
$class->properties = array_merge($trait->properties, $class->properties);
128-
$class->methods = array_merge($trait->methods, $class->methods);
129-
}
130-
}
123+
$this->handleTraitInheritance($class);
131124
}
132125
foreach ($this->interfaces as $interface) {
133126
foreach ($interface->parentInterfaces as $pInterface) {
@@ -140,22 +133,24 @@ public function updateReferences()
140133
foreach ($this->classes as $class) {
141134
$this->inheritDocs($class);
142135
}
143-
// inherit properties, methods, constants and events from subclasses
136+
// inherit properties, methods, constants and events from parent classes
144137
foreach ($this->classes as $class) {
145138
$this->handleClassInheritance($class);
146139
}
147140
// update implementedBy and usedBy for interfaces
148141
foreach ($this->classes as $class) {
149142
foreach ($class->interfaces as $interface) {
150-
if (isset($this->interfaces[$interface])) {
151-
$this->interfaces[$interface]->implementedBy[] = $class->name;
152-
if ($class->isAbstract) {
153-
// add not implemented interface methods
154-
foreach ($this->interfaces[$interface]->methods as $method) {
155-
if (!isset($class->methods[$method->name])) {
156-
$class->methods[$method->name] = $method;
157-
}
158-
}
143+
if (!isset($this->interfaces[$interface])) {
144+
continue;
145+
}
146+
$this->interfaces[$interface]->implementedBy[] = $class->name;
147+
if (!$class->isAbstract) {
148+
continue;
149+
}
150+
// add not implemented interface methods
151+
foreach ($this->interfaces[$interface]->methods as $method) {
152+
if (!isset($class->methods[$method->name])) {
153+
$class->methods[$method->name] = $method;
159154
}
160155
}
161156
}
@@ -202,6 +197,33 @@ protected function updateSubclassInheritance($class)
202197
}
203198
}
204199

200+
/**
201+
* @param ClassDoc $class
202+
*/
203+
protected function handleTraitInheritance($class)
204+
{
205+
foreach ($class->traits as $traitName) {
206+
if (!isset($this->traits[$traitName])) {
207+
continue;
208+
}
209+
210+
$trait = $this->traits[$traitName];
211+
$trait->usedBy[] = $class->name;
212+
213+
foreach ($trait->properties as $property) {
214+
if (!isset($class->properties[$property->name])) {
215+
$class->properties[$property->name] = $property;
216+
}
217+
}
218+
219+
foreach ($trait->methods as $method) {
220+
if (!isset($class->methods[$method->name])) {
221+
$class->methods[$method->name] = $method;
222+
}
223+
}
224+
}
225+
}
226+
205227
/**
206228
* @param ClassDoc $class
207229
*/
@@ -219,11 +241,14 @@ protected function handleClassInheritance($class)
219241

220242
foreach ($attrNames as $attrName) {
221243
foreach ($parent->$attrName as $item) {
222-
if (isset($class->$attrName[$item->name])) {
244+
if (
245+
isset($class->$attrName[$item->name]) &&
246+
!isset($this->traits[$class->$attrName[$item->name]->definedBy])
247+
) {
223248
continue;
224249
}
225250

226-
$class->$attrName += [$item->name => $item];
251+
$class->$attrName[$item->name] = $item;
227252
}
228253
}
229254
}

0 commit comments

Comments
 (0)