Completed
Push — master ( bde278...56c4be )
by Alexander
9s
created

MethodConditionWithInheritDocFetcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 53
ccs 16
cts 17
cp 0.9412
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConditions() 0 8 2
A hasInheritDoc() 0 4 1
A getConditionsWithInheritDoc() 0 18 3
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\ParentClass;
12
13
use Doctrine\Common\Annotations\Reader;
14
use ReflectionClass;
15
use ReflectionMethod;
16
17
class MethodConditionWithInheritDocFetcher extends Fetcher
18
{
19
    /**
20
     * @param ReflectionClass $class
21
     * @param Reader $reader
22
     * @param string $methodName
23
     * @param array $contracts
24
     * @return array
25
     */
26 19
    public function getConditions(ReflectionClass $class, Reader $reader, $methodName, array $contracts = [])
27
    {
28 19
        if ($this->hasInheritDoc($class->getMethod($methodName))) {
29 4
            return $this->getConditionsWithInheritDoc($class, $reader, $methodName, $contracts);
30
        }
31
32 15
        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 4
    private function getConditionsWithInheritDoc(ReflectionClass $class, Reader $reader, $methodName, array $contracts)
43
    {
44 4
        $parentClass = $class->getParentClass();
45 4
        if (!$parentClass) {
46
            return $contracts;
47
        }
48
49 4
        $parentMethod = $parentClass->getMethod($methodName);
50 4
        $annotations = $reader->getMethodAnnotations($parentMethod);
51 4
        $contractAnnotations = $this->getContractAnnotations($annotations);
52 4
        $contracts = array_merge($contracts, $contractAnnotations);
53
54 4
        if ($this->hasInheritDoc($parentMethod)) {
55 4
            return $this->getConditionsWithInheritDoc($parentClass, $reader, $methodName, $contracts);
56
        }
57
58 4
        return $contracts;
59
    }
60
61
    /**
62
     * @param ReflectionMethod $method
63
     * @return bool
64
     */
65 19
    private function hasInheritDoc(ReflectionMethod $method)
66
    {
67 19
        return preg_match('/\@inheritdoc/i', $method->getDocComment()) > 0;
68
    }
69
}
70