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