|
10 | 10 |
|
11 | 11 | namespace PhpDeal\Contract\Fetcher\ParentClass;
|
12 | 12 |
|
13 |
| -use Doctrine\Common\Annotations\Reader; |
14 | 13 | use ReflectionClass;
|
15 |
| -use ReflectionMethod; |
16 | 14 |
|
17 |
| -class MethodConditionWithInheritDocFetcher extends Fetcher |
| 15 | +class MethodConditionWithInheritDocFetcher extends AbstractFetcher |
18 | 16 | {
|
19 | 17 | /**
|
| 18 | + * Fetches conditions from all parent method prototypes recursively |
| 19 | + * |
20 | 20 | * @param ReflectionClass $class
|
21 |
| - * @param Reader $reader |
22 | 21 | * @param string $methodName
|
23 |
| - * @param array $contracts |
| 22 | + * |
24 | 23 | * @return array
|
25 | 24 | */
|
26 |
| - public function getConditions(ReflectionClass $class, Reader $reader, $methodName, array $contracts = []) |
| 25 | + public function getConditions(ReflectionClass $class, $methodName) |
27 | 26 | {
|
28 |
| - if ($this->hasInheritDoc($class->getMethod($methodName))) { |
29 |
| - return $this->getConditionsWithInheritDoc($class, $reader, $methodName, $contracts); |
| 27 | + $annotations = []; |
| 28 | + $parentMethods = []; |
| 29 | + while ( |
| 30 | + preg_match('/\@inheritdoc/i', $class->getMethod($methodName)->getDocComment()) |
| 31 | + && ($class = $class->getParentClass()) |
| 32 | + && $class->hasMethod($methodName) |
| 33 | + ) { |
| 34 | + $parentMethods[] = $class->getMethod($methodName); |
30 | 35 | }
|
31 | 36 |
|
32 |
| - return $contracts; |
33 |
| - } |
34 |
| - |
35 |
| - /** |
36 |
| - * @param ReflectionClass $class |
37 |
| - * @param Reader $reader |
38 |
| - * @param string $methodName |
39 |
| - * @param array $contracts |
40 |
| - * @return array |
41 |
| - */ |
42 |
| - private function getConditionsWithInheritDoc(ReflectionClass $class, Reader $reader, $methodName, array $contracts) |
43 |
| - { |
44 |
| - $parentClass = $class->getParentClass(); |
45 |
| - if (!$parentClass) { |
46 |
| - return $contracts; |
47 |
| - } |
48 |
| - |
49 |
| - $parentMethod = $parentClass->getMethod($methodName); |
50 |
| - $annotations = $reader->getMethodAnnotations($parentMethod); |
51 |
| - $contractAnnotations = $this->getContractAnnotations($annotations); |
52 |
| - $contracts = array_merge($contracts, $contractAnnotations); |
53 |
| - |
54 |
| - if ($this->hasInheritDoc($parentMethod)) { |
55 |
| - return $this->getConditionsWithInheritDoc($parentClass, $reader, $methodName, $contracts); |
| 37 | + foreach ($parentMethods as $parentMethod) { |
| 38 | + $annotations = array_merge($annotations, $this->annotationReader->getMethodAnnotations($parentMethod)); |
56 | 39 | }
|
| 40 | + $contracts = $this->filterContractAnnotation($annotations); |
57 | 41 |
|
58 | 42 | return $contracts;
|
59 | 43 | }
|
60 |
| - |
61 |
| - /** |
62 |
| - * @param ReflectionMethod $method |
63 |
| - * @return bool |
64 |
| - */ |
65 |
| - private function hasInheritDoc(ReflectionMethod $method) |
66 |
| - { |
67 |
| - return preg_match('/\@inheritdoc/i', $method->getDocComment()) > 0; |
68 |
| - } |
69 | 44 | }
|
0 commit comments