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 |
||
20 | abstract class Parameter implements \ArrayAccess |
||
21 | { |
||
22 | protected $parameters = array(); |
||
23 | protected $parametersName = array(); |
||
24 | protected $mandatoryParameters = array(); |
||
25 | |||
26 | /** |
||
27 | * @param array $parameters |
||
28 | */ |
||
29 | 38 | public function __construct(array $parameters = array()) |
|
30 | { |
||
31 | 38 | foreach ($this->parametersName as $parameterName) { |
|
32 | 38 | $this->set($parameterName, null); |
|
33 | 38 | } |
|
34 | 38 | if (!empty($parameters)) { |
|
35 | 9 | $this->fromArray($parameters); |
|
36 | 9 | } |
|
37 | 38 | } |
|
38 | |||
39 | /** |
||
40 | * @param string $key |
||
41 | * @param mixed $value |
||
42 | */ |
||
43 | 38 | View Code Duplication | public function set($key, $value) |
|
|||
44 | { |
||
45 | 38 | if (!in_array($key, $this->parametersName, true)) { |
|
46 | 1 | throw new InvalidArgumentException(sprintf('%s is not a valid parameter name.', $key)); |
|
47 | } |
||
48 | 38 | $this->parameters[$key] = $value; |
|
49 | 38 | } |
|
50 | |||
51 | /** |
||
52 | * @param string $key |
||
53 | * |
||
54 | * @return mixed|null The value at the specified index or null. |
||
55 | */ |
||
56 | 16 | public function get($key) |
|
57 | { |
||
58 | 16 | if (array_key_exists($key, $this->parameters)) { |
|
59 | 13 | return $this->parameters[$key]; |
|
60 | } |
||
61 | |||
62 | 3 | return; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param array $array |
||
67 | */ |
||
68 | 11 | public function fromArray($array) |
|
69 | { |
||
70 | 11 | foreach ($array as $key => $value) { |
|
71 | 11 | $this->set($key, $value); |
|
72 | 11 | } |
|
73 | 11 | } |
|
74 | |||
75 | /** |
||
76 | * @return array |
||
77 | */ |
||
78 | 3 | public function toArray() |
|
82 | |||
83 | /** |
||
84 | * @return bool |
||
85 | */ |
||
86 | 6 | public function areAllMandatoryParametersSet() |
|
87 | { |
||
88 | 6 | foreach ($this->mandatoryParameters as $param) { |
|
89 | 6 | if (!isset($this->$param)) { |
|
90 | 3 | return false; |
|
91 | } |
||
92 | 3 | } |
|
93 | |||
94 | 3 | return true; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Returns whether the requested index exists |
||
99 | * |
||
100 | * @param string $key |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | 1 | public function offsetExists($key) |
|
108 | |||
109 | /** |
||
110 | * Returns the value at the specified index |
||
111 | * |
||
112 | * @param string $key The index with the value. |
||
113 | * |
||
114 | * @return mixed|null The value at the specified index or null. |
||
115 | */ |
||
116 | 4 | public function offsetGet($key) |
|
120 | |||
121 | /** |
||
122 | * Sets the value at the specified index to $value |
||
123 | * |
||
124 | * @param string $key The index being set. |
||
125 | * @param mixed $value The new value for the index |
||
126 | */ |
||
127 | 2 | public function offsetSet($key, $value) |
|
131 | |||
132 | /** |
||
133 | * Unsets the value at the specified index |
||
134 | * |
||
135 | * @param string $key |
||
136 | */ |
||
137 | 1 | public function offsetUnset($key) |
|
141 | |||
142 | /** |
||
143 | * Magic getter, calls getXXX if exists. |
||
144 | * |
||
145 | * @param string $key |
||
146 | * |
||
147 | * @return mixed |
||
148 | */ |
||
149 | 6 | View Code Duplication | public function __get($key) |
150 | { |
||
151 | 6 | $getter = 'get' . $this->classify($key); |
|
152 | 6 | if (method_exists($this, $getter)) { |
|
153 | 1 | return call_user_func(array($this, $getter)); |
|
154 | } |
||
155 | |||
156 | 6 | return $this->get($key); |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Magic setter, calls getXXX if exists. |
||
161 | * @param $key |
||
162 | * @param $value |
||
163 | * |
||
164 | * @return mixed |
||
165 | */ |
||
166 | 1 | View Code Duplication | public function __set($key, $value) |
167 | { |
||
168 | 1 | $setter = 'set' . $this->classify($key); |
|
169 | 1 | if (method_exists($this, $setter)) { |
|
170 | return call_user_func_array(array($this, $setter), array($value)); |
||
171 | } |
||
172 | 1 | $this->set($key, $value); |
|
173 | 1 | } |
|
174 | |||
175 | /** |
||
176 | * Converts a string into a CamelCase word. |
||
177 | * |
||
178 | * @param string $string The string to classify. |
||
179 | * |
||
180 | * @return string The classified word. |
||
181 | */ |
||
182 | 6 | private function classify($string) |
|
186 | |||
187 | /** |
||
188 | * Returns whether the requested index exists |
||
189 | * |
||
190 | * @param string $key |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | 6 | public function __isset($key) |
|
198 | |||
199 | /** |
||
200 | * Unsets the value at the specified index |
||
201 | * |
||
202 | * @param string $key |
||
203 | */ |
||
204 | 1 | public function __unset($key) |
|
208 | } |
||
209 |
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.