Completed
Push — master ( a52438...bf8826 )
by Henry
06:30
created

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

Check for loose comparison of strings.

Best Practice Bug Major

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
				'reset' =>
70
				[
71
					'description' => 'Reset the configuration'
72
				],
73
				'parse' =>
74
				[
75
					'description' => 'Parse the configuration',
76
					'optionArray' =>
77
					[
78
						'db-url' =>
79
						[
80
							'description' => 'Required database url'
81
						]
82
					]
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 6
	public function run(string $mode = null) : ?string
99
	{
100 6
		$parser = new Parser($this->_request);
101 6
		$parser->init($mode);
102
103
		/* run command */
104
105 6
		$argumentKey = $parser->getArgument(1);
106 6
		$haltOnError = (bool)$parser->getOption('halt-on-error');
107 6
		if ($argumentKey === 'list')
108
		{
109 1
			return $this->_list();
110
		}
111 5
		if ($argumentKey === 'set')
112
		{
113 2
			return $this->_set($parser->getOptionArray()) ? $this->success() : $this->error($haltOnError);
114
		}
115 3
		if ($argumentKey === 'reset')
116
		{
117
			return $this->_reset() ? $this->success() : $this->error($haltOnError);
118
		}
119 3
		if ($argumentKey === 'parse')
120
		{
121 2
			return $this->_parse($parser->getOptionArray()) ? $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->getArray();
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)
0 ignored issues
show
Bug Best Practice introduced by
The expression $dbType of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $dbHost of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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
	 * reset the config
184
	 *
185
	 * @since 4.5.0
186
	 *
187
	 * @return bool
188
	 */
189
190
	protected function _reset() : bool
191
	{
192
		$this->_config->reset();
193
		return $this->_config->write();
194
	}
195
196
	/**
197
	 * parse the config
198
	 *
199
	 * @since 3.0.0
200
	 *
201
	 * @param array $optionArray
202
	 *
203
	 * @return bool
204
	 */
205
206 2
	protected function _parse(array $optionArray = []) : bool
207
	{
208 2
		$dbUrl = $this->prompt('db-url', $optionArray);
209 2
		if ($dbUrl)
0 ignored issues
show
Bug Best Practice introduced by
The expression $dbUrl of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
210
		{
211 1
			$this->_config->parse($dbUrl);
212 1
			return $this->_config->write();
213
		}
214 1
		return false;
215
	}
216
}
217