Completed
Push — master ( 154c2b...e96259 )
by Henry
04:55
created

includes/Console/Command/Help.php (1 issue)

Labels
Severity

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 Redaxscript\Console\Command;
3
4
use Redaxscript\Console\Parser;
5
use function array_key_exists;
6
use function array_keys;
7
use function end;
8
use function is_array;
9
use function is_string;
10
use function method_exists;
11
12
/**
13
 * children class to execute the help command
14
 *
15
 * @since 3.0.0
16
 *
17
 * @package Redaxscript
18
 * @category Console
19
 * @author Henry Ruhs
20
 */
21
22
class Help extends CommandAbstract
23
{
24
	/**
25
	 * array of the command
26
	 *
27
	 * @var array
28
	 */
29
30
	protected $_commandArray =
31
	[
32
		'help' =>
33
		[
34
			'description' => 'Help command',
35
			'argumentArray' =>
36
			[
37
				'<command>' =>
38
				[
39
					'description' => 'Show help for the <command>'
40
				]
41
			]
42
		]
43
	];
44
45
	/**
46
	 * run the command
47
	 *
48
	 * @since 3.0.0
49
	 *
50
	 * @param string $mode name of the mode
51
	 *
52
	 * @return string|null
53
	 */
54
55 2
	public function run(string $mode = null) : ?string
56
	{
57 2
		$parser = new Parser($this->_request);
58 2
		$parser->init($mode);
59
60
		/* run command */
61
62 2
		return $this->_list($parser->getArgument(1));
63
	}
64
65
	/**
66
	 * list the help
67
	 *
68
	 * @since 3.0.0
69
	 *
70
	 * @param string $argumentKey
71
	 *
72
	 * @return string|null
73
	 */
74
75 2
	protected function _list(string $argumentKey = null) : ?string
76
	{
77 2
		$output = null;
78
79
		/* collect each help */
80
81 2
		if (!is_array($this->_namespaceArray) || !array_key_exists($argumentKey, $this->_namespaceArray))
82
		{
83 1
			$namespaceKeys = array_keys($this->_namespaceArray);
84 1
			$lastKey = end($namespaceKeys);
85 1
			foreach ($this->_namespaceArray as $commandKey => $commandClass)
0 ignored issues
show
The expression $this->_namespaceArray of type string|array 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...
86
			{
87 1
				if (method_exists($commandClass, 'getHelp'))
88
				{
89 1
					$command = new $commandClass($this->_registry, $this->_request, $this->_language, $this->_config);
90 1
					$output .= $command->getHelp();
91 1
					if ($commandKey !== $lastKey)
92
					{
93 1
						$output .= PHP_EOL;
94
					}
95
				}
96
			}
97
		}
98
99
		/* else single help */
100
101
		else
102
		{
103 1
			$commandClass = $this->_namespaceArray[$argumentKey];
104 1
			if (method_exists($commandClass, 'getHelp'))
105
			{
106 1
				$command = new $commandClass($this->_registry, $this->_request, $this->_language, $this->_config);
107 1
				$output .= $command->getHelp();
108
			}
109
		}
110 2
		return $output;
111
	}
112
}
113