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() |
||
124 | |||
125 | /** |
||
126 | * Test serialization of array without objects |
||
127 | * |
||
128 | * @dataProvider arrayNoObjectData |
||
129 | * @param array $array |
||
130 | * @param string $jsoned |
||
131 | * @return void |
||
132 | */ |
||
133 | public function testSerializeArrayNoObject($array, $jsoned) |
||
137 | |||
138 | /** |
||
139 | * Test unserialization of array without objects |
||
140 | * |
||
141 | * @dataProvider arrayNoObjectData |
||
142 | * @param array $array |
||
143 | * @param string $jsoned |
||
144 | * @return void |
||
145 | */ |
||
146 | public function testUnserializeArrayNoObject($array, $jsoned) |
||
150 | |||
151 | /** |
||
152 | * List of array data |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | public function arrayNoObjectData() |
||
169 | |||
170 | /** |
||
171 | * Test serialization of objects |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | public function testSerializeObject() |
||
205 | |||
206 | /** |
||
207 | * Test unserialization of objects |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | public function testUnserializeObjects() |
||
233 | |||
234 | |||
235 | /** |
||
236 | * Test serialization of objects using the custom serializers |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | public function testCustomObjectSerializer() |
||
247 | |||
248 | /** |
||
249 | * Test unserialization of objects using the custom serializers |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | public function testCustomObjectsUnserializer() |
||
261 | |||
262 | /** |
||
263 | * Test magic serialization methods |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | public function testSerializationMagicMethods() |
||
277 | |||
278 | /** |
||
279 | * Test serialization of DateTime classes |
||
280 | * |
||
281 | * Some interal classes, such as DateTime, cannot be initialized with |
||
282 | * ReflectionClass::newInstanceWithoutConstructor() |
||
283 | * |
||
284 | * @return void |
||
285 | */ |
||
286 | public function testSerializationOfDateTime() |
||
292 | |||
293 | /** |
||
294 | * Test the serialization of closures providing closure serializer |
||
295 | * |
||
296 | * @return void |
||
297 | */ |
||
298 | public function testSerializationOfClosure() |
||
319 | |||
320 | /** |
||
321 | * Test the unserialization of closures without providing closure serializer |
||
322 | * |
||
323 | * @return void |
||
324 | */ |
||
325 | public function testUnserializeOfClosureWithoutSerializer() |
||
343 | |||
344 | /** |
||
345 | * Test unserialize of unknown class |
||
346 | * |
||
347 | * @return void |
||
348 | */ |
||
349 | public function testUnserializeUnknownClass() |
||
355 | |||
356 | /** |
||
357 | * Test serialization of undeclared properties |
||
358 | * |
||
359 | * @return void |
||
360 | */ |
||
361 | public function testSerializationUndeclaredProperties() |
||
379 | |||
380 | /** |
||
381 | * Test undeclared properties setter (valid) |
||
382 | * |
||
383 | * @return void |
||
384 | */ |
||
385 | public function testSetUnserializeUndeclaredPropertyModeValid() { |
||
389 | |||
390 | /** |
||
391 | * Test undeclared properties setter (invalid) |
||
392 | * |
||
393 | * @return void |
||
394 | */ |
||
395 | public function testSetUnserializeUndeclaredPropertyModeInvalid() { |
||
399 | |||
400 | /** |
||
401 | * Test unserialization of undeclared properties in SET mode |
||
402 | * |
||
403 | * @return void |
||
404 | */ |
||
405 | public function testUnserializeUndeclaredPropertySet() { |
||
413 | |||
414 | /** |
||
415 | * Test unserialization of undeclared properties in IGNORE mode |
||
416 | * |
||
417 | * @return void |
||
418 | */ |
||
419 | public function testUnserializeUndeclaredPropertyIgnore() { |
||
426 | |||
427 | /** |
||
428 | * Test unserialization of undeclared properties in EXCEPTION mode |
||
429 | * |
||
430 | * @return void |
||
431 | */ |
||
432 | public function testUnserializeUndeclaredPropertyException() { |
||
439 | |||
440 | /** |
||
441 | * Test serialize with recursion |
||
442 | * |
||
443 | * @return void |
||
444 | */ |
||
445 | public function testSerializeRecursion() |
||
462 | |||
463 | /** |
||
464 | * Test unserialize with recursion |
||
465 | * |
||
466 | * @return void |
||
467 | */ |
||
468 | public function testUnserializeRecursion() |
||
483 | |||
484 | /** |
||
485 | * Test unserialize with bad JSON |
||
486 | * |
||
487 | * @return void |
||
488 | */ |
||
489 | public function testUnserializeBadJSON() |
||
494 | |||
495 | /** |
||
496 | * The test attempts to serialize an array containing a NAN |
||
497 | */ |
||
498 | public function testSerializeInvalidData() |
||
507 | |||
508 | /** |
||
509 | * @return void |
||
510 | */ |
||
511 | public function testSerializeBinaryStringScalar() |
||
521 | |||
522 | /** |
||
523 | * @return void |
||
524 | */ |
||
525 | public function testSerializeArrayWithBinaryStringsAsValues() |
||
536 | |||
537 | /** |
||
538 | * Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per: |
||
539 | * https://github.com/remicollet/pecl-json-c/issues/7 |
||
540 | * https://github.com/json-c/json-c/issues/108 |
||
541 | * |
||
542 | * @return void |
||
543 | */ |
||
544 | public function testSerializeArrayWithBinaryStringsAsKeys() |
||
555 | |||
556 | /** |
||
557 | * @return void |
||
558 | */ |
||
559 | public function testSerializeObjectWithBinaryStrings() |
||
572 | |||
573 | /* |
||
574 | * Test namespace change (backward compatibility) |
||
575 | * |
||
576 | * @return void |
||
577 | * @deprecated |
||
578 | */ |
||
579 | public function testNamespaceRename() |
||
587 | |||
588 | /** |
||
589 | * Test serialization of SplDoubleLinkedList |
||
590 | * |
||
591 | * @return void |
||
592 | */ |
||
593 | public function testSerializationOfSplDoublyLinkedList() |
||
601 | } |
||
602 |
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.