Completed
Push — 2.1 ( 6b99e4...fa71f8 )
by Dmitry
15:50
created

HelpController::getDefaultHelpHeader()   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 int 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
            [$controller, $actionID] = $result;
0 ignored issues
show
Bug introduced by
The variable $controller does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $actionID does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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
            } else {
64 2
                $this->getCommandHelp($controller);
65
            }
66
        } else {
67 1
            $this->getDefaultHelp();
68
        }
69 3
    }
70
71
    /**
72
     * List all available controllers and actions in machine readable format.
73
     * This is used for shell completion.
74
     * @since 2.0.11
75
     */
76 2
    public function actionList()
77
    {
78 2
        foreach ($this->getCommandDescriptions() as $command => $description) {
79 2
            $result = Yii::$app->createController($command);
80 2
            if ($result === false || !($result[0] instanceof Controller)) {
81
                continue;
82
            }
83
            /** @var $controller Controller */
84 2
            [$controller, $actionID] = $result;
0 ignored issues
show
Bug introduced by
The variable $controller does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $actionID does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
85 2
            $actions = $this->getActions($controller);
86 2
            if (!empty($actions)) {
87 2
                $prefix = $controller->getUniqueId();
88 2
                $this->stdout("$prefix\n");
89 2
                foreach ($actions as $action) {
90 2
                    $this->stdout("$prefix/$action\n");
91
                }
92
            }
93
        }
94 2
    }
95
96
    /**
97
     * List all available options for the $action in machine readable format.
98
     * This is used for shell completion.
99
     *
100
     * @param string $action route to action
101
     * @since 2.0.11
102
     */
103 1
    public function actionListActionOptions($action)
104
    {
105 1
        $result = Yii::$app->createController($action);
106
107 1
        if ($result === false || !($result[0] instanceof Controller)) {
108
            return;
109
        }
110
111
        /** @var Controller $controller */
112 1
        [$controller, $actionID] = $result;
0 ignored issues
show
Bug introduced by
The variable $controller does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $actionID does not exist. Did you mean $action?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
113 1
        $action = $controller->createAction($actionID);
0 ignored issues
show
Bug introduced by
The variable $actionID does not exist. Did you mean $action?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
114 1
        if ($action === null) {
115
            return;
116
        }
117
118 1
        foreach ($controller->getActionArgsHelp($action) as $argument => $help) {
119 1
            $description = preg_replace("~\R~", '', addcslashes($help['comment'], ':')) ?: $argument;
120 1
            $this->stdout($argument . ':' . $description . "\n");
121
        }
122
123 1
        $this->stdout("\n");
124 1
        foreach ($controller->getActionOptionsHelp($action) as $argument => $help) {
125 1
            $description = preg_replace("~\R~", '', addcslashes($help['comment'], ':'));
126 1
            $this->stdout('--' . $argument . ($description ? ':' . $description : '') . "\n");
127
        }
128 1
    }
129
130
    /**
131
     * Displays usage information for $action.
132
     *
133
     * @param string $action route to action
134
     * @since 2.0.11
135
     */
136 1
    public function actionUsage($action)
137
    {
138 1
        $result = Yii::$app->createController($action);
139
140 1
        if ($result === false || !($result[0] instanceof Controller)) {
141
            return;
142
        }
143
144
        /** @var Controller $controller */
145 1
        [$controller, $actionID] = $result;
0 ignored issues
show
Bug introduced by
The variable $controller does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $actionID does not exist. Did you mean $action?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
146 1
        $action = $controller->createAction($actionID);
0 ignored issues
show
Bug introduced by
The variable $actionID does not exist. Did you mean $action?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
147 1
        if ($action === null) {
148
            return;
149
        }
150
151 1
        $scriptName = $this->getScriptName();
152 1
        if ($action->id === $controller->defaultAction) {
153
            $this->stdout($scriptName . ' ' . $this->ansiFormat($controller->getUniqueId(), Console::FG_YELLOW));
154
        } else {
155 1
            $this->stdout($scriptName . ' ' . $this->ansiFormat($action->getUniqueId(), Console::FG_YELLOW));
156
        }
157
158 1
        foreach ($controller->getActionArgsHelp($action) as $name => $arg) {
159 1
            if ($arg['required']) {
160 1
                $this->stdout(' <' . $name . '>', Console::FG_CYAN);
161
            } else {
162 1
                $this->stdout(' [' . $name . ']', Console::FG_CYAN);
163
            }
164
        }
165
166 1
        $this->stdout("\n");
167 1
    }
168
169
    /**
170
     * Returns all available command names.
171
     * @return array all available command names
172
     */
173 18
    public function getCommands()
174
    {
175 18
        $commands = $this->getModuleCommands(Yii::$app);
176 18
        sort($commands);
177 18
        return array_unique($commands);
178
    }
179
180
    /**
181
     * Returns an array of commands an their descriptions.
182
     * @return array all available commands as keys and their description as values.
183
     */
184 3
    protected function getCommandDescriptions()
185
    {
186 3
        $descriptions = [];
187 3
        foreach ($this->getCommands() as $command) {
188 3
            $description = '';
189
190 3
            $result = Yii::$app->createController($command);
191 3
            if ($result !== false && $result[0] instanceof Controller) {
192 3
                [$controller, $actionID] = $result;
0 ignored issues
show
Bug introduced by
The variable $controller does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $actionID does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
193
                /** @var Controller $controller */
194 3
                $description = $controller->getHelpSummary();
195
            }
196
197 3
            $descriptions[$command] = $description;
198
        }
199
200 3
        return $descriptions;
201
    }
202
203
    /**
204
     * Returns all available actions of the specified controller.
205
     * @param Controller $controller the controller instance
206
     * @return array all available action IDs.
207
     */
208 20
    public function getActions($controller)
209
    {
210 20
        $actions = array_keys($controller->actions());
211 20
        $class = new \ReflectionClass($controller);
212 20
        foreach ($class->getMethods() as $method) {
213 20
            $name = $method->getName();
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
214 20
            if ($name !== 'actions' && $method->isPublic() && !$method->isStatic() && strncmp($name, 'action', 6) === 0) {
215 20
                $actions[] = Inflector::camel2id(substr($name, 6), '-', true);
216
            }
217
        }
218 20
        sort($actions);
219
220 20
        return array_unique($actions);
221
    }
222
223
    /**
224
     * Returns available commands of a specified module.
225
     * @param \yii\base\Module $module the module instance
226
     * @return array the available command names
227
     */
228 18
    protected function getModuleCommands($module)
229
    {
230 18
        $prefix = $module instanceof Application ? '' : $module->getUniqueId() . '/';
231
232 18
        $commands = [];
233 18
        foreach (array_keys($module->controllerMap) as $id) {
234 18
            $commands[] = $prefix . $id;
235
        }
236
237 18
        foreach ($module->getModules() as $id => $child) {
238 1
            if (($child = $module->getModule($id)) === null) {
239
                continue;
240
            }
241 1
            foreach ($this->getModuleCommands($child) as $command) {
242 1
                $commands[] = $command;
243
            }
244
        }
245
246 18
        $controllerPath = $module->getControllerPath();
247 18
        if (is_dir($controllerPath)) {
248 1
            $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($controllerPath, \RecursiveDirectoryIterator::KEY_AS_PATHNAME));
249 1
            $iterator = new \RegexIterator($iterator, '/.*Controller\.php$/', \RecursiveRegexIterator::GET_MATCH);
250 1
            foreach ($iterator as $matches) {
251 1
                $file = $matches[0];
252 1
                $relativePath = str_replace($controllerPath, '', $file);
253 1
                $class = strtr($relativePath, [
254 1
                    '/' => '\\',
255
                    '.php' => '',
256
                ]);
257 1
                $controllerClass = $module->controllerNamespace . $class;
258 1
                if ($this->validateControllerClass($controllerClass)) {
259 1
                    $dir = ltrim(pathinfo($relativePath, PATHINFO_DIRNAME), '\\/');
260
261 1
                    $command = Inflector::camel2id(substr(basename($file), 0, -14), '-', true);
262 1
                    if (!empty($dir)) {
263 1
                        $command = $dir . '/' . $command;
264
                    }
265 1
                    $commands[] = $prefix . $command;
266
                }
267
            }
268
        }
269
270 18
        return $commands;
271
    }
272
273
    /**
274
     * Validates if the given class is a valid console controller class.
275
     * @param string $controllerClass
276
     * @return bool
277
     */
278 1
    protected function validateControllerClass($controllerClass)
279
    {
280 1
        if (class_exists($controllerClass)) {
281 1
            $class = new \ReflectionClass($controllerClass);
282 1
            return !$class->isAbstract() && $class->isSubclassOf(Controller::class);
283
        }
284
285
        return false;
286
    }
287
288
    /**
289
     * Displays all available commands.
290
     */
291 1
    protected function getDefaultHelp()
292
    {
293 1
        $commands = $this->getCommandDescriptions();
294 1
        $this->stdout($this->getDefaultHelpHeader());
295 1
        if (!empty($commands)) {
296 1
            $this->stdout("\nThe following commands are available:\n\n", Console::BOLD);
297 1
            $len = 0;
298 1
            foreach ($commands as $command => $description) {
299 1
                $result = Yii::$app->createController($command);
300 1
                if ($result !== false && $result[0] instanceof Controller) {
301
                    /** @var $controller Controller */
302 1
                    [$controller, $actionID] = $result;
0 ignored issues
show
Bug introduced by
The variable $controller does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $actionID does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
303 1
                    $actions = $this->getActions($controller);
304 1
                    if (!empty($actions)) {
305 1
                        $prefix = $controller->getUniqueId();
306 1
                        foreach ($actions as $action) {
307 1
                            $string = $prefix . '/' . $action;
308 1
                            if ($action === $controller->defaultAction) {
309 1
                                $string .= ' (default)';
310
                            }
311 1
                            if (($l = strlen($string)) > $len) {
312 1
                                $len = $l;
313
                            }
314
                        }
315
                    }
316
                } elseif (($l = strlen($command)) > $len) {
317 1
                    $len = $l;
318
                }
319
            }
320 1
            foreach ($commands as $command => $description) {
321 1
                $this->stdout('- ' . $this->ansiFormat($command, Console::FG_YELLOW));
322 1
                $this->stdout(str_repeat(' ', $len + 4 - strlen($command)));
323 1
                $this->stdout(Console::wrapText($description, $len + 4 + 2), Console::BOLD);
324 1
                $this->stdout("\n");
325
326 1
                $result = Yii::$app->createController($command);
327 1
                if ($result !== false && $result[0] instanceof Controller) {
328 1
                    [$controller, $actionID] = $result;
329 1
                    $actions = $this->getActions($controller);
330 1
                    if (!empty($actions)) {
331 1
                        $prefix = $controller->getUniqueId();
332 1
                        foreach ($actions as $action) {
333 1
                            $string = '  ' . $prefix . '/' . $action;
334 1
                            $this->stdout('  ' . $this->ansiFormat($string, Console::FG_GREEN));
335 1
                            if ($action === $controller->defaultAction) {
336 1
                                $string .= ' (default)';
337 1
                                $this->stdout(' (default)', Console::FG_YELLOW);
338
                            }
339 1
                            $summary = $controller->getActionHelpSummary($controller->createAction($action));
0 ignored issues
show
Documentation introduced by
$controller->createAction($action) is of type object|null, but the function expects a object<yii\base\Action>.

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...
340 1
                            if ($summary !== '') {
341 1
                                $this->stdout(str_repeat(' ', $len + 4 - strlen($string)));
342 1
                                $this->stdout(Console::wrapText($summary, $len + 4 + 2));
343
                            }
344 1
                            $this->stdout("\n");
345
                        }
346
                    }
347 1
                    $this->stdout("\n");
348
                }
349
            }
350 1
            $scriptName = $this->getScriptName();
351 1
            $this->stdout("\nTo see the help of each command, enter:\n", Console::BOLD);
352 1
            $this->stdout("\n  $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' '
353 1
                . $this->ansiFormat('<command-name>', Console::FG_CYAN) . "\n\n");
354
        } else {
355
            $this->stdout("\nNo commands are found.\n\n", Console::BOLD);
356
        }
357 1
    }
358
359
    /**
360
     * Displays the overall information of the command.
361
     * @param Controller $controller the controller instance
362
     */
363
    protected function getCommandHelp($controller)
364
    {
365
        $controller->color = $this->color;
366
367
        $this->stdout("\nDESCRIPTION\n", Console::BOLD);
368
        $comment = $controller->getHelp();
369
        if ($comment !== '') {
370
            $this->stdout("\n$comment\n\n");
371
        }
372
373
        $actions = $this->getActions($controller);
374
        if (!empty($actions)) {
375
            $this->stdout("\nSUB-COMMANDS\n\n", Console::BOLD);
376
            $prefix = $controller->getUniqueId();
377
378
            $maxlen = 5;
379
            foreach ($actions as $action) {
380
                $len = strlen($prefix . '/' . $action) + 2 + ($action === $controller->defaultAction ? 10 : 0);
381
                if ($maxlen < $len) {
382
                    $maxlen = $len;
383
                }
384
            }
385
            foreach ($actions as $action) {
386
                $this->stdout('- ' . $this->ansiFormat($prefix . '/' . $action, Console::FG_YELLOW));
387
                $len = strlen($prefix . '/' . $action) + 2;
388
                if ($action === $controller->defaultAction) {
389
                    $this->stdout(' (default)', Console::FG_GREEN);
390
                    $len += 10;
391
                }
392
                $summary = $controller->getActionHelpSummary($controller->createAction($action));
0 ignored issues
show
Documentation introduced by
$controller->createAction($action) is of type object|null, but the function expects a object<yii\base\Action>.

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...
393
                if ($summary !== '') {
394
                    $this->stdout(str_repeat(' ', $maxlen - $len + 2) . Console::wrapText($summary, $maxlen + 2));
395
                }
396
                $this->stdout("\n");
397
            }
398
            $scriptName = $this->getScriptName();
399
            $this->stdout("\nTo see the detailed information about individual sub-commands, enter:\n");
400
            $this->stdout("\n  $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' '
401
                . $this->ansiFormat('<sub-command>', Console::FG_CYAN) . "\n\n");
402
        }
403
    }
404
405
    /**
406
     * Displays the detailed information of a command action.
407
     * @param Controller $controller the controller instance
408
     * @param string $actionID action ID
409
     * @throws Exception if the action does not exist
410
     */
411 2
    protected function getSubCommandHelp($controller, $actionID)
412
    {
413 2
        $action = $controller->createAction($actionID);
414 2
        if ($action === null) {
415
            $name = $this->ansiFormat(rtrim($controller->getUniqueId() . '/' . $actionID, '/'), Console::FG_YELLOW);
416
            throw new Exception("No help for unknown sub-command \"$name\".");
417
        }
418
419 2
        $description = $controller->getActionHelp($action);
420 2
        if ($description !== '') {
421 2
            $this->stdout("\nDESCRIPTION\n", Console::BOLD);
422 2
            $this->stdout("\n$description\n\n");
423
        }
424
425 2
        $this->stdout("\nUSAGE\n\n", Console::BOLD);
426 2
        $scriptName = $this->getScriptName();
427 2
        if ($action->id === $controller->defaultAction) {
428 2
            $this->stdout($scriptName . ' ' . $this->ansiFormat($controller->getUniqueId(), Console::FG_YELLOW));
429
        } else {
430
            $this->stdout($scriptName . ' ' . $this->ansiFormat($action->getUniqueId(), Console::FG_YELLOW));
431
        }
432
433 2
        $args = $controller->getActionArgsHelp($action);
434 2
        foreach ($args as $name => $arg) {
435 2
            if ($arg['required']) {
436
                $this->stdout(' <' . $name . '>', Console::FG_CYAN);
437
            } else {
438 2
                $this->stdout(' [' . $name . ']', Console::FG_CYAN);
439
            }
440
        }
441
442 2
        $options = $controller->getActionOptionsHelp($action);
443 2
        $options[\yii\console\Application::OPTION_APPCONFIG] = [
444
            'type' => 'string',
445
            'default' => null,
446
            'comment' => "custom application configuration file path.\nIf not set, default application configuration is used.",
447
        ];
448 2
        ksort($options);
449
450 2
        if (!empty($options)) {
451 2
            $this->stdout(' [...options...]', Console::FG_RED);
452
        }
453 2
        $this->stdout("\n\n");
454
455 2
        if (!empty($args)) {
456 2
            foreach ($args as $name => $arg) {
457 2
                $this->stdout($this->formatOptionHelp(
458 2
                        '- ' . $this->ansiFormat($name, Console::FG_CYAN),
459 2
                        $arg['required'],
460 2
                        $arg['type'],
461 2
                        $arg['default'],
462 2
                        $arg['comment']) . "\n\n");
463
            }
464
        }
465
466 2
        if (!empty($options)) {
467 2
            $this->stdout("\nOPTIONS\n\n", Console::BOLD);
468 2
            foreach ($options as $name => $option) {
469 2
                $this->stdout($this->formatOptionHelp(
470 2
                        $this->ansiFormat('--' . $name . $this->formatOptionAliases($controller, $name),
471 2
                            Console::FG_RED, empty($option['required']) ? Console::FG_RED : Console::BOLD),
472 2
                        !empty($option['required']),
473 2
                        $option['type'],
474 2
                        $option['default'],
475 2
                        $option['comment']) . "\n\n");
476
            }
477
        }
478 2
    }
479
480
    /**
481
     * Generates a well-formed string for an argument or option.
482
     * @param string $name the name of the argument or option
483
     * @param bool $required whether the argument is required
484
     * @param string $type the type of the option or argument
485
     * @param mixed $defaultValue the default value of the option or argument
486
     * @param string $comment comment about the option or argument
487
     * @return string the formatted string for the argument or option
488
     */
489 2
    protected function formatOptionHelp($name, $required, $type, $defaultValue, $comment)
490
    {
491 2
        $comment = trim($comment);
492 2
        $type = trim($type);
493 2
        if (strncmp($type, 'bool', 4) === 0) {
494 2
            $type = 'boolean, 0 or 1';
495
        }
496
497 2
        if ($defaultValue !== null && !is_array($defaultValue)) {
498 2
            if ($type === null) {
499
                $type = gettype($defaultValue);
500
            }
501 2
            if (is_bool($defaultValue)) {
502
                // show as integer to avoid confusion
503 2
                $defaultValue = (int)$defaultValue;
504
            }
505 2
            if (is_string($defaultValue)) {
506 1
                $defaultValue = "'" . $defaultValue . "'";
507
            } else {
508 2
                $defaultValue = var_export($defaultValue, true);
509
            }
510 2
            $doc = "$type (defaults to $defaultValue)";
511
        } else {
512 2
            $doc = $type;
513
        }
514
515 2
        if ($doc === '') {
516
            $doc = $comment;
517 2
        } elseif ($comment !== '') {
518 2
            $doc .= "\n" . preg_replace('/^/m', '  ', $comment);
519
        }
520
521 2
        $name = $required ? "$name (required)" : $name;
522
523 2
        return $doc === '' ? $name : "$name: $doc";
524
    }
525
526
    /**
527
     * @param Controller $controller the controller instance
528
     * @param string $option the option name
529
     * @return string the formatted string for the alias argument or option
530
     * @since 2.0.8
531
     */
532 2
    protected function formatOptionAliases($controller, $option)
533
    {
534 2
        foreach ($controller->optionAliases() as $name => $value) {
535 2
            if ($value === $option) {
536 2
                return ', -' . $name;
537
            }
538
        }
539
540 2
        return '';
541
    }
542
543
    /**
544
     * @return string the name of the cli script currently running.
545
     */
546 4
    protected function getScriptName()
547
    {
548 4
        return basename(Yii::$app->request->scriptFile);
549
    }
550
551
    /**
552
     * Return a default help header.
553
     * @return string default help header.
554
     * @since 2.0.11
555
     */
556 1
    protected function getDefaultHelpHeader()
557
    {
558 1
        return "\nThis is Yii version " . \Yii::getVersion() . ".\n";
559
    }
560
}
561