HelpController::getDefaultHelp()   C
last analyzed

Complexity

Conditions 15
Paths 28

Size

Total Lines 66
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 66
rs 5.9166
c 0
b 0
f 0
cc 15
nc 28
nop 0
ccs 0
cts 64
cp 0
crap 240

How to fix   Long Method    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
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace neon\core\console;
9
10
use \Yii;
11
use \yii\helpers\Console;
12
use \yii\helpers\Inflector;
13
14
/**
15
 * Display help information about the available commands for neo
16
 * Before you gasp in horror this is taking Yii and working with it to fit it in to how we want it to work
17
 * A simple convention of a controller class in a console folder... Not much to ask
18
 * Hooking this in with their current plan is kinda fugly because all the logic is in this help controller
19
 *
20
 * @todo This needs sanitizing!!
21
 *
22
 * @author Qiang Xue <[email protected]>
23
 * Hacked by Steve
24
 * @since 2.0
25
 */
26
class HelpController extends \yii\console\controllers\HelpController
27
{
28
	/**
29
	 * Displays all available commands.
30
	 */
31
	protected function getDefaultHelp()
32
	{
33
		$commands = $this->getCommandDescriptions();
34
		if (isset($commands['core/help'])) {
35
			unset($commands['core/help']);
36
		}
37
		$this->stdout("\nThis is Neon version " . \Neon::getVersion() . ".\n");
38
		if (!empty($commands)) {
39
			$this->stdout("\nThe following commands are available:\n\n", Console::BOLD);
40
			$len = 0;
41
			foreach ($commands as $command => $description) {
42
				$result = Yii::$app->createController($command);
43
				if ($result !== false) {
44
					/** @var $controller Controller */
45
					list($controller, $actionID) = $result;
46
					$actions = $this->getActions($controller);
47
					if (!empty($actions)) {
48
						$prefix = $controller->getUniqueId();
49
						foreach ($actions as $action) {
50
							$string = $prefix . '/' . $action;
51
							if ($action === $controller->defaultAction) {
52
								$string .= ' (default)';
53
							}
54
							if (($l = strlen($string)) > $len) {
55
								$len = $l;
56
							}
57
						}
58
					}
59
				} elseif (($l = strlen($command)) > $len) {
60
					$len = $l;
61
				}
62
				$this->stdout('- ' . $this->ansiFormat($command, Console::FG_YELLOW));
63
				$this->stdout(str_repeat(' ', $len + 4 - strlen($command)));
64
				$this->stdout(Console::wrapText($description, $len + 4 + 2), Console::BOLD);
65
				$this->stdout("\n");
66
67
				$result = Yii::$app->createController($command);
68
				if ($result !== false) {
69
					list($controller, $actionID) = $result;
70
					$actions = $this->getActions($controller);
71
					if (!empty($actions)) {
72
						$prefix = $controller->getUniqueId();
73
						foreach ($actions as $action) {
74
							$string = '  ' . $prefix . '/' . $action;
75
							$this->stdout('  ' . $this->ansiFormat($string, Console::FG_GREEN));
76
							if ($action === $controller->defaultAction) {
77
								$string .= ' (default)';
78
								$this->stdout(' (default)', Console::FG_YELLOW);
79
							}
80
							$summary = $controller->getActionHelpSummary($controller->createAction($action));
81
							if ($summary !== '') {
82
								$this->stdout(str_repeat(' ', $len + 4 - strlen($string)));
83
								$this->stdout(Console::wrapText($summary, $len + 4 + 2));
84
							}
85
							$this->stdout("\n");
86
						}
87
					}
88
					$this->stdout("\n");
89
				}
90
			}
91
			$scriptName = $this->getScriptName();
92
			$this->stdout("\nTo see the help of each command, enter:\n", Console::BOLD);
93
			$this->stdout("\n  $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' '
94
							. $this->ansiFormat('<command-name>', Console::FG_CYAN) . "\n\n");
95
		} else {
96
			$this->stdout("\nNo commands are found.\n\n", Console::BOLD);
97
		}
98
	}
99
100
	/**
101
	 * Return a default help header.
102
	 * @return string default help header.
103
	 * @since 2.0.11
104
	 */
105
	protected function getDefaultHelpHeader()
106
	{
107
		return "\nThis is Yii version " . \Yii::getVersion() . ".\n"
108
			. "\nThis is Neon version " . \Neon::getVersion() . ".\n";
109
110
	}
111
112
	/**
113
	 * @inheritdoc
114
	 */
115
	protected function getModuleCommands($module)
116
	{
117
		$prefix = $module instanceof Application ? '' : $module->getUniqueId() . '/';
0 ignored issues
show
Bug introduced by
The type neon\core\console\Application 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...
118
119
		$commands = [];
120
121
		if ($module->id != 'core') {
122
			foreach (array_keys($module->controllerMap) as $id) {
123
				$commands[] = $prefix . $id;
124
			}
125
		}
126
127
		// only include commands from apps.
128
		$controllerPath = $module->getControllerPath();
129
		if (is_dir($controllerPath)) {
130
			$files = scandir($controllerPath);
131
			foreach ($files as $file) {
132
				if (!empty($file) && substr_compare($file, 'Controller.php', -14, 14) === 0) {
133
					$controllerClass = $module->controllerNamespace . '\\' . substr(basename($file), 0, -4);
134
					if ($this->validateControllerClass($controllerClass)) {
135
//						if ($prefix == 'core' || $prefix == 'core/')
136
//							continue;
137
						$commands[] = $prefix . Inflector::camel2id(substr(basename($file), 0, -14));
138
					}
139
				}
140
			}
141
		}
142
143
		foreach ($module->getModules() as $id => $child) {
144
			if (($child = $module->getModule($id)) === null) {
145
				continue;
146
			}
147
			foreach ($this->getModuleCommands($child) as $command) {
148
				$commands[] = $command;
149
			}
150
		}
151
152
		return $commands;
153
	}
154
}
155