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) |
|
142 | { |
||
143 | 85 | KeyUtil::validate($key); |
|
144 | |||
145 | 83 | if (is_float($value) && $value > self::MAX_FLOAT) { |
|
146 | 1 | throw new UnsupportedValueException('The JSON file store cannot handle floats larger than 1.0E+14.'); |
|
147 | } |
||
148 | |||
149 | 82 | $data = $this->load(); |
|
150 | 82 | $data[$key] = $this->serializeValue($value); |
|
151 | |||
152 | 80 | $this->save($data); |
|
153 | 76 | } |
|
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 25 | View Code Duplication | public function get($key, $default = null) |
159 | { |
||
160 | 25 | KeyUtil::validate($key); |
|
161 | |||
162 | 23 | $data = $this->load(); |
|
163 | |||
164 | 21 | if (!array_key_exists($key, $data)) { |
|
165 | 1 | return $default; |
|
166 | } |
||
167 | |||
168 | 20 | return $this->unserializeValue($data[$key]); |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 32 | View Code Duplication | public function getOrFail($key) |
175 | { |
||
176 | 32 | KeyUtil::validate($key); |
|
177 | |||
178 | 30 | $data = $this->load(); |
|
179 | |||
180 | 28 | if (!array_key_exists($key, $data)) { |
|
181 | 1 | throw NoSuchKeyException::forKey($key); |
|
182 | } |
||
183 | |||
184 | 27 | return $this->unserializeValue($data[$key]); |
|
185 | } |
||
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) |
235 | { |
||
236 | 11 | KeyUtil::validate($key); |
|
237 | |||
238 | 9 | $data = $this->load(); |
|
239 | |||
240 | 9 | if (!array_key_exists($key, $data)) { |
|
241 | 7 | return false; |
|
242 | } |
||
243 | |||
244 | 9 | unset($data[$key]); |
|
245 | |||
246 | 9 | $this->save($data); |
|
247 | |||
248 | 8 | return true; |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | 28 | public function exists($key) |
|
255 | { |
||
256 | 28 | KeyUtil::validate($key); |
|
257 | |||
258 | 26 | $data = $this->load(); |
|
259 | |||
260 | 25 | return array_key_exists($key, $data); |
|
261 | } |
||
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) |
|
283 | { |
||
284 | 8 | $data = $this->load(); |
|
285 | |||
286 | 7 | ksort($data, $flags); |
|
287 | |||
288 | 7 | $this->save($data); |
|
289 | 6 | } |
|
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | 2 | public function count() |
|
295 | { |
||
296 | 2 | $data = $this->load(); |
|
297 | |||
298 | 1 | return count($data); |
|
299 | } |
||
300 | |||
301 | 102 | private function load() |
|
313 | |||
314 | 115 | private function save($data) |
|
315 | { |
||
316 | try { |
||
317 | 115 | $this->encoder->encodeFile($data, $this->path); |
|
318 | 7 | } catch (EncodingFailedException $e) { |
|
319 | 3 | if (JSON_ERROR_UTF8 === $e->getCode()) { |
|
320 | 3 | throw UnsupportedValueException::forType('binary', $this); |
|
321 | } |
||
322 | |||
328 | |||
329 | 82 | private function serializeValue($value) |
|
350 | |||
351 | 61 | private function unserializeValue($value) |
|
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: