Passed
Push — develop ( 37b65a...9b346b )
by Paul
03:39
created

SiteMetaManager::__get()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 5
nop 1
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux\MetaBox;
4
5
use GeminiLabs\Pollux\Settings\Settings;
6
7
/**
8
 * SiteMeta::all();
9
 * SiteMeta::global();
10
 * SiteMeta::global('seo_enabled','fallback');
11
 * SiteMeta::get('global');
12
 * SiteMeta::get('global','seo_enabled','fallback');
13
 */
14
class SiteMetaManager
15
{
16
	protected $options;
17
18
	public function __construct()
19
	{
20
		$this->options = get_option( Settings::id(), [] );
21
	}
22
23
	/**
24
	 * @param string $group
25
	 * @return object|array|null
26
	 */
27
	public function __call( $group, $args )
28
	{
29
		$args = array_pad( $args, 2, null );
30
		$group = $this->$group;
31
		if( is_object( $group )) {
32
			return $group;
33
		}
34
		return $this->get( $group, $args[0], $args[1] );
35
	}
36
37
	/**
38
	 * @param string $group
39
	 * @return object|array|null
40
	 */
41
	public function __get( $group )
42
	{
43
		if( $group == 'all' ) {
44
			return (object) $this->options;
45
		}
46
		if( empty( $group )) {
47
			$group = $this->getDefaultGroup();
48
		}
49
		return isset( $this->options[$group] )
50
			? $this->options[$group]
51
			: null;
52
	}
53
54
	/**
55
	 * @param string $group
56
	 * @param string|null $key
57
	 * @param mixed $fallback
58
	 * @return mixed
59
	 */
60
	public function get( $group = '', $key = '', $fallback = null )
61
	{
62
		if( func_num_args() < 1 ) {
63
			return $this->all;
0 ignored issues
show
Documentation introduced by
The property all does not exist on object<GeminiLabs\Pollux\MetaBox\SiteMetaManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
64
		}
65
		if( is_string( $group )) {
66
			$group = $this->$group;
67
		}
68
		if( !is_array( $group )) {
69
			return $fallback;
70
		}
71
		if( is_null( $key )) {
72
			return $group;
73
		}
74
		return $this->getValue( $group, $key, $fallback );
75
	}
76
77
	/**
78
	 * @return string
79
	 */
80
	protected function getDefaultGroup()
81
	{
82
		return '';
83
	}
84
85
	/**
86
	 * @param string $key
87
	 * @param mixed $fallback
88
	 * @return mixed
89
	 */
90
	protected function getValue( array $group, $key, $fallback )
91
	{
92
		if( !array_key_exists( $key, $group )) {
93
			return $fallback;
94
		}
95
		return empty( $group[$key] ) && !is_null( $fallback )
96
			? $fallback
97
			: $group[$key];
98
	}
99
}
100