Completed
Pull Request — master (#35)
by
unknown
14:05
created

MethodConditionWithInheritDocFetcher   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 73
ccs 26
cts 27
cp 0.963
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConditions() 0 25 5
A getParentClassesMethods() 0 14 3
A getInterfacesMethods() 0 8 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * PHP Deal framework
6
 *
7
 * @copyright Copyright 2019, Lisachenko Alexander <[email protected]>
8
 *
9
 * This source file is subject to the license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace PhpDeal\Contract\Fetcher\Parent;
14
15
use ReflectionClass;
16
use ReflectionException;
17
18
class MethodConditionWithInheritDocFetcher extends AbstractFetcher
19
{
20
    /**
21
     * Fetches conditions from all parent method prototypes recursively
22
     *
23
     * @param ReflectionClass $class
24
     * @param string          $methodName
25
     *
26
     * @return array
27
     * @throws ReflectionException
28
     */
29 11
    public function getConditions(ReflectionClass $class, string $methodName): array
30
    {
31 11
        $annotations   = [];
32 11
        $parentMethods = [];
33 11
        $docComment    = $class->getMethod($methodName)->getDocComment();
34
35 11
        if ($docComment === false) {
36
            return $annotations;
37
        }
38
39 11
        if (\preg_match('/\@inheritdoc/i', $docComment)) {
40 6
            $this->getParentClassesMethods($class, $methodName, $parentMethods);
41 6
            $this->getInterfacesMethods($class, $methodName, $parentMethods);
42
        }
43
44 11
        foreach ($parentMethods as $parentMethod) {
45 6
            $annotations[] = $this->annotationReader->getMethodAnnotations($parentMethod);
46
        }
47
48 11
        if (\count($annotations)) {
49 6
            $annotations = \array_merge(...$annotations);
50
        }
51
52 11
        return $this->filterContractAnnotation($annotations);
53
    }
54
55
    /**
56
     * @param ReflectionClass $class
57
     * @param string          $methodName
58
     * @param array           $parentMethods
59
     * @throws ReflectionException
60
     */
61 6
    private function getParentClassesMethods(ReflectionClass $class, string $methodName, array &$parentMethods): void
62
    {
63 6
        $parent = $class->getParentClass();
64
65 6
        if ($parent === false) {
66 6
            return;
67
        }
68
69 3
        if ($parent->hasMethod($methodName)) {
70 3
            $parentMethods[] = $parent->getMethod($methodName);
71
        }
72
73 3
        $this->getParentClassesMethods($parent, $methodName, $parentMethods);
74 3
    }
75
76
    /**
77
     * @param ReflectionClass $class
78
     * @param string          $methodName
79
     * @param array           $parentMethods
80
     * @throws ReflectionException
81
     */
82 6
    private function getInterfacesMethods(ReflectionClass $class, $methodName, &$parentMethods): void
83
    {
84 6
        foreach ($class->getInterfaces() as $interface) {
85 6
            if ($interface->hasMethod($methodName)) {
86 3
                $parentMethods[] = $interface->getMethod($methodName);
87
            }
88
        }
89 6
    }
90
}
91