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 |
||
8 | class Settings implements ReadOnlySettingsInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private $defaults = []; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $overrides = []; |
||
19 | |||
20 | /** |
||
21 | * @var bool |
||
22 | */ |
||
23 | private $locked = false; |
||
24 | |||
25 | /** |
||
26 | * Returns the value for the $key. Throws an exception is value is not found in overrides. |
||
27 | * |
||
28 | * @param string $key |
||
29 | * |
||
30 | * @throws OutOfBoundsException |
||
31 | * @return mixed |
||
32 | */ |
||
33 | 3 | View Code Duplication | public function get($key) |
45 | |||
46 | /** |
||
47 | * Returns the value for the $key searching through overrides and defaults. Returns null if not found. |
||
48 | * |
||
49 | * @param string $key |
||
50 | * |
||
51 | * @return mixed |
||
52 | */ |
||
53 | 4 | View Code Duplication | public function tryGet($key) |
65 | |||
66 | /** |
||
67 | * Returns true if there is an override or a default value for $key |
||
68 | * |
||
69 | * @param string $key |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | 3 | public function has($key) |
|
81 | |||
82 | /** |
||
83 | * @param string $key |
||
84 | * @param mixed $value |
||
85 | */ |
||
86 | 5 | public function set($key, $value) |
|
91 | |||
92 | /** |
||
93 | * @param string $key |
||
94 | * @param mixed $value |
||
95 | */ |
||
96 | 5 | public function setDefault($key, $value) |
|
101 | |||
102 | 9 | private function assertWriteable() |
|
108 | |||
109 | 2 | public function preventChanges() |
|
113 | } |