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 |
||
10 | class JsonSerializerTest extends \PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * Serializer instance |
||
15 | * |
||
16 | * @var JsonSerializer |
||
17 | */ |
||
18 | protected $serializer; |
||
19 | |||
20 | /** |
||
21 | * Test case setup |
||
22 | * |
||
23 | * @return void |
||
24 | */ |
||
25 | public function setUp() |
||
31 | |||
32 | /** |
||
33 | * Test serialization of scalar values |
||
34 | * |
||
35 | * @dataProvider scalarData |
||
36 | * @param mixed $scalar |
||
37 | * @param string $jsoned |
||
38 | * @return void |
||
39 | */ |
||
40 | public function testSerializeScalar($scalar, $jsoned) |
||
44 | |||
45 | /** |
||
46 | * Test serialization of float values with locale |
||
47 | * |
||
48 | * @return void |
||
49 | */ |
||
50 | public function testSerializeFloatLocalized() |
||
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() |
||
208 | |||
209 | /** |
||
210 | * Test unserialization of objects |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | public function testUnserializeObjects() |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Test serialization of objects using the custom serializers |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | public function testCustomObjectSerializer() |
||
251 | |||
252 | /** |
||
253 | * Test serialization of objects using the custom serializers |
||
254 | * |
||
255 | * @return void |
||
256 | */ |
||
257 | public function testCustomObjectInheritanceSerializer() |
||
265 | |||
266 | /** |
||
267 | * Test unserialization of objects using the custom serializers |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | public function testCustomObjectsUnserializer() |
||
279 | |||
280 | /** |
||
281 | * Test unserialization of objects using the custom serializers |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | public function testCustomObjectsInheritanceUnserializer() |
||
293 | |||
294 | /** |
||
295 | * Test magic serialization methods |
||
296 | * |
||
297 | * @return void |
||
298 | */ |
||
299 | public function testSerializationMagicMethods() |
||
309 | |||
310 | /** |
||
311 | * Test serialization of DateTime classes |
||
312 | * |
||
313 | * Some interal classes, such as DateTime, cannot be initialized with |
||
314 | * ReflectionClass::newInstanceWithoutConstructor() |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | public function testSerializationOfDateTime() |
||
324 | |||
325 | /** |
||
326 | * Test the serialization of closures providing closure serializer |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | public function testSerializationOfClosure() |
||
351 | |||
352 | /** |
||
353 | * Test the unserialization of closures without providing closure serializer |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | public function testUnserializeOfClosureWithoutSerializer() |
||
375 | |||
376 | /** |
||
377 | * Test unserialize of unknown class |
||
378 | * |
||
379 | * @return void |
||
380 | */ |
||
381 | public function testUnserializeUnknownClass() |
||
387 | |||
388 | /** |
||
389 | * Test serialization of undeclared properties |
||
390 | * |
||
391 | * @return void |
||
392 | */ |
||
393 | public function testSerializationUndeclaredProperties() |
||
411 | |||
412 | /** |
||
413 | * Test serialize with recursion |
||
414 | * |
||
415 | * @return void |
||
416 | */ |
||
417 | public function testSerializeRecursion() |
||
434 | |||
435 | /** |
||
436 | * Test unserialize with recursion |
||
437 | * |
||
438 | * @return void |
||
439 | */ |
||
440 | public function testUnserializeRecursion() |
||
455 | |||
456 | /** |
||
457 | * Test unserialize with bad JSON |
||
458 | * |
||
459 | * @return void |
||
460 | */ |
||
461 | public function testUnserializeBadJSON() |
||
466 | |||
467 | /** |
||
468 | * The test attempts to serialize an array containing a NAN |
||
469 | */ |
||
470 | public function testSerializeInvalidData() |
||
479 | |||
480 | /** |
||
481 | * @return void |
||
482 | */ |
||
483 | public function testSerializeBinaryStringScalar() |
||
493 | |||
494 | /** |
||
495 | * @return void |
||
496 | */ |
||
497 | public function testSerializeArrayWithBinaryStringsAsValues() |
||
508 | |||
509 | /** |
||
510 | * Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per: |
||
511 | * https://github.com/remicollet/pecl-json-c/issues/7 |
||
512 | * https://github.com/json-c/json-c/issues/108 |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | public function testSerializeArrayWithBinaryStringsAsKeys() |
||
527 | |||
528 | /** |
||
529 | * @return void |
||
530 | */ |
||
531 | public function testSerializeObjectWithBinaryStrings() |
||
544 | |||
545 | /* |
||
546 | * Test namespace change (backward compatibility) |
||
547 | * |
||
548 | * @return void |
||
549 | * @deprecated |
||
550 | */ |
||
551 | public function testNamespaceRename() |
||
559 | |||
560 | /** |
||
561 | * Test serialization of SplDoubleLinkedList |
||
562 | * |
||
563 | * @return void |
||
564 | */ |
||
565 | public function testSerializationOfSplDoublyLinkedList() |
||
573 | |||
574 | /** |
||
575 | * Test serialization of SplDoubleLinkedList |
||
576 | * |
||
577 | * @return void |
||
578 | */ |
||
579 | public function testEntitySerializerRegistration() |
||
584 | } |
||
585 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..