Completed
Push — master ( 8adc32...f2957c )
by Boy
02:42
created

BaseReflector::getImplemented()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Atreyu;
4
5
use phpDocumentor\Reflection\DocBlock;
6
7
abstract class BaseReflector
8
{
9
    /**
10
     * @param array                $docBlockParams
11
     * @param \ReflectionParameter $param
12
     * @param array                $arguments
13
     *
14
     * @return bool
15
     */
16
    public function getParamDocBlockHint(array $docBlockParams, \ReflectionParameter $param, array $arguments = [])
17
    {
18
        $typeHint = false;
19
20
        /** @var DocBlock\Tag\ParamTag $docBlockParam */
21
        foreach ($docBlockParams as $docBlockParam) {
22
23
            if (!($docBlockParam instanceof DocBlock\Tag\ParamTag)) {
24
                continue;
25
            }
26
27
            $type = $docBlockParam->getType();
28
            $docBlockParamName = $docBlockParam->getVariableName();
29
30
            if (($param->getName() === ltrim($docBlockParamName, '$'))
31
                && (!empty($type))
32
            ) {
33
                $definitions = explode('|', $type);
34
35
                foreach ($arguments as $key => $argument) {
36
37
                    foreach ($definitions as $definition) {
38
39
                        if (is_object($argument)
40
                            && in_array(ltrim($definition, '\\'), $this->getImplemented(get_class($argument)))
41
                            && (is_numeric($key) || (ltrim($docBlockParamName, '$') === $key)
42
                            )) {
43
                            $typeHint = $definition;
44
45
                            // no need to loop again, since we found a match already!
46
                            continue 3;
47
                        }
48
                    }
49
                }
50
51
                if ($typeHint === false) {
52
53
                    // use first definition, there is no way to know which instance of the hinted doc block definitions is actually required
54
                    // because there were either no arguments given or no argument match was found
55
                    list($typeHint,) = $definitions;
56
                }
57
            }
58
        }
59
60
        return $typeHint;
61
    }
62
63
    /**
64
     * @param $className
65
     *
66
     * @return array
67
     */
68
    public function getImplemented($className)
69
    {
70
        return array_merge([$className], class_implements($className));
71
    }
72
}
73