Passed
Push — master ( 8b6406...aa2a47 )
by Pol
02:18
created

YamlCommands::runTasks()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 51
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 7
eloc 27
c 4
b 0
f 0
nc 16
nop 0
dl 0
loc 51
ccs 0
cts 37
cp 0
crap 56
rs 8.5546

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Robo\Plugin\Commands;
6
7
use Consolidation\AnnotatedCommand\AnnotatedCommand;
8
use PhpTaskman\Core\Common\NullOutputAdapter;
0 ignored issues
show
Bug introduced by
The type PhpTaskman\Core\Common\NullOutputAdapter 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...
9
use PhpTaskman\Core\Robo\Task\PreconditionsCollectionFactoryTask;
0 ignored issues
show
Bug introduced by
The type PhpTaskman\Core\Robo\Tas...nsCollectionFactoryTask 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...
10
use PhpTaskman\CoreTasks\Plugin\Task\CollectionFactoryTask;
11
use Robo\Collection\CollectionBuilder;
12
use Robo\Contract\VerbosityThresholdInterface;
13
use Robo\Exception\TaskException;
14
use Symfony\Component\Console\Event\ConsoleCommandEvent;
15
16
/**
17
 * Class DynamicCommands.
18
 */
19
final class YamlCommands extends AbstractCommands
20
{
21
    /**
22
     * Bind input values of custom command options to config entries.
23
     *
24
     * @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
25
     *
26
     * @hook pre-command-event *
27
     */
28
    public function bindInputOptionsToConfig(ConsoleCommandEvent $event): void
29
    {
30
        $command = $event->getCommand();
31
32
        if (null === $command) {
33
            return;
34
        }
35
36
        if (AnnotatedCommand::class !== \get_class($command)) {
37
            return;
38
        }
39
40
        if (!($command instanceof AnnotatedCommand)) {
41
            return;
42
        }
43
44
        /** @var \Consolidation\AnnotatedCommand\AnnotatedCommand $command */
45
        /** @var \Consolidation\AnnotatedCommand\AnnotationData $annotatedData */
46
        $annotatedData = $command->getAnnotationData();
47
48
        if (!$annotatedData->get('dynamic-command')) {
49
            return;
50
        }
51
52
        // Dynamic commands may define their own options bound to specific configuration. Dynamically set the
53
        // configuration from command options.
54
        $config = $this->getConfig();
55
        $commands = $config->get('commands');
56
57
        if (empty($commands[$command->getName()]['options'])) {
58
            return;
59
        }
60
61
        foreach ($commands[$command->getName()]['options'] as $optionName => $option) {
62
            if (empty($option['config']) && $event->getInput()->hasOption($optionName)) {
63
                continue;
64
            }
65
66
            $inputValue = $event->getInput()->getOption($optionName);
67
68
            if (null === $inputValue) {
69
                continue;
70
            }
71
72
            $config->set(
73
                $option['config'],
74
                $event->getInput()->getOption($optionName)
75
            );
76
        }
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getConfigurationFile(): string
83
    {
84
        return __DIR__ . '/../../../../config/commands/default.yml';
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getDefaultConfigurationFile(): string
91
    {
92
        return __DIR__ . '/../../../../config/default.yml';
93
    }
94
95
    /**
96
     * Run a task.
97
     *
98
     * @dynamic-command true
99
     *
100
     * @throws \Robo\Exception\TaskException
101
     *
102
     * @return \Robo\Collection\CollectionBuilder
103
     */
104
    public function runTasks(): CollectionBuilder
105
    {
106
        $command = $this->input()->getArgument('command');
107
108
        if (!\is_string($command)) {
109
            throw new TaskException($this, 'The command must be a string.');
110
        }
111
112
        $inputOptions = [];
113
114
        foreach ($this->input()->getOptions() as $name => $value) {
115
            if ($this->input()->hasParameterOption('--' . $name)) {
116
                $inputOptions[$name] = $value;
117
            }
118
        }
119
120
        $command = $this->getConfig()->get('commands.' . $command);
121
122
        // Handle different types of command definitions.
123
        if (isset($command['tasks'])) {
124
            $arguments = [
125
                'tasks' => $command['tasks'],
126
                'options' => $inputOptions,
127
                'preconditions' => $command['preconditions'] ?? [],
128
            ];
129
130
            if (is_string($arguments['preconditions'])) {
131
                $arguments['preconditions'] = [$arguments['preconditions']];
132
            }
133
134
            /** @var CollectionFactoryTask $preconditionsTask */
135
            $preconditionsTask = $this->task(CollectionFactoryTask::class);
136
            $preconditionsTask->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG);
137
            $preconditionsTask->setTaskArguments([
138
                'tasks' => $arguments['preconditions'],
139
            ]);
140
141
            if (0 !== $preconditionsTask->run()->getExitCode()) {
142
                $arguments['tasks'] = [];
143
            }
144
        } else {
145
            $arguments = [
146
                'tasks' => $command,
147
                'options' => [],
148
            ];
149
        }
150
151
        /** @var CollectionFactoryTask $collectionFactory */
152
        $collectionFactory = $this->task(CollectionFactoryTask::class);
153
154
        return $collectionFactory->setTaskArguments($arguments);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $collectionFactor...skArguments($arguments) returns the type PhpTaskman\CoreTasks\Plu...k\CollectionFactoryTask which is incompatible with the type-hinted return Robo\Collection\CollectionBuilder.
Loading history...
155
    }
156
}
157