Passed
Pull Request — master (#947)
by
unknown
20:18
created

Tokenizer::scopedEnumLocator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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