Completed
Push — master ( 8b8afe...5f2bbe )
by Greg
02:21
created

src/GlobalOptionsEventListener.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo;
3
4
use Symfony\Component\Console\ConsoleEvents;
5
use Symfony\Component\Console\Event\ConsoleCommandEvent;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Robo\Contract\ConfigAwareInterface;
8
use Robo\Common\ConfigAwareTrait;
9
use Robo\Config\GlobalOptionDefaultValuesInterface;
10
11
class GlobalOptionsEventListener implements EventSubscriberInterface, ConfigAwareInterface
12
{
13
    use ConfigAwareTrait;
14
15
    /** @var Application */
16
    protected $application;
17
18
    /** @var string */
19
    protected $prefix;
20
21
    /**
22
     * GlobalOptionsEventListener listener
23
     */
24
    public function __construct()
25
    {
26
        $this->prefix = 'options';
27
    }
28
29
    /**
30
     * Add a reference to the Symfony Console application object.
31
     */
32
    public function setApplication($application)
33
    {
34
        $this->application = $application;
35
        return $this;
36
    }
37
38
    /**
39
     * Stipulate the prefix to use for option injection.
40
     * @param string $prefix
41
     */
42
    public function setGlobalOptionsPrefix($prefix)
43
    {
44
        $this->prefix = $prefix;
45
        return $this;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public static function getSubscribedEvents()
52
    {
53
        return [ConsoleEvents::COMMAND => 'handleCommandEvent'];
54
    }
55
56
    /**
57
     * Run all of our individual operations when a command event is received.
58
     */
59
    public function handleCommandEvent(ConsoleCommandEvent $event)
60
    {
61
        $this->setGlobalOptions($event);
62
        $this->setConfigurationValues($event);
63
    }
64
65
    /**
66
     * Before a Console command runs, examine the global
67
     * commandline options from the event Input, and set
68
     * configuration values as appropriate.
69
     *
70
     * @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
71
     */
72
    public function setGlobalOptions(ConsoleCommandEvent $event)
73
    {
74
        $config = $this->getConfig();
75
        $input = $event->getInput();
76
77
        $globalOptions = $config->get($this->prefix, []);
0 ignored issues
show
array() is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        if ($config instanceof \Consolidation\Config\GlobalOptionDefaultValuesInterface) {
79
            $globalOptions += $config->getGlobalOptionDefaultValues();
80
        }
81
82
        $globalOptions += $this->applicationOptionDefaultValues();
83
84
        // Set any config value that has a defined global option (e.g. --simulate)
85
        foreach ($globalOptions as $option => $default) {
86
            $value = $input->hasOption($option) ? $input->getOption($option) : null;
87
            // Unfortunately, the `?:` operator does not differentate between `0` and `null`
88
            if (!isset($value)) {
89
                $value = $default;
90
            }
91
            $config->set($this->prefix . '.' . $option, $value);
92
        }
93
    }
94
95
    /**
96
     * Examine the commandline --define / -D options, and apply the provided
97
     * values to the active configuration.
98
     *
99
     * @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
100
     */
101
    public function setConfigurationValues(ConsoleCommandEvent $event)
102
    {
103
        $config = $this->getConfig();
104
        $input = $event->getInput();
105
106
        // Also set any `-D config.key=value` options from the commandline.
107
        if ($input->hasOption('define')) {
108
            $configDefinitions = $input->getOption('define');
109
            foreach ($configDefinitions as $value) {
0 ignored issues
show
The expression $configDefinitions of type string|array<integer,string>|boolean|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
110
                list($key, $value) = $this->splitConfigKeyValue($value);
111
                $config->set($key, $value);
112
            }
113
        }
114
    }
115
116
    /**
117
     * Split up the key=value config setting into its component parts. If
118
     * the input string contains no '=' character, then the value will be 'true'.
119
     *
120
     * @param string $value
121
     * @return array
122
     */
123
    protected function splitConfigKeyValue($value)
124
    {
125
        $parts = explode('=', $value, 2);
126
        $parts[] = true;
127
        return $parts;
128
    }
129
130
    /**
131
     * Get default option values from the Symfony Console application, if
132
     * it is available.
133
     */
134
    protected function applicationOptionDefaultValues()
135
    {
136
        if (!$this->application) {
137
            return [];
138
        }
139
140
        $result = [];
141
        foreach ($this->application->getDefinition()->getOptions() as $key => $option) {
142
            $result[$key] = $option->acceptValue() ? $option->getDefault() : null;
143
        }
144
        return $result;
145
    }
146
}
147