Completed
Push — master ( 1f2156...14cedc )
by Garrett
04:26
created

RuleValidator::checkBoolean()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 3
eloc 5
nc 3
nop 2
1
<?php
2
3
namespace Dice\Extra;
4
5
class RuleValidator
6
{
7
	private $dice;
8
9
	public function __construct(\Dice\Dice $dice)
10
	{
11
		$this->dice = $dice;
12
	}
13
14
	public function addRule($name, array $rule)
15
	{
16
		$this->checkValidKeys($rule);
17
		$this->checkBoolean($rule, 'inherit');
18
		$this->checkBoolean($rule, 'shared');
19
		$this->checkNumericArray($rule, 'constructParams');
20
		$this->checkNumericArray($rule, 'shareInstances');
21
		$this->checkNumericArray($rule, 'call');
22
		$this->checkAssocArray($rule, 'call');
23
24
		$this->dice->addRule($name, $rule);
25
	}
26
27
	public function create($name, array $args = [], array $share = [])
28
	{
29
		return $this->dice->create($name, $args, $share);
30
	}
31
32 View Code Duplication
	public function checkAssocArray($rule, $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
	{
34
		if (!isset($rule[$key])) {
35
			return;
36
		}
37
38
		if (count(array_filter(array_keys($rule[$key]), 'is_string')) === 0) {
39
			throw new \InvalidArgumentException('Rule option ' . $key . ' must be a an associative array');
40
		}
41
42
	}
43
44
	public function checkBoolean($rule, $key)
45
	{
46
		if (!isset($rule[$key])) {
47
			return;
48
		}
49
50
		if (!is_bool($rule[$key])) {
51
			throw new \InvalidArgumentException('Rule option ' . $key . ' must be true or false');
52
		}
53
	}
54
55 View Code Duplication
	public function checkNumericArray($rule, $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
	{
57
		if (!isset($rule[$key])) {
58
			return;
59
		}
60
61
		if (count(array_filter(array_keys($rule[$key]), 'is_string')) > 0) {
62
			throw new \InvalidArgumentException('Rule option ' . $key
63
				. ' must be a sequential array, not an associative array');
64
		}
65
	}
66
67
	private function checkValidKeys($rule)
68
	{
69
		$validKeys = ['call', 'shared', 'substitutions', 'instanceOf', 'inherit', 'shareInstances', 'constructParams'];
70
71
		foreach ($rule as $name => $value) {
72
			if (!in_array($name, $validKeys)) {
73
				throw new \InvalidArgumentException('Invalid rule option: ' . $name);
74
			}
75
		}
76
	}
77
}
78