BaseReflector   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 1
dl 0
loc 71
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getParamDocBlockHint() 0 51 13
A getImplemented() 0 4 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|string
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($firstDefinition,) = $definitions;
56
57
                    if (!in_array(strtolower($firstDefinition), ['int', 'float', 'bool', 'string', 'array', 'null', 'mixed'])) {
58
59
                        $typeHint = $firstDefinition;
60
                    }
61
                }
62
            }
63
        }
64
65
        return $typeHint;
66
    }
67
68
    /**
69
     * @param $className
70
     *
71
     * @return array
72
     */
73
    public function getImplemented($className)
74
    {
75
        return array_merge([$className], class_implements($className));
76
    }
77
}
78