Test Failed
Pull Request — master (#947)
by
unknown
09:21 queued 01:29
created

Tokenizer::interfaceLocator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 5
rs 10
ccs 3
cts 3
cp 1
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 382
    public function __construct(
24
        private readonly TokenizerConfig $config
25
    ) {
26 382
    }
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 368
    public function scopedEnumLocator(string $scope): EnumsInterface
42
    {
43
        $dirs = $this->config->getScope($scope);
44
45 368
        return $this->enumLocator($dirs['directories'], $dirs['exclude']);
46
    }
47
48
    /**
49
     * Get pre-configured interface locator for specific scope.
50
     */
51 27
    public function scopedInterfaceLocator(string $scope): InterfacesInterface
52
    {
53
        $dirs = $this->config->getScope($scope);
54
55 27
        return $this->interfaceLocator($dirs['directories'], $dirs['exclude']);
56
    }
57
58
    /**
59
     * Get pre-configured class locator.
60
     */
61 386
    public function classLocator(
62
        array $directories = [],
63 386
        array $exclude = []
64
    ): ClassLocator {
65 386
        return new ClassLocator($this->makeFinder($directories, $exclude), $this->config->isDebug());
66 386
    }
67 386
68 386
    /**
69
     * Get pre-configured invocation locator.
70
     */
71 386
    public function invocationLocator(
72 386
        array $directories = [],
73
        array $exclude = []
74
    ): InvocationLocator {
75 386
        return new InvocationLocator($this->makeFinder($directories, $exclude), $this->config->isDebug());
76
    }
77
78 386
    public function enumLocator(
79
        array $directories = [],
80
        array $exclude = []
81
    ): EnumLocator {
82
        return new EnumLocator($this->makeFinder($directories, $exclude), $this->config->isDebug());
83
    }
84
85 382
    public function interfaceLocator(
86
        array $directories = [],
87 382
        array $exclude = []
88
    ): InterfaceLocator {
89 382
        return new InterfaceLocator($this->makeFinder($directories, $exclude), $this->config->isDebug());
90 377
    }
91
92
    /**
93 382
     * Get all tokes for specific file.
94 381
     */
95
    public static function getTokens(string $filename): array
96
    {
97 382
        $tokens = \token_get_all(\file_get_contents($filename));
98
99
        $line = 0;
100
        foreach ($tokens as &$token) {
101
            if (isset($token[self::LINE])) {
102
                $line = $token[self::LINE];
103
            }
104
105
            if (!\is_array($token)) {
106
                $token = [$token, $token, $line];
107
            }
108
109
            unset($token);
110
        }
111
112
        return $tokens;
113
    }
114
115
    /**
116
     * @param array $directories Overwrites default config values.
117
     * @param array $exclude     Overwrites default config values.
118
     */
119
    private function makeFinder(array $directories = [], array $exclude = []): Finder
120
    {
121
        $finder = new Finder();
122
123
        if (empty($directories)) {
124
            $directories = $this->config->getDirectories();
125
        }
126
127
        if (empty($exclude)) {
128
            $exclude = $this->config->getExcludes();
129
        }
130
131
        return $finder->files()->in($directories)->exclude($exclude)->name('*.php');
132
    }
133
}
134