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.
Completed
Push — master ( 2ab419...940f7c )
by Robert
32:50
created

HelpController::getScriptName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\console\controllers;
9
10
use Yii;
11
use yii\base\Application;
12
use yii\console\Controller;
13
use yii\console\Exception;
14
use yii\helpers\Console;
15
use yii\helpers\Inflector;
16
17
/**
18
 * Provides help information about console commands.
19
 *
20
 * This command displays the available command list in
21
 * the application or the detailed instructions about using
22
 * a specific command.
23
 *
24
 * This command can be used as follows on command line:
25
 *
26
 * ```
27
 * yii help [command name]
28
 * ```
29
 *
30
 * In the above, if the command name is not provided, all
31
 * available commands will be displayed.
32
 *
33
 * @property array $commands All available command names. This property is read-only.
34
 *
35
 * @author Qiang Xue <[email protected]>
36
 * @since 2.0
37
 */
38
class HelpController extends Controller
39
{
40
    /**
41
     * Displays available commands or the detailed information
42
     * about a particular command.
43
     *
44
     * @param string $command The name of the command to show help about.
45
     * If not provided, all available commands will be displayed.
46
     * @return integer the exit status
47
     * @throws Exception if the command for help is unknown
48
     */
49 3
    public function actionIndex($command = null)
50
    {
51 3
        if ($command !== null) {
52 2
            $result = Yii::$app->createController($command);
53 2
            if ($result === false) {
54
                $name = $this->ansiFormat($command, Console::FG_YELLOW);
55
                throw new Exception("No help for unknown command \"$name\".");
56
            }
57
58 2
            list($controller, $actionID) = $result;
59
60 2
            $actions = $this->getActions($controller);
61 2
            if ($actionID !== '' || count($actions) === 1 && $actions[0] === $controller->defaultAction) {
62 2
                $this->getSubCommandHelp($controller, $actionID);
63 2
            } else {
64
                $this->getCommandHelp($controller);
65
            }
66 2
        } else {
67 1
            $this->getDefaultHelp();
68
        }
69 3
    }
70
71
    /**
72
     * Returns all available command names.
73
     * @return array all available command names
74
     */
75 1
    public function getCommands()
76
    {
77 1
        $commands = $this->getModuleCommands(Yii::$app);
78 1
        sort($commands);
79 1
        return array_unique($commands);
80
    }
81
82
    /**
83
     * Returns an array of commands an their descriptions.
84
     * @return array all available commands as keys and their description as values.
85
     */
86 1
    protected function getCommandDescriptions()
87
    {
88 1
        $descriptions = [];
89 1
        foreach ($this->getCommands() as $command) {
90 1
            $description = '';
91
92 1
            $result = Yii::$app->createController($command);
93 1
            if ($result !== false && $result[0] instanceof Controller) {
94 1
                list($controller, $actionID) = $result;
0 ignored issues
show
Unused Code introduced by
The assignment to $actionID is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
95
                /** @var Controller $controller */
96 1
                $description = $controller->getHelpSummary();
97 1
            }
98
99 1
            $descriptions[$command] = $description;
100 1
        }
101
102 1
        return $descriptions;
103
    }
104
105
    /**
106
     * Returns all available actions of the specified controller.
107
     * @param Controller $controller the controller instance
108
     * @return array all available action IDs.
109
     */
110 3
    public function getActions($controller)
111
    {
112 3
        $actions = array_keys($controller->actions());
113 3
        $class = new \ReflectionClass($controller);
114 3
        foreach ($class->getMethods() as $method) {
115 3
            $name = $method->getName();
116 3
            if ($name !== 'actions' && $method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0) {
117 3
                $actions[] = Inflector::camel2id(substr($name, 6), '-', true);
118 3
            }
119 3
        }
120 3
        sort($actions);
121
122 3
        return array_unique($actions);
123
    }
124
125
    /**
126
     * Returns available commands of a specified module.
127
     * @param \yii\base\Module $module the module instance
128
     * @return array the available command names
129
     */
130 1
    protected function getModuleCommands($module)
131
    {
132 1
        $prefix = $module instanceof Application ? '' : $module->getUniqueId() . '/';
133
134 1
        $commands = [];
135 1
        foreach (array_keys($module->controllerMap) as $id) {
136 1
            $commands[] = $prefix . $id;
137 1
        }
138
139 1
        foreach ($module->getModules() as $id => $child) {
140
            if (($child = $module->getModule($id)) === null) {
141
                continue;
142
            }
143
            foreach ($this->getModuleCommands($child) as $command) {
144
                $commands[] = $command;
145
            }
146 1
        }
147
148 1
        $controllerPath = $module->getControllerPath();
149 1
        if (is_dir($controllerPath)) {
150
            $files = scandir($controllerPath);
151
            foreach ($files as $file) {
152
                if (!empty($file) && substr_compare($file, 'Controller.php', -14, 14) === 0) {
153
                    $controllerClass = $module->controllerNamespace . '\\' . substr(basename($file), 0, -4);
154
                    if ($this->validateControllerClass($controllerClass)) {
155
                        $commands[] = $prefix . Inflector::camel2id(substr(basename($file), 0, -14));
156
                    }
157
                }
158
            }
159
        }
160
161 1
        return $commands;
162
    }
163
164
    /**
165
     * Validates if the given class is a valid console controller class.
166
     * @param string $controllerClass
167
     * @return boolean
168
     */
169
    protected function validateControllerClass($controllerClass)
170
    {
171
        if (class_exists($controllerClass)) {
172
            $class = new \ReflectionClass($controllerClass);
173
            return !$class->isAbstract() && $class->isSubclassOf('yii\console\Controller');
174
        } else {
175
            return false;
176
        }
177
    }
178
179
    /**
180
     * Displays all available commands.
181
     */
182 1
    protected function getDefaultHelp()
183
    {
184 1
        $commands = $this->getCommandDescriptions();
185 1
        $this->stdout("\nThis is Yii version " . \Yii::getVersion() . ".\n");
186 1
        if (!empty($commands)) {
187 1
            $this->stdout("\nThe following commands are available:\n\n", Console::BOLD);
188 1
            $len = 0;
189 1
            foreach ($commands as $command => $description) {
190 1
                $result = Yii::$app->createController($command);
191 1
                if ($result !== false && $result[0] instanceof Controller) {
192
                    /** @var $controller Controller */
193 1
                    list($controller, $actionID) = $result;
0 ignored issues
show
Unused Code introduced by
The assignment to $actionID is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
194 1
                    $actions = $this->getActions($controller);
195 1
                    if (!empty($actions)) {
196 1
                        $prefix = $controller->getUniqueId();
197 1
                        foreach ($actions as $action) {
198 1
                            $string = $prefix . '/' . $action;
199 1
                            if ($action === $controller->defaultAction) {
200 1
                                $string .= ' (default)';
201 1
                            }
202 1
                            if (($l = strlen($string)) > $len) {
203 1
                                $len = $l;
204 1
                            }
205 1
                        }
206 1
                    }
207 1
                } elseif (($l = strlen($command)) > $len) {
208
                    $len = $l;
209
                }
210 1
            }
211 1
            foreach ($commands as $command => $description) {
212 1
                $this->stdout('- ' . $this->ansiFormat($command, Console::FG_YELLOW));
213 1
                $this->stdout(str_repeat(' ', $len + 4 - strlen($command)));
214 1
                $this->stdout(Console::wrapText($description, $len + 4 + 2), Console::BOLD);
215 1
                $this->stdout("\n");
216
217 1
                $result = Yii::$app->createController($command);
218 1
                if ($result !== false && $result[0] instanceof Controller) {
219 1
                    list($controller, $actionID) = $result;
0 ignored issues
show
Unused Code introduced by
The assignment to $actionID is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
220 1
                    $actions = $this->getActions($controller);
221 1
                    if (!empty($actions)) {
222 1
                        $prefix = $controller->getUniqueId();
223 1
                        foreach ($actions as $action) {
224 1
                            $string = '  ' . $prefix . '/' . $action;
225 1
                            $this->stdout('  ' . $this->ansiFormat($string, Console::FG_GREEN));
226 1
                            if ($action === $controller->defaultAction) {
227 1
                                $string .= ' (default)';
228 1
                                $this->stdout(' (default)', Console::FG_YELLOW);
229 1
                            }
230 1
                            $summary = $controller->getActionHelpSummary($controller->createAction($action));
231 1
                            if ($summary !== '') {
232 1
                                $this->stdout(str_repeat(' ', $len + 4 - strlen($string)));
233 1
                                $this->stdout(Console::wrapText($summary, $len + 4 + 2));
234 1
                            }
235 1
                            $this->stdout("\n");
236 1
                        }
237 1
                    }
238 1
                    $this->stdout("\n");
239 1
                }
240 1
            }
241 1
            $scriptName = $this->getScriptName();
242 1
            $this->stdout("\nTo see the help of each command, enter:\n", Console::BOLD);
243 1
            $this->stdout("\n  $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' '
244 1
                            . $this->ansiFormat('<command-name>', Console::FG_CYAN) . "\n\n");
245 1
        } else {
246
            $this->stdout("\nNo commands are found.\n\n", Console::BOLD);
247
        }
248 1
    }
249
250
    /**
251
     * Displays the overall information of the command.
252
     * @param Controller $controller the controller instance
253
     */
254
    protected function getCommandHelp($controller)
255
    {
256
        $controller->color = $this->color;
257
258
        $this->stdout("\nDESCRIPTION\n", Console::BOLD);
259
        $comment = $controller->getHelp();
260
        if ($comment !== '') {
261
            $this->stdout("\n$comment\n\n");
262
        }
263
264
        $actions = $this->getActions($controller);
265
        if (!empty($actions)) {
266
            $this->stdout("\nSUB-COMMANDS\n\n", Console::BOLD);
267
            $prefix = $controller->getUniqueId();
268
269
            $maxlen = 5;
270
            foreach ($actions as $action) {
271
                $len = strlen($prefix.'/'.$action) + 2 + ($action === $controller->defaultAction ? 10 : 0);
272
                if ($maxlen < $len) {
273
                    $maxlen = $len;
274
                }
275
            }
276
            foreach ($actions as $action) {
277
                $this->stdout('- ' . $this->ansiFormat($prefix.'/'.$action, Console::FG_YELLOW));
278
                $len = strlen($prefix.'/'.$action) + 2;
279
                if ($action === $controller->defaultAction) {
280
                    $this->stdout(' (default)', Console::FG_GREEN);
281
                    $len += 10;
282
                }
283
                $summary = $controller->getActionHelpSummary($controller->createAction($action));
0 ignored issues
show
Bug introduced by
It seems like $controller->createAction($action) can be null; however, getActionHelpSummary() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
284
                if ($summary !== '') {
285
                    $this->stdout(str_repeat(' ', $maxlen - $len + 2) . Console::wrapText($summary, $maxlen + 2));
286
                }
287
                $this->stdout("\n");
288
            }
289
            $scriptName = $this->getScriptName();
290
            $this->stdout("\nTo see the detailed information about individual sub-commands, enter:\n");
291
            $this->stdout("\n  $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' '
292
                            . $this->ansiFormat('<sub-command>', Console::FG_CYAN) . "\n\n");
293
        }
294
    }
295
296
    /**
297
     * Displays the detailed information of a command action.
298
     * @param Controller $controller the controller instance
299
     * @param string $actionID action ID
300
     * @throws Exception if the action does not exist
301
     */
302 2
    protected function getSubCommandHelp($controller, $actionID)
303
    {
304 2
        $action = $controller->createAction($actionID);
305 2
        if ($action === null) {
306
            $name = $this->ansiFormat(rtrim($controller->getUniqueId() . '/' . $actionID, '/'), Console::FG_YELLOW);
307
            throw new Exception("No help for unknown sub-command \"$name\".");
308
        }
309
310 2
        $description = $controller->getActionHelp($action);
311 2
        if ($description !== '') {
312 2
            $this->stdout("\nDESCRIPTION\n", Console::BOLD);
313 2
            $this->stdout("\n$description\n\n");
314 2
        }
315
316 2
        $this->stdout("\nUSAGE\n\n", Console::BOLD);
317 2
        $scriptName = $this->getScriptName();
318 2
        if ($action->id === $controller->defaultAction) {
319 2
            $this->stdout($scriptName . ' ' . $this->ansiFormat($controller->getUniqueId(), Console::FG_YELLOW));
320 2
        } else {
321
            $this->stdout($scriptName . ' ' . $this->ansiFormat($action->getUniqueId(), Console::FG_YELLOW));
322
        }
323
324 2
        $args = $controller->getActionArgsHelp($action);
325 2
        foreach ($args as $name => $arg) {
326 2
            if ($arg['required']) {
327
                $this->stdout(' <' . $name . '>', Console::FG_CYAN);
328
            } else {
329 2
                $this->stdout(' [' . $name . ']', Console::FG_CYAN);
330
            }
331 2
        }
332
333 2
        $options = $controller->getActionOptionsHelp($action);
334 2
        $options[\yii\console\Application::OPTION_APPCONFIG] = [
335 2
            'type' => 'string',
336 2
            'default' => null,
337 2
            'comment' => "custom application configuration file path.\nIf not set, default application configuration is used.",
338
        ];
339 2
        ksort($options);
340
341 2
        if (!empty($options)) {
342 2
            $this->stdout(' [...options...]', Console::FG_RED);
343 2
        }
344 2
        $this->stdout("\n\n");
345
346 2
        if (!empty($args)) {
347 2
            foreach ($args as $name => $arg) {
348 2
                $this->stdout($this->formatOptionHelp(
349 2
                        '- ' . $this->ansiFormat($name, Console::FG_CYAN),
350 2
                        $arg['required'],
351 2
                        $arg['type'],
352 2
                        $arg['default'],
353 2
                        $arg['comment']) . "\n\n");
354 2
            }
355 2
        }
356
357 2
        if (!empty($options)) {
358 2
            $this->stdout("\nOPTIONS\n\n", Console::BOLD);
359 2
            foreach ($options as $name => $option) {
360 2
                $this->stdout($this->formatOptionHelp(
361 2
                        $this->ansiFormat('--' . $name . $this->formatOptionAliases($controller, $name), Console::FG_RED, empty($option['required']) ? Console::FG_RED : Console::BOLD),
362 2
                        !empty($option['required']),
363 2
                        $option['type'],
364 2
                        $option['default'],
365 2
                        $option['comment']) . "\n\n");
366 2
            }
367 2
        }
368 2
    }
369
370
    /**
371
     * Generates a well-formed string for an argument or option.
372
     * @param string $name the name of the argument or option
373
     * @param boolean $required whether the argument is required
374
     * @param string $type the type of the option or argument
375
     * @param mixed $defaultValue the default value of the option or argument
376
     * @param string $comment comment about the option or argument
377
     * @return string the formatted string for the argument or option
378
     */
379 2
    protected function formatOptionHelp($name, $required, $type, $defaultValue, $comment)
380
    {
381 2
        $comment = trim($comment);
382 2
        $type = trim($type);
383 2
        if (strncmp($type, 'bool', 4) === 0) {
384 2
            $type = 'boolean, 0 or 1';
385 2
        }
386
387 2
        if ($defaultValue !== null && !is_array($defaultValue)) {
388 2
            if ($type === null) {
389
                $type = gettype($defaultValue);
390
            }
391 2
            if (is_bool($defaultValue)) {
392
                // show as integer to avoid confusion
393 2
                $defaultValue = (int) $defaultValue;
394 2
            }
395 2
            if (is_string($defaultValue)) {
396 1
                $defaultValue = "'" . $defaultValue . "'";
397 1
            } else {
398 2
                $defaultValue = var_export($defaultValue, true);
399
            }
400 2
            $doc = "$type (defaults to $defaultValue)";
401 2
        } else {
402 2
            $doc = $type;
403
        }
404
405 2
        if ($doc === '') {
406
            $doc = $comment;
407 2
        } elseif ($comment !== '') {
408 2
            $doc .= "\n" . preg_replace('/^/m', '  ', $comment);
409 2
        }
410
411 2
        $name = $required ? "$name (required)" : $name;
412
413 2
        return $doc === '' ? $name : "$name: $doc";
414
    }
415
416
    /**
417
     * @param Controller $controller the controller instance
418
     * @param string $option the option name
419
     * @return string the formatted string for the alias argument or option
420
     * @since 2.0.8
421
     */
422 2
    protected function formatOptionAliases($controller, $option)
423
    {
424 2
        $aliases = $controller->optionAliases();
425 2
        foreach ($aliases as $name => $value) {
426 2
            if ($value === $option) {
427 2
                return ', -' . $name;
428
            }
429 2
        }
430 2
        return '';
431
    }
432
433
    /**
434
     * @return string the name of the cli script currently running.
435
     */
436 3
    protected function getScriptName()
437
    {
438 3
        return basename(Yii::$app->request->scriptFile);
439
    }
440
}
441