Passed
Pull Request — master (#1040)
by Maxim
11:00
created

ValidateCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A perform() 0 29 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Command\Tokenizer;
6
7
use Spiral\Attributes\ReaderInterface;
8
use Spiral\Console\Command;
9
use Spiral\Tokenizer\Attribute\AbstractTarget;
10
use Spiral\Tokenizer\ClassesInterface;
11
use Spiral\Tokenizer\TokenizationListenerInterface;
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 2
    public function perform(ClassesInterface $classes, ReaderInterface $reader): int
19
    {
20 2
        $invalid = [];
21 2
        foreach ($classes->getClasses() as $class) {
22 1
            if (!$class->implementsInterface(TokenizationListenerInterface::class)) {
23 1
                continue;
24
            }
25 1
            $attribute = $reader->firstClassMetadata($class, AbstractTarget::class);
26 1
            if ($attribute === null) {
27 1
                $invalid[$class->getName()] = 'Add <comment>#[TargetClass]</comment> or ' .
28 1
                    '<comment>#[TargetAttribute]</comment> attribute to the listener';
29
            }
30
        }
31
32 2
        if ($invalid === []) {
33 1
            $this->info('All listeners are correctly configured.');
34
35 1
            return self::SUCCESS;
36
        }
37
38 1
        $grid = $this->table(['Listener', 'Suggestion']);
39
40 1
        foreach ($invalid as $listener => $message) {
41 1
            $grid->addRow([$listener, $message]);
42
        }
43
44 1
        $grid->render();
45
46 1
        return self::SUCCESS;
47
    }
48
}
49