Passed
Pull Request — master (#1040)
by Maxim
14:41 queued 04:55
created

ValidateCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 17
c 2
b 0
f 0
dl 0
loc 30
ccs 17
cts 17
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A perform() 0 25 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Command\Tokenizer;
6
7
use Spiral\Attributes\ReaderInterface;
8
use Spiral\Boot\DirectoriesInterface;
9
use Spiral\Console\Command;
10
use Spiral\Tokenizer\Attribute\AbstractTarget;
11
use Spiral\Tokenizer\TokenizerListenerRegistryInterface;
12
13
final class ValidateCommand extends Command
14
{
15
    protected const NAME = 'tokenizer:validate';
16
    protected const DESCRIPTION = 'Checks all listeners in the application to ensure they are correctly configured';
17
18 1
    public function perform(
19
        TokenizerListenerRegistryInterface $registry,
20
        DirectoriesInterface $dirs,
21
        ReaderInterface $reader
22
    ): int {
23 1
        $listeners = \method_exists($registry, 'getListenerClasses') ? $registry->getListenerClasses() : [];
24
25 1
        $grid = $this->table(['Listener', 'Suggestion']);
26 1
        foreach ($listeners as $class) {
27 1
            $ref = new \ReflectionClass($class);
28 1
            $attribute = $reader->firstClassMetadata($ref, AbstractTarget::class);
29 1
            $suggestion = match (true) {
30 1
                $attribute === null => 'Add <comment>#[TargetClass]</comment> or ' .
31 1
                    '<comment>#[TargetAttribute]</comment> attribute to the listener',
32 1
                default => '<info>Listener is configured correctly</info>',
33 1
            };
34 1
            $grid->addRow([
35 1
                $class . "\n" . \sprintf('<fg=blue>%s</>', \str_replace($dirs->get('root'), '', $ref->getFileName())),
36 1
                $suggestion,
37 1
            ]);
38
        }
39
40 1
        $grid->render();
41
42 1
        return self::SUCCESS;
43
    }
44
}
45