Completed
Push — master ( aaa756...6a04f6 )
by Henry
70:00 queued 35:28
created

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

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