InfoCommand::perform()   D
last analyzed

Complexity

Conditions 14
Paths 288

Size

Total Lines 81
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 47
CRAP Score 14.4266

Importance

Changes 0
Metric Value
eloc 49
c 0
b 0
f 0
dl 0
loc 81
ccs 47
cts 54
cp 0.8704
rs 4.3333
cc 14
nc 288
nop 3
crap 14.4266

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Command\Tokenizer;
6
7
use Spiral\Boot\DirectoriesInterface;
8
use Spiral\Console\Command;
9
use Spiral\Tokenizer\Config\TokenizerConfig;
10
use Spiral\Tokenizer\TokenizerListenerRegistryInterface;
11
12
final class InfoCommand extends Command
13
{
14
    protected const NAME = 'tokenizer:info';
15
    protected const DESCRIPTION = 'Get information about tokenizer directories to scan';
16
17 1
    public function perform(
18
        TokenizerConfig $config,
19
        DirectoriesInterface $dirs,
20
        TokenizerListenerRegistryInterface $listenerRegistry,
21
    ): int {
22 1
        $this->info('Included directories:');
23 1
        $grid = $this->table(['Directory', 'Scope']);
24 1
        foreach ($config->getDirectories() as $directory) {
25 1
            $grid->addRow([\str_replace($dirs->get('root'), '', $directory), '']);
26
        }
27 1
        foreach ($config->getScopes() as $scope => $data) {
28
            foreach ($data['directories'] ?? [] as $directory) {
29
                $grid->addRow([\str_replace($dirs->get('root'), '', $directory), $scope]);
30
            }
31
        }
32
33 1
        $grid->render();
34
35 1
        $this->newLine();
36
37 1
        $this->info('Excluded directories:');
38 1
        $grid = $this->table(['Directory', 'Scope']);
39 1
        foreach ($config->getExcludes() as $directory) {
40 1
            $grid->addRow([\str_replace($dirs->get('root'), '', $directory), '']);
41
        }
42 1
        foreach ($config->getScopes() as $scope => $data) {
43
            foreach ($data['exclude'] ?? [] as $directory) {
44
                $grid->addRow([\str_replace($dirs->get('root'), '', $directory), $scope]);
45
            }
46
        }
47
48 1
        $grid->render();
49
50 1
        $this->newLine();
51
52 1
        $this->info('Loaders:');
53 1
        $grid = $this->table(['Loader', 'Status']);
54
55 1
        $grid->addRow(['Classes', $config->isLoadClassesEnabled()
56 1
            ? '<info>enabled</>'
57 1
            : '<error>disabled</>. <comment>To enable, add "TOKENIZER_LOAD_CLASSES=true" to your .env file.</>']);
58 1
        $grid->addRow(['Enums', $config->isLoadEnumsEnabled()
59
            ? '<info>enabled</>'
60 1
            : '<error>disabled</>. <comment>To enable, add "TOKENIZER_LOAD_ENUMS=true" to your .env file.</>']);
61 1
        $grid->addRow(
62 1
            ['Interfaces', $config->isLoadInterfacesEnabled()
63
                ? '<info>enabled</>'
64 1
                : '<error>disabled</>. <comment>To enable, add "TOKENIZER_LOAD_INTERFACES=true" to your .env file.</>'],
65 1
        );
66
67 1
        $grid->render();
68
69 1
        $listeners = \method_exists($listenerRegistry, 'getListenerClasses')
70 1
            ? $listenerRegistry->getListenerClasses()
71
            : [];
72
73 1
        $this->newLine();
74
75 1
        $this->info('Listeners:');
76 1
        $grid = $this->table(['Registered Listener', 'File']);
77 1
        foreach ($listeners as $listener) {
78 1
            $grid->addRow([
79 1
                $listener,
80 1
                \str_replace([$dirs->get('root'), '\\'], ['', '/'], (new \ReflectionClass($listener))->getFileName()),
81 1
            ]);
82
        }
83
84 1
        $grid->render();
85
86 1
        $this->newLine();
87
88 1
        $this->newLine();
89 1
        $this->info(
90 1
            \sprintf('Tokenizer cache: %s', $config->isCacheEnabled() ? '<info>enabled</>' : '<error>disabled</>'),
91 1
        );
92 1
        if (!$config->isCacheEnabled()) {
93 1
            $this->comment('To enable cache, add "TOKENIZER_CACHE_TARGETS=true" to your .env file.');
94 1
            $this->comment('Read more at https://spiral.dev/docs/advanced-tokenizer/#class-listeners');
95
        }
96
97 1
        return self::SUCCESS;
98
    }
99
}
100