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() |
||
25 | { |
||
26 | parent::setUp(); |
||
27 | $customObjectSerializerMap['Zumba\\JsonSerializer\\Test\\SupportClasses\\MyType'] = new \Zumba\JsonSerializer\Test\SupportClasses\MyTypeSerializer(); |
||
|
|||
28 | $this->serializer = new JsonSerializer(null, $customObjectSerializerMap); |
||
29 | } |
||
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() |
||
50 | { |
||
51 | $possibleLocales = ['fr_FR', 'fr_FR.utf8', 'fr', 'fra', 'French']; |
||
52 | $originalLocale = setlocale(LC_NUMERIC, 0); |
||
53 | if (!setlocale(LC_NUMERIC, $possibleLocales)) { |
||
54 | $this->markTestSkipped('Unable to set an i18n locale.'); |
||
55 | } |
||
56 | |||
57 | $data = [1.0, 1.1, 0.00000000001, 1.999999999999, 223423.123456789, 1e5, 1e11]; |
||
58 | $expected = version_compare(PHP_VERSION, '7.1', '>=') ? |
||
59 | '[1.0,1.1,1.0e-11,1.999999999999,223423.123456789,100000.0,100000000000.0]' : |
||
60 | '[1.0,1.1,1.0e-11,1.999999999999,223423.12345679,100000.0,100000000000.0]'; |
||
61 | $this->assertSame($expected, $this->serializer->serialize($data)); |
||
62 | |||
63 | setlocale(LC_NUMERIC, $originalLocale); |
||
64 | } |
||
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() |
||
235 | |||
236 | |||
237 | /** |
||
238 | * Test serialization of objects using the custom serializers |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | public function testCustomObjectSerializer() |
||
249 | |||
250 | /** |
||
251 | * Test unserialization of objects using the custom serializers |
||
252 | * |
||
253 | * @return void |
||
254 | */ |
||
255 | public function testCustomObjectsUnserializer() |
||
263 | |||
264 | /** |
||
265 | * Test magic serialization methods |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function testSerializationMagicMethods() |
||
279 | |||
280 | /** |
||
281 | * Test serialization of DateTime classes |
||
282 | * |
||
283 | * Some interal classes, such as DateTime, cannot be initialized with |
||
284 | * ReflectionClass::newInstanceWithoutConstructor() |
||
285 | * |
||
286 | * @return void |
||
287 | */ |
||
288 | public function testSerializationOfDateTime() |
||
294 | |||
295 | /** |
||
296 | * Test the serialization of closures providing closure serializer |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | public function testSerializationOfClosure() |
||
323 | |||
324 | /** |
||
325 | * Test the unserialization of closures without providing closure serializer |
||
326 | * |
||
327 | * @return void |
||
328 | */ |
||
329 | public function testUnserializeOfClosureWithoutSerializer() |
||
349 | |||
350 | /** |
||
351 | * Test unserialize of unknown class |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | public function testUnserializeUnknownClass() |
||
361 | |||
362 | /** |
||
363 | * Test serialization of undeclared properties |
||
364 | * |
||
365 | * @return void |
||
366 | */ |
||
367 | public function testSerializationUndeclaredProperties() |
||
385 | |||
386 | /** |
||
387 | * Test undeclared properties setter (valid) |
||
388 | * |
||
389 | * @return void |
||
390 | */ |
||
391 | public function testSetUnserializeUndeclaredPropertyModeValid() |
||
396 | |||
397 | /** |
||
398 | * Test undeclared properties setter (invalid) |
||
399 | * |
||
400 | * @return void |
||
401 | */ |
||
402 | public function testSetUnserializeUndeclaredPropertyModeInvalid() |
||
407 | |||
408 | /** |
||
409 | * Test unserialization of undeclared properties in SET mode |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | public function testUnserializeUndeclaredPropertySet() |
||
422 | |||
423 | /** |
||
424 | * Test unserialization of undeclared properties in IGNORE mode |
||
425 | * |
||
426 | * @return void |
||
427 | */ |
||
428 | public function testUnserializeUndeclaredPropertyIgnore() |
||
436 | |||
437 | /** |
||
438 | * Test unserialization of undeclared properties in EXCEPTION mode |
||
439 | * |
||
440 | * @return void |
||
441 | */ |
||
442 | public function testUnserializeUndeclaredPropertyException() |
||
450 | |||
451 | /** |
||
452 | * Test serialize with recursion |
||
453 | * |
||
454 | * @return void |
||
455 | */ |
||
456 | public function testSerializeRecursion() |
||
473 | |||
474 | /** |
||
475 | * Test unserialize with recursion |
||
476 | * |
||
477 | * @return void |
||
478 | */ |
||
479 | public function testUnserializeRecursion() |
||
494 | |||
495 | /** |
||
496 | * Test unserialize with bad JSON |
||
497 | * |
||
498 | * @return void |
||
499 | */ |
||
500 | public function testUnserializeBadJSON() |
||
505 | |||
506 | /** |
||
507 | * The test attempts to serialize an array containing a NAN |
||
508 | */ |
||
509 | public function testSerializeInvalidData() |
||
518 | |||
519 | /** |
||
520 | * |
||
521 | * @return void |
||
522 | */ |
||
523 | public function testSerializeBinaryStringScalar() |
||
533 | |||
534 | /** |
||
535 | * |
||
536 | * @return void |
||
537 | */ |
||
538 | public function testSerializeArrayWithBinaryStringsAsValues() |
||
549 | |||
550 | /** |
||
551 | * Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per: |
||
552 | * https://github.com/remicollet/pecl-json-c/issues/7 |
||
553 | * https://github.com/json-c/json-c/issues/108 |
||
554 | * |
||
555 | * @return void |
||
556 | */ |
||
557 | public function testSerializeArrayWithBinaryStringsAsKeys() |
||
568 | |||
569 | /** |
||
570 | * |
||
571 | * @return void |
||
572 | */ |
||
573 | public function testSerializeObjectWithBinaryStrings() |
||
586 | |||
587 | /* |
||
588 | * Test namespace change (backward compatibility) |
||
589 | * |
||
590 | * @return void |
||
591 | * @deprecated |
||
592 | */ |
||
593 | public function testNamespaceRename() |
||
601 | |||
602 | /** |
||
603 | * Test serialization of SplDoubleLinkedList |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | public function testSerializationOfSplDoublyLinkedList() |
||
615 | } |
||
616 |
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.