Passed
Push — master ( 813f0f...a309d2 )
by butschster
09:30 queued 12s
created

Tokenizer::interfaceLocator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tokenizer;
6
7
use Spiral\Core\Container\SingletonInterface;
8
use Spiral\Tokenizer\Config\TokenizerConfig;
9
use Symfony\Component\Finder\Finder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
/**
12
 * Manages automatic container injections of class and invocation locators.
13
 */
14
final class Tokenizer implements SingletonInterface
15
{
16
    /**
17
     * Token array constants.
18
     */
19
    public const TYPE = 0;
20
    public const CODE = 1;
21
    public const LINE = 2;
22
23 396
    public function __construct(
24
        private readonly TokenizerConfig $config
25
    ) {
26 396
    }
27
28
    /**
29
     * Get pre-configured class locator for specific scope.
30
     */
31 25
    public function scopedClassLocator(string $scope): ClassesInterface
32
    {
33 25
        $dirs = $this->config->getScope($scope);
34
35 25
        return $this->classLocator($dirs['directories'], $dirs['exclude']);
36
    }
37
38
    /**
39
     * Get pre-configured enum locator for specific scope.
40
     */
41 2
    public function scopedEnumLocator(string $scope): EnumsInterface
42
    {
43 2
        $dirs = $this->config->getScope($scope);
44
45 2
        return $this->enumLocator($dirs['directories'], $dirs['exclude']);
46
    }
47
48
    /**
49
     * Get pre-configured interface locator for specific scope.
50
     */
51 2
    public function scopedInterfaceLocator(string $scope): InterfacesInterface
52
    {
53 2
        $dirs = $this->config->getScope($scope);
54
55 2
        return $this->interfaceLocator($dirs['directories'], $dirs['exclude']);
56
    }
57
58
    /**
59
     * Get pre-configured class locator.
60
     */
61 368
    public function classLocator(
62
        array $directories = [],
63
        array $exclude = []
64
    ): ClassLocator {
65 368
        return new ClassLocator($this->makeFinder($directories, $exclude), $this->config->isDebug());
66
    }
67
68
    /**
69
     * Get pre-configured invocation locator.
70
     */
71 27
    public function invocationLocator(
72
        array $directories = [],
73
        array $exclude = []
74
    ): InvocationLocator {
75 27
        return new InvocationLocator($this->makeFinder($directories, $exclude), $this->config->isDebug());
76
    }
77
78 8
    public function enumLocator(
79
        array $directories = [],
80
        array $exclude = []
81
    ): EnumLocator {
82 8
        return new EnumLocator($this->makeFinder($directories, $exclude), $this->config->isDebug());
83
    }
84
85 6
    public function interfaceLocator(
86
        array $directories = [],
87
        array $exclude = []
88
    ): InterfaceLocator {
89 6
        return new InterfaceLocator($this->makeFinder($directories, $exclude), $this->config->isDebug());
90
    }
91
92
    /**
93
     * Get all tokes for specific file.
94
     */
95 399
    public static function getTokens(string $filename): array
96
    {
97 399
        $tokens = \token_get_all(\file_get_contents($filename));
98
99 399
        $line = 0;
100 399
        foreach ($tokens as &$token) {
101 399
            if (isset($token[self::LINE])) {
102 399
                $line = $token[self::LINE];
103
            }
104
105 399
            if (!\is_array($token)) {
106 399
                $token = [$token, $token, $line];
107
            }
108
109 399
            unset($token);
110
        }
111
112 399
        return $tokens;
113
    }
114
115
    /**
116
     * @param array $directories Overwrites default config values.
117
     * @param array $exclude     Overwrites default config values.
118
     */
119 396
    private function makeFinder(array $directories = [], array $exclude = []): Finder
120
    {
121 396
        $finder = new Finder();
122
123 396
        if (empty($directories)) {
124 387
            $directories = $this->config->getDirectories();
125
        }
126
127 396
        if (empty($exclude)) {
128 393
            $exclude = $this->config->getExcludes();
129
        }
130
131 396
        return $finder->files()->in($directories)->exclude($exclude)->name('*.php');
132
    }
133
}
134