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 Redaxscript\Model; |
||
6 | use function str_pad; |
||
7 | |||
8 | /** |
||
9 | * children class to execute the setting command |
||
10 | * |
||
11 | * @since 3.0.0 |
||
12 | * |
||
13 | * @package Redaxscript |
||
14 | * @category Console |
||
15 | * @author Henry Ruhs |
||
16 | */ |
||
17 | |||
18 | class Setting extends CommandAbstract |
||
19 | { |
||
20 | /** |
||
21 | * array of the command |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | |||
26 | protected $_commandArray = |
||
27 | [ |
||
28 | 'setting' => |
||
29 | [ |
||
30 | 'description' => 'Setting command', |
||
31 | 'argumentArray' => |
||
32 | [ |
||
33 | 'list' => |
||
34 | [ |
||
35 | 'description' => 'List the settings' |
||
36 | ], |
||
37 | 'set' => |
||
38 | [ |
||
39 | 'description' => 'Set the setting', |
||
40 | 'optionArray' => |
||
41 | [ |
||
42 | 'key' => |
||
43 | [ |
||
44 | 'description' => 'Required setting key' |
||
45 | ], |
||
46 | 'value' => |
||
47 | [ |
||
48 | 'description' => 'Required setting value' |
||
49 | ] |
||
50 | ] |
||
51 | ] |
||
52 | ] |
||
53 | ] |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * run the command |
||
58 | * |
||
59 | * @param string $mode name of the mode |
||
60 | * |
||
61 | * @since 3.0.0 |
||
62 | * |
||
63 | * @return string|null |
||
64 | */ |
||
65 | 4 | ||
66 | public function run(string $mode = null) : ?string |
||
67 | 4 | { |
|
68 | 4 | $parser = new Parser($this->_request); |
|
69 | $parser->init($mode); |
||
70 | |||
71 | /* run command */ |
||
72 | 4 | ||
73 | 4 | $argumentKey = $parser->getArgument(1); |
|
74 | $haltOnError = (bool)$parser->getOption('halt-on-error'); |
||
75 | 1 | if ($argumentKey === 'list') |
|
76 | { |
||
77 | 3 | return $this->_list(); |
|
78 | } |
||
79 | 2 | if ($argumentKey === 'set') |
|
80 | { |
||
81 | 1 | return $this->_set($parser->getOption()) ? $this->success() : $this->error($haltOnError); |
|
82 | } |
||
83 | return $this->getHelp(); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * list the settings |
||
88 | * |
||
89 | * @since 3.0.0 |
||
90 | * |
||
91 | * @return string|null |
||
92 | 1 | */ |
|
93 | |||
94 | 1 | protected function _list() : ?string |
|
95 | 1 | { |
|
96 | 1 | $output = null; |
|
97 | $settingModel = new Model\Setting(); |
||
98 | $settings = $settingModel->getAll(); |
||
99 | |||
100 | 1 | /* process settings */ |
|
101 | |||
102 | 1 | foreach ($settings as $setting) |
|
0 ignored issues
–
show
|
|||
103 | { |
||
104 | 1 | $output .= str_pad($setting->name, 30) . $setting->value . PHP_EOL; |
|
105 | } |
||
106 | return $output; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * set the setting |
||
111 | * |
||
112 | * @since 3.0.0 |
||
113 | * |
||
114 | * @param array $optionArray |
||
115 | * |
||
116 | * @return bool |
||
117 | 2 | */ |
|
118 | |||
119 | 2 | protected function _set(array $optionArray = []) : bool |
|
120 | 2 | { |
|
121 | 2 | $settingModel = new Model\Setting(); |
|
122 | 2 | $key = $this->prompt('key', $optionArray); |
|
123 | $value = $this->prompt('value', $optionArray); |
||
124 | 1 | if ($key && $value) |
|
125 | { |
||
126 | 1 | return $settingModel->set($key, $value); |
|
127 | } |
||
128 | return false; |
||
129 | } |
||
130 | } |
||
131 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.