Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php declare(strict_types=1); |
||
16 | class Configuration implements \ArrayAccess |
||
17 | { |
||
18 | private $parent; |
||
19 | private $values = []; |
||
20 | |||
21 | 42 | public function __construct(Configuration $parent = null) |
|
22 | { |
||
23 | 42 | $this->parent = $parent; |
|
24 | 42 | } |
|
25 | |||
26 | 42 | public function set(string $name, $value) |
|
27 | { |
||
28 | 42 | $this->values[$name] = $value; |
|
29 | 42 | } |
|
30 | |||
31 | 11 | View Code Duplication | public function has(string $name): bool |
32 | { |
||
33 | 11 | $ok = array_key_exists($name, $this->values); |
|
34 | 11 | if ($ok) { |
|
35 | 8 | return true; |
|
36 | } |
||
37 | 5 | if ($this->parent) { |
|
38 | 2 | return $this->parent->has($name); |
|
39 | } |
||
40 | 3 | return false; |
|
41 | } |
||
42 | |||
43 | 8 | public function add(string $name, array $array) |
|
44 | { |
||
45 | 8 | if ($this->has($name)) { |
|
46 | 7 | $config = $this->get($name); |
|
47 | 7 | if (!is_array($config)) { |
|
48 | 2 | throw new ConfigurationException("Config option \"$name\" isn't array."); |
|
49 | } |
||
50 | 5 | $this->set($name, array_merge_alternate($config, $array)); |
|
51 | } else { |
||
52 | 1 | $this->set($name, $array); |
|
53 | } |
||
54 | 6 | } |
|
55 | |||
56 | 30 | public function get(string $name, $default = null) |
|
57 | { |
||
58 | 30 | if (array_key_exists($name, $this->values)) { |
|
59 | 27 | if (is_closure($this->values[$name])) { |
|
60 | 2 | return $this->values[$name] = $this->parse(call_user_func($this->values[$name])); |
|
61 | } else { |
||
62 | 27 | return $this->parse($this->values[$name]); |
|
63 | } |
||
64 | } |
||
65 | |||
66 | 12 | if ($this->parent) { |
|
67 | 7 | $rawValue = $this->parent->fetch($name); |
|
68 | 7 | if ($rawValue !== null) { |
|
69 | 5 | if (is_closure($rawValue)) { |
|
70 | 1 | return $this->values[$name] = $this->parse(call_user_func($rawValue)); |
|
71 | } else { |
||
72 | 4 | return $this->values[$name]= $this->parse($rawValue); |
|
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | 8 | if ($default !== null) { |
|
78 | 7 | return $this->parse($default); |
|
79 | } |
||
80 | |||
81 | 2 | throw new ConfigurationException("Config option \"$name\" does not exist."); |
|
82 | } |
||
83 | |||
84 | 7 | View Code Duplication | protected function fetch($name) |
85 | { |
||
86 | 7 | if (array_key_exists($name, $this->values)) { |
|
87 | 5 | return $this->values[$name]; |
|
88 | } |
||
89 | 4 | if ($this->parent) { |
|
90 | 1 | return $this->parent->fetch($name); |
|
91 | } |
||
92 | 3 | return null; |
|
93 | } |
||
94 | |||
95 | 29 | public function parse($value) |
|
103 | |||
104 | 6 | private function parseCallback(array $matches) |
|
105 | { |
||
106 | 6 | return isset($matches[1]) ? $this->get($matches[1]) : null; |
|
107 | } |
||
108 | |||
109 | 3 | public function offsetExists($offset) |
|
110 | { |
||
113 | |||
114 | 4 | public function offsetGet($offset) |
|
118 | |||
119 | 11 | public function offsetSet($offset, $value) |
|
123 | |||
124 | 1 | public function offsetUnset($offset) |
|
128 | |||
129 | 1 | public function persist() |
|
143 | |||
144 | public function load() |
||
148 | |||
149 | public function save() |
||
153 | |||
154 | private function configFile() { |
||
157 | } |
||
158 |