Model_Base::set()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
namespace Clubdeuce\WPGoogleMaps;
4
5
/**
6
 * Class Model_Base
7
 * @package Clubdeuce\WPGoogleMaps
8
 *
9
 * @method array extra_args()
10
 */
11
class Model_Base {
12
13
	/**
14
	 * @var array
15
	 */
16
	protected $_extra_args;
17
18
	/**
19
	 * Model_Base constructor.
20
	 *
21
	 * @param array $args
22
	 */
23 10
	public function __construct( $args = array() ) {
24
25 10
		$args = wp_parse_args( $args );
26
27 10
		foreach ( $args as $key => $value ) {
28
29 9
			$this->set( $key, $value );
30
31
		}
32
33 10
	}
34
35
	/**
36
	 * @param string $property
37
	 * @param mixed  $value
38
	 */
39 3
	public function set( $property, $value ) {
40
41
		do {
42 3
			if ( property_exists( get_called_class(), "_{$property}" ) ) {
43 1
				$property = "_{$property}";
44 1
				$this->{$property} = $value;
45 1
				break;
46
			}
47
48 3
			$this->_extra_args[ $property ] = $value;
49 3
		} while ( false );
50
51 3
	}
52
53
	/**
54
	 * @param string $method_name
55
	 * @param array  $args
56
	 *
57
	 * @return mixed|null
58
	 */
59 16
	public function __call( $method_name, $args ) {
60
61 16
		$value = null;
62
63
		do {
64 16
			if ( property_exists( $this, "_{$method_name}" ) ) {
65 16
				$property = "_{$method_name}";
66 16
				$value    = $this->{$property};
67 16
				break;
68
			}
69
70 3
			if ( isset( $this->extra_args()[ $method_name ] ) ) {
71 2
				$value = $this->extra_args()[ $method_name ];
72 2
				break;
73
			}
74 1
		} while ( false );
75
76 16
		return $value;
77
78
	}
79
80
}
81