Completed
Branch feature/split-orm (60a911)
by Anton
03:15
created

InvocationsLocator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInvocations() 0 11 3
B availableInvocations() 0 20 5
B isTargeted() 0 26 5
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\Exceptions\LocatorException;
11
use Spiral\Tokenizer\Prototypes\AbstractLocator;
12
use Spiral\Tokenizer\Reflections\ReflectionInvocation;
13
14
/**
15
 * Can locate invocations in a specified directory. Can only find simple invocations!
16
 *
17
 * Potentially this class have to be rewritten in order to use new PHP API and AST tree, for now it
18
 * still relies on legacy token based parser.
19
 */
20
class InvocationsLocator extends AbstractLocator implements InvocationInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getInvocations(\ReflectionFunctionAbstract $function): array
26
    {
27
        $result = [];
28
        foreach ($this->availableInvocations($function->getName()) as $invocation) {
29
            if ($this->isTargeted($invocation, $function)) {
30
                $result[] = $invocation;
31
            }
32
        }
33
34
        return $result;
35
    }
36
37
    /**
38
     * Invocations available in finder scope.
39
     *
40
     * @param string $signature Method or function signature (name), for pre-filtering.
41
     *
42
     * @return ReflectionInvocation[]
43
     */
44
    protected function availableInvocations(string $signature = ''): array
45
    {
46
        $invocations = [];
47
48
        $signature = strtolower(trim($signature, '\\'));
49
        foreach ($this->availableReflections() as $reflection) {
50
            foreach ($reflection->getInvocations() as $invocation) {
51
                if (
52
                    !empty($signature)
53
                    && strtolower(trim($invocation->getName(), '\\')) != $signature
54
                ) {
55
                    continue;
56
                }
57
58
                $invocations[] = $invocation;
59
            }
60
        }
61
62
        return $invocations;
63
    }
64
65
    /**
66
     * @param ReflectionInvocation        $invocation
67
     * @param \ReflectionFunctionAbstract $function
68
     *
69
     * @return bool
70
     */
71
    protected function isTargeted(
72
        ReflectionInvocation $invocation,
73
        \ReflectionFunctionAbstract $function
74
    ): bool {
75
        if ($function instanceof \ReflectionFunction) {
76
            return !$invocation->isMethod();
77
        }
78
79
        try {
80
            $reflection = $this->classReflection($invocation->getClass());
81
        } catch (LocatorException $e) {
82
            return false;
83
        }
84
85
        /**
86
         * @var \ReflectionMethod $function
87
         */
88
        $target = $function->getDeclaringClass();
89
90
        if ($target->isTrait()) {
91
            //Let's compare traits
92
            return in_array($target->getName(), $this->fetchTraits($invocation->getClass()));
93
        }
94
95
        return $reflection->getName() == $target->getName() || $reflection->isSubclassOf($target);
96
    }
97
}
98