MethodConditionFetcher   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 66
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConditions() 0 18 3
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
18
class MethodConditionFetcher 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 10
    public function getConditions(ReflectionClass $class, string $methodName): array
30
    {
31 10
        $annotations = [];
32 10
        $parentMethods = [];
33
34 10
        $this->getParentClassesMethods($class, $methodName, $parentMethods);
35 10
        $this->getInterfacesMethods($class, $methodName, $parentMethods);
36
37 10
        foreach ($parentMethods as $parentMethod) {
38 8
            $annotations[] = $this->annotationReader->getMethodAnnotations($parentMethod);
39
        }
40
41 10
        if (\count($annotations) > 0) {
42 8
            $annotations = \array_merge(...$annotations);
43
        }
44
45 10
        return $this->filterContractAnnotation($annotations);
46
    }
47
48
    /**
49
     * @param ReflectionClass $class
50
     * @param string          $methodName
51
     * @param array           $parentMethods
52
     * @throws \ReflectionException
53
     */
54 10
    private function getParentClassesMethods(ReflectionClass $class, string $methodName, array &$parentMethods): void
55
    {
56 10
        $parent = $class->getParentClass();
57
58 10
        if ($parent === false) {
59 10
            return;
60
        }
61
62 3
        if ($parent->hasMethod($methodName)) {
63 3
            $parentMethods[] = $parent->getMethod($methodName);
64
        }
65
66 3
        $this->getParentClassesMethods($parent, $methodName, $parentMethods);
67 3
    }
68
69
    /**
70
     * @param ReflectionClass $class
71
     * @param string          $methodName
72
     * @param array           $parentMethods
73
     * @throws \ReflectionException
74
     */
75 10
    private function getInterfacesMethods(ReflectionClass $class, $methodName, &$parentMethods): void
76
    {
77 10
        foreach ($class->getInterfaces() as $interface) {
78 8
            if ($interface->hasMethod($methodName)) {
79 5
                $parentMethods[] = $interface->getMethod($methodName);
80
            }
81
        }
82 10
    }
83
}
84