Passed
Push — master ( ed42e4...b3b06a )
by Kirill
04:44
created

CallableSequence::execute()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 26
rs 8.8333
cc 7
nc 8
nop 2
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Console\Sequence;
13
14
use Psr\Container\ContainerInterface;
15
use Spiral\Core\ResolverInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Invokes service method as part of the sequence.
20
 */
21
final class CallableSequence extends AbstractSequence
22
{
23
    /** @var string */
24
    private $function;
25
26
    /** @var array */
27
    private $parameters = [];
28
29
    /**
30
     * @param callable $function
31
     * @param array    $parameters
32
     * @param string   $header
33
     * @param string   $footer
34
     */
35
    public function __construct(
36
        $function,
37
        array $parameters = [],
38
        string $header = '',
39
        string $footer = ''
40
    ) {
41
        $this->function = $function;
0 ignored issues
show
Documentation Bug introduced by
It seems like $function of type callable is incompatible with the declared type string of property $function.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->parameters = $parameters;
43
44
        parent::__construct($header, $footer);
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function execute(ContainerInterface $container, OutputInterface $output): void
51
    {
52
        $function = $this->function;
53
        if (is_string($function) && strpos($function, ':') !== false) {
54
            $function = explode(':', str_replace('::', ':', $function));
55
        }
56
57
        if (is_array($function) && isset($function[0]) && !is_object($function[0])) {
58
            $function[0] = $container->get($function[0]);
59
        }
60
61
        /** @var ResolverInterface $resolver */
62
        $resolver = $container->get(ResolverInterface::class);
63
64
        if (is_array($function)) {
65
            $reflection = new \ReflectionMethod($function[0], $function[1]);
66
            $reflection->invokeArgs($function[0], $resolver->resolveArguments($reflection, [
67
                'output' => $output
68
            ]));
69
70
            return;
71
        }
72
73
        $reflection = new \ReflectionFunction($function);
74
        $reflection->invokeArgs($resolver->resolveArguments($reflection, [
75
            'output' => $output
76
        ]));
77
    }
78
}
79