MethodConditionWithInheritDocFetcher   A
last analyzed

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