Completed
Branch dbal-improvement (e43d29)
by Anton
06:02
created

InvocationLocator::availableInvocations()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 11
nc 4
nop 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Tokenizer;
9
10
use Spiral\Tokenizer\Prototypes\AbstractLocator;
11
use Spiral\Tokenizer\Reflections\ReflectionInvocation;
12
13
/**
14
 * Can locate invocations in a specified directory. Can only find simple invocations!
15
 */
16
class InvocationLocator extends AbstractLocator implements InvocationLocatorInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function getInvocations(\ReflectionFunctionAbstract $function)
22
    {
23
        $result = [];
24
        foreach ($this->availableInvocations($function->getName()) as $invocation) {
25
            if ($this->isTargeted($invocation, $function)) {
26
                $result[] = $invocation;
27
            }
28
        }
29
30
        return $result;
31
    }
32
33
    /**
34
     * Classes available in finder scope.
35
     *
36
     * @param string $signature Method or function signature (name), for pre-filtering.
37
     * @return ReflectionInvocation[]
38
     */
39
    protected function availableInvocations($signature = '')
40
    {
41
        $invocations = [];
42
43
        $signature = strtolower(trim($signature, '\\'));
44
        foreach ($this->availableReflections() as $reflection) {
45
            foreach ($reflection->getInvocations() as $invocation) {
46
                if (
47
                    !empty($signature)
48
                    && strtolower(trim($invocation->getName(), '\\')) != $signature
49
                ) {
50
                    continue;
51
                }
52
53
                $invocations[] = $invocation;
54
            }
55
        }
56
57
        return $invocations;
58
    }
59
60
    /**
61
     *
62
     *
63
     * @param ReflectionInvocation        $invocation
64
     * @param \ReflectionFunctionAbstract $function
65
     * @return bool
66
     */
67
    protected function isTargeted(
68
        ReflectionInvocation $invocation,
69
        \ReflectionFunctionAbstract $function
70
    ) {
71
        if ($function instanceof \ReflectionFunction) {
72
            return !$invocation->isMethod();
73
        }
74
75
        if (empty($class = $this->classReflection($invocation->getClass()))) {
76
            //Unable to get reflection
77
            return false;
78
        }
79
80
        /**
81
         * @var \ReflectionMethod $function
82
         */
83
        $target = $function->getDeclaringClass();
84
85
        if ($target->isTrait()) {
86
            //Let's compare traits
87
            return in_array($target->getName(), $this->getTraits($invocation->getClass()));
88
        }
89
90
        return $class->getName() == $target->getName() || $class->isSubclassOf($target);
91
    }
92
}
93