Config   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.37%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 169
ccs 38
cts 43
cp 0.8837
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A data() 0 4 1
A __call() 0 19 3
A validateArgs() 0 14 4
A getMethod() 0 10 3
A getArgs() 0 10 3
A add() 0 4 1
A create() 0 4 1
A set() 0 4 1
1
<?php
2
3
namespace Chay22\RecSelMeter;
4
5
6
class Config
7
{
8
	/**
9
	 * @var array $rank
10
	 */
11
	private $rank = [
0 ignored issues
show
Unused Code introduced by
The property $rank is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
			 'auto banned'		=> -100,
13
			 'kaskus addict'	=> 2,
14
			 'kaskus maniac'	=> 3,
15
			 'kaskus geek'		=> 7,
16
			 'kaskus freak'		=> 10,
17
			 'made in kaskus'	=> 15,
18
			 'kaskus plus'		=> 15,
19
			 'reg. leader'		=> 15,
20
			 'moderator'		=> 15,
21
			 'kaskus online bazaar'	=> 200,
22
			];
23
24
	/**
25
	 * @var array $feedback
26
	 */
27
	private $feedback = [10];
0 ignored issues
show
Unused Code introduced by
The property $feedback is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
29
	/**
30
	 * @var array $feedbackPercent
31
	 */
32
	private $feedbackPercent = [10];
0 ignored issues
show
Unused Code introduced by
The property $feedbackPercent is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
33
34
	/**
35
	 * @var string $accountAge
36
	 */
37
	private $accountAge = 5;
0 ignored issues
show
Unused Code introduced by
The property $accountAge is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
38
39
	/**
40
	 * @var array $storeActive
41
	 */
42
	private $storeActive = [
0 ignored issues
show
Unused Code introduced by
The property $storeActive is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
43
				7 => 1,
44
				3 => 5,
45
				1 => 8,
46
				0 => 10,
47
			       ];
48
	/**
49
	 * @var string $imageCount
50
	 */				  
51
	private $imageCount = 1;
0 ignored issues
show
Unused Code introduced by
The property $imageCount is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
52
53
	/**
54
	 * @var array $sold
55
	 */
56
	private $sold = [
0 ignored issues
show
Unused Code introduced by
The property $sold is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
57
			 0  => 0,
58
			 1  => 20,
59
			 5  => 50,
60
			 10 => 100,
61
			];
62
	/**
63
	 * @var string $cod
64
	 */
65
	private $cod = 10;
0 ignored issues
show
Unused Code introduced by
The property $cod is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
66
67
	/**
68
	 * Return set of configuration data (properties)
69
	 * 
70
	 * @return array
71
	 */
72 4
	public function data()
73
	{
74 4
		return get_object_vars($this);
75
	}
76
77
	/**
78
	 * Create/add/modify chosen property value
79
	 *
80
	 * @see ::create()
81
	 * @see ::add()
82
	 * @see ::set()
83
	 */
84 3
	public function __call($name, $args = [])
85
	{	
86
		//Throw error if property is not found
87 3
		if (!$args = $this->validateArgs($args)) {
88
			throw new \Exception('Parameters need to be an array!');
89
		}
90
91
		//Change ::new() to ::create() since it's a reserved word
92 3
		if (stripos($name, 'new') !== false) {
93 1
			$name = str_replace('new', 'create', $name);
94 1
		}
95 3
		$method = $this->getMethod($name);
96 3
		$property = explode($method, $name);
97 3
		$property = end($property);
98 3
		$config['name'] = $this->getArgs($property);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
99 3
		$config['value'] = $args[0];
100
101 3
		return $this->{$method}($config);
102
	}
103
104 3
	private function validateArgs($args)
105
	{
106 3
		if (!is_array($args)) {
107
			return false;
108
		}
109
110 3
		foreach ($args[0] as $value) {
111 3
			if (!is_int($value)) {
112
				return false;
113
			}
114 3
		}
115
116 3
		return array_change_key_case($args, CASE_LOWER);
117
	}
118
119 3
	private function getMethod($name)
120
	{
121 3
		foreach(get_class_methods($this) as $method) {
122 3
			if(stripos($name, $method) !== false) {
123 3
				return $method;
124
			}
125 3
		}
126
		
127
		throw new \Exception('Method not found');
128
	}
129
130 3
	private function getArgs($name)
131
	{
132 3
		foreach(get_object_vars($this) as $property => $value) {
133 3
			if (stripos($property, $name) !== false) {
134 3
				return $property;
135
			}
136 1
		}
137
		
138
		throw new \Exception('Property not found');
139
	}
140
141
	/**
142
	 * Add new key and value for chosen config (property)
143
	 * 
144
	 * @param 	array 	$config  name of config and it's key and value
145
	 * @return void
146
	 */
147 1
	private function add($config = [])
148
	{
149 1
		$this->{$config['name']} += $config['value'];
150 1
	}
151
152
	/**
153
	 * Overwrite default configuration (key and) value
154
	 *
155
	 * @uses 	::new()
156
	 * @param 	array 	$config  name of config and it's key and value
157
	 * @return 	void
158
	 */
159 1
	private function create($config = [])
160
	{
161 1
		$this->{$config['name']} = $config['value'];
162 1
	}
163
164
	/**
165
	 * Modify value of configuration key
166
	 * 
167
	 * @param 	array 	$config  name of config and it's key and value
168
	 * @return void
169
	 */
170 1
	private function set($config = [])
171
	{	
172 1
		$this->{$config['name']} = $config['value'] + $this->{$config['name']};
173 1
	}
174
}
175