Completed
Push — master ( 2f9c4a...218c91 )
by Alexander
02:43
created

MethodConditionFetcher::getConditions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * PHP Deal framework
4
 *
5
 * @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace PhpDeal\Contract\Fetcher\Parent;
12
13
use ReflectionClass;
14
15
class MethodConditionFetcher extends AbstractFetcher
16
{
17
    /**
18
     * Fetches conditions from all parent method prototypes recursively
19
     *
20
     * @param ReflectionClass $class
21
     * @param string $methodName
22
     *
23
     * @return array
24
     */
25 8
    public function getConditions(ReflectionClass $class, $methodName)
26
    {
27 8
        $annotations   = [];
28 8
        $parentMethods = [];
29
30 8
        $this->getParentClassesMethods($class, $methodName, $parentMethods);
31 8
        $this->getInterfacesMethods($class, $methodName, $parentMethods);
32
33 8
        foreach ($parentMethods as $parentMethod) {
34 6
            $annotations = array_merge($annotations, $this->annotationReader->getMethodAnnotations($parentMethod));
35
        }
36 8
        $contracts = $this->filterContractAnnotation($annotations);
37
38 8
        return $contracts;
39
    }
40
41
    /**
42
     * @param ReflectionClass $class
43
     * @param string $methodName
44
     * @param array $parentMethods
45
     */
46 8
    private function getParentClassesMethods(ReflectionClass $class, $methodName, &$parentMethods)
47
    {
48 8
        while (($class = $class->getParentClass()) && $class->hasMethod($methodName)) {
49 3
            $parentMethods[] = $class->getMethod($methodName);
50
        }
51 8
    }
52
53
    /**
54
     * @param ReflectionClass $class
55
     * @param string $methodName
56
     * @param array $parentMethods
57
     */
58 8
    private function getInterfacesMethods(ReflectionClass $class, $methodName, &$parentMethods)
59
    {
60 8
        $interfaces = $class->getInterfaces();
61
62 8
        foreach ($interfaces as $interface) {
63 6
            if ($interface->hasMethod($methodName)) {
64 6
                $parentMethods[] = $interface->getMethod($methodName);
65
            }
66
        }
67 8
    }
68
}
69