GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ShellTask::offsetSet()   D
last analyzed

Complexity

Conditions 17
Paths 20

Size

Total Lines 46
Code Lines 33

Duplication

Lines 14
Ratio 30.43 %

Importance

Changes 0
Metric Value
cc 17
eloc 33
nc 20
nop 2
dl 14
loc 46
rs 4.9679
c 0
b 0
f 0

How to fix   Complexity   

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
 * See class comment
4
 *
5
 * PHP Version 5
6
 *
7
 * @category   Netresearch
8
 * @package    Netresearch\Kite
9
 * @subpackage Task
10
 * @author     Christian Opitz <[email protected]>
11
 * @license    http://www.netresearch.de Netresearch Copyright
12
 * @link       http://www.netresearch.de
13
 */
14
15
namespace Netresearch\Kite\Task;
16
use Netresearch\Kite\Exception;
17
18
/**
19
 * Executes a command locally and returns the output
20
 *
21
 * @category   Netresearch
22
 * @package    Netresearch\Kite
23
 * @subpackage Task
24
 * @author     Christian Opitz <[email protected]>
25
 * @license    http://www.netresearch.de Netresearch Copyright
26
 * @link       http://www.netresearch.de
27
 */
28
class ShellTask extends \Netresearch\Kite\Task
29
{
30
    /**
31
     * Configure the options
32
     *
33
     * @return array
34
     */
35
    protected function configureVariables()
36
    {
37
        return array(
0 ignored issues
show
Best Practice introduced by
The expression return array('command' =...::configureVariables(); seems to be an array, but some of its elements' types (string) are incompatible with the return type of the parent method Netresearch\Kite\Task::configureVariables of type array<string,array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
38
            'command' => array(
39
                'type' => 'string|array',
40
                'label' => 'Command(s) to execute',
41
                'required' => true,
42
            ),
43
            'cwd' => array(
44
                'type' => 'string',
45
                'label' => 'The directory to change to before running the command'
46
            ),
47
            'argv' => array(
48
                'type' => 'array|string',
49
                'label' => 'String with all options and arguments for the command or an array in the same format as $argv. '
50
                    . 'Attention: Values won\'t be escaped!'
51
            ),
52
            'options' => array(
53
                'type' => 'array',
54
                'default' => array(),
55
                'label' => 'Array with options: Elements with numeric keys or bool true values will be --switches.'
56
            ),
57
            'arguments' => array(
58
                'type' => 'array',
59
                'default' => array(),
60
                'label' => 'Arguments to pass to the cmd'
61
            ),
62
            'optArg' => array(
63
                'type' => 'array|string',
64
                'label' => 'Arguments and options in one array. '
65
                    . 'When array, elements with numeric keys will be added as {@see arguments} and elements with string keys will be added as {@see options}. '
66
                    . 'When string, {@see argv} will be set to this value'
67
            ),
68
            'errorMessage' => array(
69
                'type' => 'string',
70
                'label' => 'Message to display when the command failed'
71
            ),
72
            'processSettings' => array(
73
                'type' => 'array',
74
                'default' => array(),
75
                'label' => 'Settings for symfony process class'
76
            ),
77
            '--',
78
        ) + parent::configureVariables();
79
    }
80
81
    /**
82
     * Handle arguments, options and optArg
83
     *
84
     * @param string $option Option name
85
     * @param mixed  $value  Option value
86
     *
87
     * @return void
88
     */
89
    public function offsetSet($option, $value)
90
    {
91
        if ($option === 'processSettings') {
92
            $value = array_merge($this->offsetGet('processSettings'), $value);
93
        }
94
        if (in_array($option, array('arguments', 'options', 'optArg'), true)) {
95
            if ($value === null) {
96
                if ($option === 'optArg' || $option === 'options') {
97
                    parent::offsetSet('options', array());
98
                }
99
                if ($option === 'optArg' || $option === 'arguments') {
100
                    parent::offsetSet('arguments', array());
101
                }
102
                return;
103
            }
104
            if ($option === 'optArg' && is_string($value)) {
105
                parent::offsetSet('argv', $value);
106
                return;
107
            }
108
            $arguments = $this->get('arguments');
109
            $options = $this->get('options');
110
            if ($option == 'arguments') {
111
                $arguments = array_merge($arguments, $value);
112
            } elseif ($option == 'options') {
113 View Code Duplication
                foreach ($value as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
                    if (is_numeric($k)) {
115
                        $options[$v] = true;
116
                    } else {
117
                        $options[$k] = $v;
118
                    }
119
                }
120
            } elseif ($option == 'optArg') {
121 View Code Duplication
                foreach ($value as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
                    if (is_numeric($k)) {
123
                        $arguments[] = $v;
124
                    } else {
125
                        $options[$k] = $v;
126
                    }
127
                }
128
            }
129
            parent::offsetSet('arguments', $arguments);
130
            parent::offsetSet('options', $options);
131
            return;
132
        }
133
        parent::offsetSet($option, $value);
134
    }
135
136
    /**
137
     * Execute the command
138
     *
139
     * @return mixed
140
     */
141
    protected function executeCommand()
142
    {
143
        $process = $this->console->createProcess($this->getCommand(), $this->get('cwd'));
144
        $process->setDryRun(!$this->shouldExecute());
145
        foreach ($this->get('processSettings') as $key => $value) {
146
            $process->{'set' . ucfirst($key)}($value);
147
        }
148
        return $process->run();
149
    }
150
151
    /**
152
     * Get the command with options and arguments
153
     *
154
     * @return string
155
     */
156
    protected function getCommand()
157
    {
158
        $cmd = $this->get('command');
159
        $argv = $this->get('argv');
160
        $options = $this->get('options');
161
        $arguments = $this->get('arguments');
162
        if (is_array($cmd)) {
163
            if ($argv || $options || $arguments) {
164
                throw new Exception('Can not use argv, options or arguments on multiple commands');
165
            }
166
            $cmd = $this->expand(implode('; ', $cmd));
167
        } else {
168
            if ($argv) {
169
                if ($options || $arguments) {
170
                    throw new Exception('Can not combine argv with options or arguments');
171
                }
172
                $cmd .= ' ' . (is_array($argv) ? implode(' ', $argv) : $argv);
173
            } else {
174
                $cmd .= $this->renderOptions($options);
175
                $cmd .= $this->renderArguments($arguments);
176
            }
177
        }
178
179
        return $cmd;
180
    }
181
182
    /**
183
     * Render options as options for the command
184
     *
185
     * @param array $options The options
186
     *
187
     * @return string
188
     */
189
    protected function renderOptions(array $options)
190
    {
191
        $optionString = '';
192
        foreach ($options as $option => $value) {
193
            $value = $this->expand($value);
194
            if ($value === false) {
195
                continue;
196
            }
197
            $l = strlen($option);
198
            $optionString .= ' ';
199
            if ($option[0] !== '-') {
200
                $optionString .= ($l === 1) ? '-' : '--';
201
            }
202
            $optionString .= $option;
203
            if ($value !== true) {
204
                if ($l > 1 && ($option[0] !== '-' || $option[1] === '-')) {
205
                    $optionString .= '=';
206
                } else {
207
                    $optionString .= ' ';
208
                }
209
                $optionString .= escapeshellarg($value);
210
            }
211
        }
212
        return $optionString;
213
    }
214
215
    /**
216
     * Render arguments for the command
217
     *
218
     * @param array $arguments The arguments
219
     *
220
     * @return string
221
     */
222
    protected function renderArguments(array $arguments)
223
    {
224
        $argumentString = '';
225
        foreach ($arguments as $argument) {
226
            $value = $this->expand($argument);
227
            $argumentString .= ' ' . escapeshellarg($value);
228
        }
229
        return $argumentString;
230
    }
231
232
    /**
233
     * Run the command
234
     *
235
     * @return string
236
     */
237
    public function run()
238
    {
239
        $this->preview();
240
        $res = $this->execute();
241
        return $res;
242
    }
243
244
    /**
245
     * Execute the command
246
     *
247
     * @return string
248
     */
249
    public function execute()
250
    {
251
        return $this->executeCommand();
252
    }
253
}
254
?>
255