Complex classes like JsonSerializerTest 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 JsonSerializerTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class JsonSerializerTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * Serializer instance |
||
14 | * |
||
15 | * @var JsonSerializer |
||
16 | */ |
||
17 | protected $serializer; |
||
18 | |||
19 | /** |
||
20 | * Test case setup |
||
21 | * |
||
22 | * @return void |
||
23 | */ |
||
24 | public function setUp() |
||
30 | |||
31 | /** |
||
32 | * Test serialization of scalar values |
||
33 | * |
||
34 | * @dataProvider scalarData |
||
35 | * @param mixed $scalar |
||
36 | * @param string $jsoned |
||
37 | * @return void |
||
38 | */ |
||
39 | public function testSerializeScalar($scalar, $jsoned) |
||
43 | |||
44 | /** |
||
45 | * Test serialization of float values with locale |
||
46 | * |
||
47 | * @return void |
||
48 | */ |
||
49 | public function testSerializeFloatLocalized() |
||
65 | /** |
||
66 | * Test unserialization of scalar values |
||
67 | * |
||
68 | * @dataProvider scalarData |
||
69 | * @param mixed $scalar |
||
70 | * @param string $jsoned |
||
71 | * @return void |
||
72 | */ |
||
73 | public function testUnserializeScalar($scalar, $jsoned) |
||
77 | |||
78 | /** |
||
79 | * List of scalar data |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | public function scalarData() |
||
100 | |||
101 | /** |
||
102 | * Test the serialization of resources |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | public function testSerializeResource() |
||
111 | |||
112 | /** |
||
113 | * Test the serialization of closures when not providing closure serializer |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function testSerializeClosureWithoutSerializer() |
||
126 | |||
127 | /** |
||
128 | * Test serialization of array without objects |
||
129 | * |
||
130 | * @dataProvider arrayNoObjectData |
||
131 | * @param array $array |
||
132 | * @param string $jsoned |
||
133 | * @return void |
||
134 | */ |
||
135 | public function testSerializeArrayNoObject($array, $jsoned) |
||
139 | |||
140 | /** |
||
141 | * Test unserialization of array without objects |
||
142 | * |
||
143 | * @dataProvider arrayNoObjectData |
||
144 | * @param array $array |
||
145 | * @param string $jsoned |
||
146 | * @return void |
||
147 | */ |
||
148 | public function testUnserializeArrayNoObject($array, $jsoned) |
||
152 | |||
153 | /** |
||
154 | * List of array data |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | public function arrayNoObjectData() |
||
171 | |||
172 | /** |
||
173 | * Test serialization of objects |
||
174 | * |
||
175 | * @return void |
||
176 | */ |
||
177 | public function testSerializeObject() |
||
207 | |||
208 | /** |
||
209 | * Test unserialization of objects |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | public function testUnserializeObjects() |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Test serialization of objects using the custom serializers |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | public function testCustomObjectSerializer() |
||
250 | |||
251 | /** |
||
252 | * Test unserialization of objects using the custom serializers |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | public function testCustomObjectsUnserializer() |
||
264 | |||
265 | /** |
||
266 | * Test magic serialization methods |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | public function testSerializationMagicMethods() |
||
280 | |||
281 | /** |
||
282 | * Test serialization of DateTime classes |
||
283 | * |
||
284 | * Some interal classes, such as DateTime, cannot be initialized with |
||
285 | * ReflectionClass::newInstanceWithoutConstructor() |
||
286 | * |
||
287 | * @return void |
||
288 | */ |
||
289 | public function testSerializationOfDateTime() |
||
295 | |||
296 | /** |
||
297 | * Test the serialization of closures providing closure serializer |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | public function testSerializationOfClosure() |
||
324 | |||
325 | /** |
||
326 | * Test the unserialization of closures without providing closure serializer |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | public function testUnserializeOfClosureWithoutSerializer() |
||
350 | |||
351 | /** |
||
352 | * Test unserialize of unknown class |
||
353 | * |
||
354 | * @return void |
||
355 | */ |
||
356 | public function testUnserializeUnknownClass() |
||
362 | |||
363 | /** |
||
364 | * Test serialization of undeclared properties |
||
365 | * |
||
366 | * @return void |
||
367 | */ |
||
368 | public function testSerializationUndeclaredProperties() |
||
386 | |||
387 | /** |
||
388 | * Test undeclared properties setter (valid) |
||
389 | * |
||
390 | * @return void |
||
391 | */ |
||
392 | public function testSetUnserializeUndeclaredPropertyModeValid() |
||
397 | |||
398 | /** |
||
399 | * Test undeclared properties setter (invalid) |
||
400 | * |
||
401 | * @return void |
||
402 | */ |
||
403 | public function testSetUnserializeUndeclaredPropertyModeInvalid() |
||
408 | |||
409 | /** |
||
410 | * Test unserialization of undeclared properties in SET mode |
||
411 | * |
||
412 | * @return void |
||
413 | */ |
||
414 | public function testUnserializeUndeclaredPropertySet() |
||
423 | |||
424 | /** |
||
425 | * Test unserialization of undeclared properties in IGNORE mode |
||
426 | * |
||
427 | * @return void |
||
428 | */ |
||
429 | public function testUnserializeUndeclaredPropertyIgnore() |
||
437 | |||
438 | /** |
||
439 | * Test unserialization of undeclared properties in EXCEPTION mode |
||
440 | * |
||
441 | * @return void |
||
442 | */ |
||
443 | public function testUnserializeUndeclaredPropertyException() |
||
451 | |||
452 | /** |
||
453 | * Test serialize with recursion |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | public function testSerializeRecursion() |
||
474 | |||
475 | /** |
||
476 | * Test unserialize with recursion |
||
477 | * |
||
478 | * @return void |
||
479 | */ |
||
480 | public function testUnserializeRecursion() |
||
495 | |||
496 | /** |
||
497 | * Test unserialize with bad JSON |
||
498 | * |
||
499 | * @return void |
||
500 | */ |
||
501 | public function testUnserializeBadJSON() |
||
506 | |||
507 | /** |
||
508 | * The test attempts to serialize an array containing a NAN |
||
509 | */ |
||
510 | public function testSerializeInvalidData() |
||
519 | |||
520 | /** |
||
521 | * |
||
522 | * @return void |
||
523 | */ |
||
524 | public function testSerializeBinaryStringScalar() |
||
534 | |||
535 | /** |
||
536 | * |
||
537 | * @return void |
||
538 | */ |
||
539 | public function testSerializeArrayWithBinaryStringsAsValues() |
||
550 | |||
551 | /** |
||
552 | * Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per: |
||
553 | * https://github.com/remicollet/pecl-json-c/issues/7 |
||
554 | * https://github.com/json-c/json-c/issues/108 |
||
555 | * |
||
556 | * @return void |
||
557 | */ |
||
558 | public function testSerializeArrayWithBinaryStringsAsKeys() |
||
569 | |||
570 | /** |
||
571 | * |
||
572 | * @return void |
||
573 | */ |
||
574 | public function testSerializeObjectWithBinaryStrings() |
||
587 | |||
588 | /* |
||
589 | * Test namespace change (backward compatibility) |
||
590 | * |
||
591 | * @return void |
||
592 | * @deprecated |
||
593 | */ |
||
594 | public function testNamespaceRename() |
||
602 | |||
603 | /** |
||
604 | * Test serialization of SplDoubleLinkedList |
||
605 | * |
||
606 | * @return void |
||
607 | */ |
||
608 | public function testSerializationOfSplDoublyLinkedList() |
||
616 | |||
617 | /** |
||
618 | * Test serialization of Array references |
||
619 | * |
||
620 | * @return void |
||
621 | */ |
||
622 | public function testSerializationOfArrayReferences() |
||
637 | } |
||
638 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.