Passed
Push — master ( b292e6...c5f4fa )
by butschster
07:03
created

LazyTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 6
eloc 19
dl 0
loc 43
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsLazyLoading() 0 4 2
A createLazyCommand() 0 21 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Console\Traits;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Spiral\Console\Command as SpiralCommand;
10
use Spiral\Console\Config\ConsoleConfig;
11
use Spiral\Events\EventDispatcherAwareInterface;
12
use Symfony\Component\Console\Command\Command as SymfonyCommand;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command 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...
13
use Symfony\Component\Console\Command\LazyCommand;
14
15
trait LazyTrait
16
{
17
    private ContainerInterface $container;
18
    private array $interceptors = [];
19
    private ?EventDispatcherInterface $dispatcher = null;
20
21
    /**
22
     * Check if command can be lazy-loaded.
23
     *
24
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
25
     */
26 70
    private function supportsLazyLoading(string $class): bool
27
    {
28 70
        return \is_subclass_of($class, SpiralCommand::class)
29 70
            && \defined(\sprintf('%s::%s', $class, 'NAME'));
30
    }
31
32
    /**
33
     * Wrap given command into LazyCommand which will be executed only on run.
34
     *
35
     * @param class-string<SpiralCommand> $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<SpiralCommand> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<SpiralCommand>.
Loading history...
36
     */
37 55
    private function createLazyCommand(string $class): LazyCommand
38
    {
39 55
        return new LazyCommand(
40
            $class::NAME,
41
            [],
42 55
            \defined(\sprintf('%s::%s', $class, 'DESCRIPTION'))
43 55
                ? $class::DESCRIPTION
44 55
                : '',
45
            false,
46 55
            function () use ($class): SymfonyCommand {
47
                /** @var SpiralCommand $command */
48 38
                $command = $this->container->get($class);
49
50 38
                $command->setContainer($this->container);
51 38
                $command->setInterceptors($this->interceptors);
52
53 38
                if ($this->dispatcher !== null && $command instanceof EventDispatcherAwareInterface) {
54
                    $command->setEventDispatcher($this->dispatcher);
55
                }
56
57 38
                return $command;
58
            }
59
        );
60
    }
61
}
62