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:
Complex classes like JsonFileStore often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JsonFileStore, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class JsonFileStore implements SortableStore, CountableStore |
||
39 | { |
||
40 | /** |
||
41 | * Flag: Disable serialization of strings. |
||
42 | */ |
||
43 | const NO_SERIALIZE_STRINGS = 1; |
||
44 | |||
45 | /** |
||
46 | * Flag: Disable serialization of arrays. |
||
47 | */ |
||
48 | const NO_SERIALIZE_ARRAYS = 2; |
||
49 | |||
50 | /** |
||
51 | * Flag: Escape ">" and "<". |
||
52 | */ |
||
53 | const ESCAPE_GT_LT = 4; |
||
54 | |||
55 | /** |
||
56 | * Flag: Escape "&". |
||
57 | */ |
||
58 | const ESCAPE_AMPERSAND = 8; |
||
59 | |||
60 | /** |
||
61 | * Flag: Escape single quotes. |
||
62 | */ |
||
63 | const ESCAPE_SINGLE_QUOTE = 16; |
||
64 | |||
65 | /** |
||
66 | * Flag: Escape double quotes. |
||
67 | */ |
||
68 | const ESCAPE_DOUBLE_QUOTE = 32; |
||
69 | |||
70 | /** |
||
71 | * Flag: Don't escape forward slashes. |
||
72 | */ |
||
73 | const NO_ESCAPE_SLASH = 64; |
||
74 | |||
75 | /** |
||
76 | * Flag: Don't escape Unicode characters. |
||
77 | */ |
||
78 | const NO_ESCAPE_UNICODE = 128; |
||
79 | |||
80 | /** |
||
81 | * Flag: Format the JSON nicely. |
||
82 | */ |
||
83 | const PRETTY_PRINT = 256; |
||
84 | |||
85 | /** |
||
86 | * Flag: Terminate the JSON with a line feed. |
||
87 | */ |
||
88 | const TERMINATE_WITH_LINE_FEED = 512; |
||
89 | |||
90 | /** |
||
91 | * This seems to be the biggest float supported by json_encode()/json_decode(). |
||
92 | */ |
||
93 | const MAX_FLOAT = 1.0E+14; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | private $path; |
||
99 | |||
100 | /** |
||
101 | * @var int |
||
102 | */ |
||
103 | private $flags; |
||
104 | |||
105 | /** |
||
106 | * @var JsonEncoder |
||
107 | */ |
||
108 | private $encoder; |
||
109 | |||
110 | /** |
||
111 | * @var JsonDecoder |
||
112 | */ |
||
113 | private $decoder; |
||
114 | |||
115 | 115 | public function __construct($path, $flags = 0) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 85 | public function set($key, $value) |
|
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 25 | View Code Duplication | public function get($key, $default = null) |
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 32 | View Code Duplication | public function getOrFail($key) |
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | 15 | public function getMultiple(array $keys, $default = null) |
|
191 | { |
||
192 | 15 | $values = array(); |
|
193 | 15 | $data = $this->load(); |
|
194 | |||
195 | 13 | foreach ($keys as $key) { |
|
196 | 13 | KeyUtil::validate($key); |
|
197 | |||
198 | 13 | if (array_key_exists($key, $data)) { |
|
199 | 13 | $value = $this->unserializeValue($data[$key]); |
|
200 | } else { |
||
201 | 1 | $value = $default; |
|
202 | } |
||
203 | |||
204 | 12 | $values[$key] = $value; |
|
205 | } |
||
206 | |||
207 | 10 | return $values; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | 32 | public function getMultipleOrFail(array $keys) |
|
214 | { |
||
215 | 32 | $values = array(); |
|
216 | 32 | $data = $this->load(); |
|
217 | |||
218 | 30 | foreach ($keys as $key) { |
|
219 | 30 | KeyUtil::validate($key); |
|
220 | |||
221 | 30 | if (!array_key_exists($key, $data)) { |
|
222 | 1 | throw NoSuchKeyException::forKey($key); |
|
223 | } |
||
224 | |||
225 | 30 | $values[$key] = $this->unserializeValue($data[$key]); |
|
226 | } |
||
227 | |||
228 | 26 | return $values; |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | 11 | View Code Duplication | public function remove($key) |
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | 28 | public function exists($key) |
|
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | 115 | public function clear() |
|
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | 10 | public function keys() |
|
278 | |||
279 | /** |
||
280 | * {@inheritdoc} |
||
281 | */ |
||
282 | 8 | public function sort($flags = SORT_REGULAR) |
|
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | 2 | public function count() |
|
300 | |||
301 | 102 | private function load() |
|
313 | |||
314 | 115 | private function save($data) |
|
328 | |||
329 | 82 | private function serializeValue($value) |
|
350 | |||
351 | 61 | private function unserializeValue($value) |
|
352 | { |
||
374 | } |
||
375 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: