Completed
Push — master ( cbd317...231b94 )
by Henry
09:43
created

includes/Model/Setting.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\Model;
3
4
/**
5
 * parent class to provide the setting model
6
 *
7
 * @since 3.3.0
8
 *
9
 * @package Redaxscript
10
 * @category Model
11
 * @author Henry Ruhs
12
 */
13
14
class Setting extends ModelAbstract
15
{
16
	/**
17
	 * name of the table
18
	 *
19
	 * @var string
20
	 */
21
22
	protected $_table = 'settings';
23
24
	/**
25
	 * get the value from settings
26
	 *
27
	 * @since 3.3.0
28
	 *
29
	 * @param string $key key of the item
30
	 *
31
	 * @return string|null
32
	 */
33
34 2
	public function get(string $key = null) : ?string
35
	{
36 2
		$settings = $this->getAll();
37
38
		/* process settings */
39
40 2
		if ($key)
41
		{
42 2
			foreach ($settings as $setting)
0 ignored issues
show
The expression $settings of type array|object<IdiormResultSet>|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...
43
			{
44 2
				if ($setting->name === $key)
45
				{
46 2
					return $setting->value;
47
				}
48
			}
49
		}
50 1
		return null;
51
	}
52
53
	/**
54
	 * set the value to settings
55
	 *
56
	 * @since 3.3.0
57
	 *
58
	 * @param string $key key of the item
59
	 * @param string $value value of the item
60
	 *
61
	 * @return bool
62
	 */
63
64 1
	public function set(string $key = null, string $value = null) : bool
65
	{
66
		return $this
67 1
			->query()
68 1
			->where('name', $key)
69 1
			->findOne()
70 1
			->set('value', $value)
71 1
			->save();
72
	}
73
}
74