Test Failed
Push — master ( 0ec5a1...d8a21b )
by butschster
05:56 queued 18s
created

ValidateCommand::perform()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 25
rs 9.7998
cc 3
nc 4
nop 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
    public function perform(
19
        TokenizerListenerRegistryInterface $registry,
20
        DirectoriesInterface $dirs,
21
        ReaderInterface $reader
22
    ): int {
23
        $listeners = \method_exists($registry, 'getListenerClasses') ? $registry->getListenerClasses() : [];
24
25
        $grid = $this->table(['Listener', 'Suggestion']);
26
        foreach ($listeners as $class) {
27
            $ref = new \ReflectionClass($class);
28
            $attribute = $reader->firstClassMetadata($ref, AbstractTarget::class);
29
            $suggestion = match (true) {
30
                $attribute === null => 'Add <comment>#[TargetClass]</comment> or ' .
31
                    '<comment>#[TargetAttribute]</comment> attribute to the listener',
32
                default => '<fg=green> ✓ </>',
33
            };
34
            $grid->addRow([
35
                $class . "\n" . \sprintf('<fg=gray>%s</>', \str_replace($dirs->get('root'), '', $ref->getFileName())),
36
                $suggestion,
37
            ]);
38
        }
39
40
        $grid->render();
41
42
        return self::SUCCESS;
43
    }
44
}
45