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

includes/Registry.php (1 issue)

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;
3
4
use function array_key_exists;
5
use function array_merge;
6
use function is_array;
7
8
/**
9
 * children class to store the registry
10
 *
11
 * @since 2.1.0
12
 *
13
 * @package Redaxscript
14
 * @category Registry
15
 * @author Gary Aylward
16
 */
17
18
class Registry extends Singleton
19
{
20
	/**
21
	 * array of the registry
22
	 *
23
	 * @var array
24
	 */
25
26
	protected static $_registryArray = [];
27
28
	/**
29
	 * init the class
30
	 *
31
	 * @since 2.1.0
32
	 *
33
	 * @param array $registryArray array of the registry
34
	 */
35
36 1
	public function init(array $registryArray = []) : void
37
	{
38 1
		self::$_registryArray = array_merge(self::$_registryArray, $registryArray);
39 1
	}
40
41
	/**
42
	 * get the value from registry
43
	 *
44
	 * @since 3.0.0
45
	 *
46
	 * @param string $key key of the item
47
	 *
48
	 * @return string|array|null
49
	 */
50
51 3
	public function get(string $key = null)
52
	{
53 3
		if (is_array(self::$_registryArray) && array_key_exists($key, self::$_registryArray))
54
		{
55 1
			return self::$_registryArray[$key];
56
		}
57 2
		if (!$key)
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type null|string is loosely compared to false; 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...
58
		{
59 1
			return self::$_registryArray;
60
		}
61 1
		return null;
62
	}
63
64
	/**
65
	 * set the value to registry
66
	 *
67
	 * @since 2.1.0
68
	 *
69
	 * @param string $key key of the item
70
	 * @param string|array|null $value value of the item
71
	 */
72
73 2
	public function set(string $key = null, $value = null) : void
74
	{
75 2
		self::$_registryArray[$key] = $value;
76 2
	}
77
}
78