Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

includes/Console/Command/Config.php (2 issues)

Check for mismatching type of a variable.

Bug Minor

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 str_pad;
6
use function str_repeat;
7
use function strlen;
8
9
/**
10
 * children class to execute the config command
11
 *
12
 * @since 3.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Console
16
 * @author Henry Ruhs
17
 */
18
19
class Config extends CommandAbstract
20
{
21
	/**
22
	 * array of the command
23
	 *
24
	 * @var array
25
	 */
26
27
	protected $_commandArray =
28
	[
29
		'config' =>
30
		[
31
			'description' => 'Config command',
32
			'argumentArray' =>
33
			[
34
				'list' =>
35
				[
36
					'description' => 'List the configuration'
37
				],
38
				'set' =>
39
				[
40
					'description' => 'Set the configuration',
41
					'optionArray' =>
42
					[
43
						'db-type' =>
44
						[
45
							'description' => 'Required database type'
46
						],
47
						'db-host' =>
48
						[
49
							'description' => 'Required database host or file'
50
						],
51
						'db-name' =>
52
						[
53
							'description' => 'Optional database name'
54
						],
55
						'db-user' =>
56
						[
57
							'description' => 'Optional database user'
58
						],
59
						'db-password' =>
60
						[
61
							'description' => 'Optional database password'
62
						],
63
						'db-prefix' =>
64
						[
65
							'description' => 'Optional database prefix'
66
						]
67
					]
68
				],
69
				'parse' =>
70
				[
71
					'description' => 'Parse the configuration',
72
					'optionArray' =>
73
					[
74
						'db-url' =>
75
						[
76
							'description' => 'Required database url'
77
						]
78
					]
79
				],
80
				'lock' =>
81
				[
82
					'description' => 'Lock the production environment'
83
				]
84
			]
85
		]
86
	];
87
88
	/**
89
	 * run the command
90
	 *
91
	 * @since 3.0.0
92
	 *
93
	 * @param string $mode name of the mode
94
	 *
95
	 * @return string|null
96
	 */
97
98 7
	public function run(string $mode = null) : ?string
99
	{
100 7
		$parser = new Parser($this->_request);
101 7
		$parser->init($mode);
102
103
		/* run command */
104
105 7
		$argumentKey = $parser->getArgument(1);
106 7
		$haltOnError = (bool)$parser->getOption('halt-on-error');
107 7
		if ($argumentKey === 'list')
108
		{
109 1
			return $this->_list();
110
		}
111 6
		if ($argumentKey === 'set')
112
		{
113 2
			return $this->_set($parser->getOption()) ? $this->success() : $this->error($haltOnError);
0 ignored issues
show
It seems like $parser->getOption() targeting Redaxscript\Console\Parser::getOption() can also be of type null or string; however, Redaxscript\Console\Command\Config::_set() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
114
		}
115 4
		if ($argumentKey === 'parse')
116
		{
117 2
			return $this->_parse($parser->getOption()) ? $this->success() : $this->error($haltOnError);
0 ignored issues
show
It seems like $parser->getOption() targeting Redaxscript\Console\Parser::getOption() can also be of type null or string; however, Redaxscript\Console\Command\Config::_parse() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
118
		}
119 2
		if ($argumentKey === 'lock')
120
		{
121 1
			return $this->_lock() ? $this->success() : $this->error($haltOnError);
122
		}
123 1
		return $this->getHelp();
124
	}
125
126
	/**
127
	 * list the config
128
	 *
129
	 * @since 3.0.0
130
	 *
131
	 * @return string|null
132
	 */
133
134 1
	protected function _list() : ?string
135
	{
136 1
		$output = null;
137 1
		$configArray = $this->_config->get();
138
139
		/* process config */
140
141 1
		foreach ($configArray as $key => $value)
142
		{
143 1
			if ($key === 'dbPassword')
144
			{
145 1
				$value = str_repeat('*', strlen($value));
146
			}
147 1
			if ($value)
148
			{
149 1
				$output .= str_pad($key, 30) . $value . PHP_EOL;
150
			}
151
		}
152 1
		return $output;
153
	}
154
155
	/**
156
	 * set the config
157
	 *
158
	 * @since 3.0.0
159
	 *
160
	 * @param array $optionArray
161
	 *
162
	 * @return bool
163
	 */
164
165 2
	protected function _set(array $optionArray = []) : bool
166
	{
167 2
		$dbType = $this->prompt('db-type', $optionArray);
168 2
		$dbHost = $this->prompt('db-host', $optionArray);
169 2
		if ($dbType && $dbHost)
170
		{
171 1
			$this->_config->set('dbType', $dbType);
172 1
			$this->_config->set('dbHost', $dbHost);
173 1
			$this->_config->set('dbName', $optionArray['db-name']);
174 1
			$this->_config->set('dbUser', $optionArray['db-user']);
175 1
			$this->_config->set('dbPassword', $optionArray['db-password']);
176 1
			$this->_config->set('dbPrefix', $optionArray['db-prefix']);
177 1
			return $this->_config->write();
178
		}
179 1
		return false;
180
	}
181
182
	/**
183
	 * parse the config
184
	 *
185
	 * @since 3.0.0
186
	 *
187
	 * @param array $optionArray
188
	 *
189
	 * @return bool
190
	 */
191
192 2
	protected function _parse(array $optionArray = []) : bool
193
	{
194 2
		$dbUrl = $this->prompt('db-url', $optionArray);
195 2
		if ($dbUrl)
196
		{
197 1
			$this->_config->parse($dbUrl);
198 1
			return $this->_config->write();
199
		}
200 1
		return false;
201
	}
202
203
	/**
204
	 * lock the config
205
	 *
206
	 * @since 3.0.0
207
	 *
208
	 * @return bool
209
	 */
210
211 1
	protected function _lock() : bool
212
	{
213 1
		$this->_config->set('env', 'production');
214 1
		return $this->_config->write();
215
	}
216
}
217