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() |
||
63 | /** |
||
64 | * Test unserialization of scalar values |
||
65 | * |
||
66 | * @dataProvider scalarData |
||
67 | * @param mixed $scalar |
||
68 | * @param string $jsoned |
||
69 | * @return void |
||
70 | */ |
||
71 | public function testUnserializeScalar($scalar, $jsoned) |
||
75 | |||
76 | /** |
||
77 | * List of scalar data |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | public function scalarData() |
||
98 | |||
99 | /** |
||
100 | * Test the serialization of resources |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | public function testSerializeResource() |
||
109 | |||
110 | /** |
||
111 | * Test the serialization of closures when not providing closure serializer |
||
112 | * |
||
113 | * @return void |
||
114 | */ |
||
115 | public function testSerializeClosureWithoutSerializer() |
||
122 | |||
123 | /** |
||
124 | * Test serialization of array without objects |
||
125 | * |
||
126 | * @dataProvider arrayNoObjectData |
||
127 | * @param array $array |
||
128 | * @param string $jsoned |
||
129 | * @return void |
||
130 | */ |
||
131 | public function testSerializeArrayNoObject($array, $jsoned) |
||
135 | |||
136 | /** |
||
137 | * Test unserialization of array without objects |
||
138 | * |
||
139 | * @dataProvider arrayNoObjectData |
||
140 | * @param array $array |
||
141 | * @param string $jsoned |
||
142 | * @return void |
||
143 | */ |
||
144 | public function testUnserializeArrayNoObject($array, $jsoned) |
||
148 | |||
149 | /** |
||
150 | * List of array data |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | public function arrayNoObjectData() |
||
167 | |||
168 | /** |
||
169 | * Test serialization of objects |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | public function testSerializeObject() |
||
174 | { |
||
175 | $obj = new stdClass(); |
||
176 | $this->assertSame('{"@type":"stdClass"}', $this->serializer->serialize($obj)); |
||
177 | |||
178 | $obj = $empty = new SupportClasses\EmptyClass(); |
||
179 | $this->assertSame('{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}', $this->serializer->serialize($obj)); |
||
180 | |||
181 | $obj = new SupportClasses\AllVisibilities(); |
||
182 | $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"this is public","prot":"protected","priv":"dont tell anyone"}'; |
||
183 | $this->assertSame($expected, $this->serializer->serialize($obj)); |
||
184 | |||
185 | $obj->pub = 'new value'; |
||
186 | $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"new value","prot":"protected","priv":"dont tell anyone"}'; |
||
187 | $this->assertSame($expected, $this->serializer->serialize($obj)); |
||
188 | |||
189 | $obj->pub = $empty; |
||
190 | $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}'; |
||
191 | $this->assertSame($expected, $this->serializer->serialize($obj)); |
||
192 | |||
193 | $array = array('instance' => $empty); |
||
194 | $expected = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}'; |
||
195 | $this->assertSame($expected, $this->serializer->serialize($array)); |
||
196 | |||
197 | $obj = new stdClass(); |
||
198 | $obj->total = 10.0; |
||
199 | $obj->discount = 0.0; |
||
200 | $expected = '{"@type":"stdClass","total":10.0,"discount":0.0}'; |
||
201 | $this->assertSame($expected, $this->serializer->serialize($obj)); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Test unserialization of objects |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | public function testUnserializeObjects() |
||
210 | { |
||
211 | $serialized = '{"@type":"stdClass"}'; |
||
212 | $obj = $this->serializer->unserialize($serialized); |
||
213 | $this->assertInstanceOf('stdClass', $obj); |
||
214 | |||
215 | $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}'; |
||
216 | $obj = $this->serializer->unserialize($serialized); |
||
217 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj); |
||
218 | |||
219 | $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}'; |
||
220 | $obj = $this->serializer->unserialize($serialized); |
||
221 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\AllVisibilities', $obj); |
||
222 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj->pub); |
||
223 | $this->assertAttributeSame('protected', 'prot', $obj); |
||
224 | $this->assertAttributeSame('dont tell anyone', 'priv', $obj); |
||
225 | |||
226 | $serialized = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}'; |
||
227 | $array = $this->serializer->unserialize($serialized); |
||
228 | $this->assertTrue(is_array($array)); |
||
229 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $array['instance']); |
||
230 | } |
||
231 | |||
232 | |||
233 | /** |
||
234 | * Test serialization of objects using the custom serializers |
||
235 | * |
||
236 | * @return void |
||
237 | */ |
||
238 | public function testCustomObjectSerializer() |
||
239 | { |
||
240 | $obj = new SupportClasses\MyType(); |
||
241 | $obj->field1 = 'x'; |
||
242 | $obj->field2 = 'y'; |
||
243 | $this->assertSame('{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MyType","fields":"x y"}', $this->serializer->serialize($obj)); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Test unserialization of objects using the custom serializers |
||
248 | * |
||
249 | * @return void |
||
250 | */ |
||
251 | public function testCustomObjectsUnserializer() |
||
252 | { |
||
253 | $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MyType","fields":"x y"}'; |
||
254 | $obj = $this->serializer->unserialize($serialized); |
||
255 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\MyType', $obj); |
||
256 | $this->assertAttributeSame('x', 'field1', $obj); |
||
257 | $this->assertAttributeSame('y', 'field2', $obj); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Test magic serialization methods |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | public function testSerializationMagicMethods() |
||
275 | |||
276 | /** |
||
277 | * Test serialization of DateTime classes |
||
278 | * |
||
279 | * Some interal classes, such as DateTime, cannot be initialized with |
||
280 | * ReflectionClass::newInstanceWithoutConstructor() |
||
281 | * |
||
282 | * @return void |
||
283 | */ |
||
284 | public function testSerializationOfDateTime() |
||
290 | |||
291 | /** |
||
292 | * Test the serialization of closures providing closure serializer |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | public function testSerializationOfClosure() |
||
317 | |||
318 | /** |
||
319 | * Test the unserialization of closures without providing closure serializer |
||
320 | * |
||
321 | * @return void |
||
322 | */ |
||
323 | public function testUnserializeOfClosureWithoutSerializer() |
||
341 | |||
342 | /** |
||
343 | * Test unserialize of unknown class |
||
344 | * |
||
345 | * @return void |
||
346 | */ |
||
347 | public function testUnserializeUnknownClass() |
||
353 | |||
354 | /** |
||
355 | * Test serialization of undeclared properties |
||
356 | * |
||
357 | * @return void |
||
358 | */ |
||
359 | public function testSerializationUndeclaredProperties() |
||
377 | |||
378 | /** |
||
379 | * Test undeclared properties setter (valid) |
||
380 | * |
||
381 | * @return void |
||
382 | */ |
||
383 | public function testSetUnserializeUndeclaredPropertyModeValid() { |
||
387 | |||
388 | /** |
||
389 | * Test undeclared properties setter (invalid) |
||
390 | * |
||
391 | * @return void |
||
392 | */ |
||
393 | public function testSetUnserializeUndeclaredPropertyModeInvalid() { |
||
397 | |||
398 | /** |
||
399 | * Test unserialization of undeclared properties in SET mode |
||
400 | * |
||
401 | * @return void |
||
402 | */ |
||
403 | public function testUnserializeUndeclaredPropertySet() { |
||
411 | |||
412 | /** |
||
413 | * Test unserialization of undeclared properties in IGNORE mode |
||
414 | * |
||
415 | * @return void |
||
416 | */ |
||
417 | public function testUnserializeUndeclaredPropertyIgnore() { |
||
424 | |||
425 | /** |
||
426 | * Test unserialization of undeclared properties in EXCEPTION mode |
||
427 | * |
||
428 | * @return void |
||
429 | */ |
||
430 | public function testUnserializeUndeclaredPropertyException() { |
||
437 | |||
438 | /** |
||
439 | * Test serialize with recursion |
||
440 | * |
||
441 | * @return void |
||
442 | */ |
||
443 | public function testSerializeRecursion() |
||
460 | |||
461 | /** |
||
462 | * Test unserialize with recursion |
||
463 | * |
||
464 | * @return void |
||
465 | */ |
||
466 | public function testUnserializeRecursion() |
||
481 | |||
482 | /** |
||
483 | * Test unserialize with bad JSON |
||
484 | * |
||
485 | * @return void |
||
486 | */ |
||
487 | public function testUnserializeBadJSON() |
||
492 | |||
493 | /** |
||
494 | * The test attempts to serialize an array containing a NAN |
||
495 | */ |
||
496 | public function testSerializeInvalidData() |
||
505 | |||
506 | /** |
||
507 | * @return void |
||
508 | */ |
||
509 | public function testSerializeBinaryStringScalar() |
||
519 | |||
520 | /** |
||
521 | * @return void |
||
522 | */ |
||
523 | public function testSerializeArrayWithBinaryStringsAsValues() |
||
534 | |||
535 | /** |
||
536 | * Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per: |
||
537 | * https://github.com/remicollet/pecl-json-c/issues/7 |
||
538 | * https://github.com/json-c/json-c/issues/108 |
||
539 | * |
||
540 | * @return void |
||
541 | */ |
||
542 | public function testSerializeArrayWithBinaryStringsAsKeys() |
||
553 | |||
554 | /** |
||
555 | * @return void |
||
556 | */ |
||
557 | public function testSerializeObjectWithBinaryStrings() |
||
570 | |||
571 | /* |
||
572 | * Test namespace change (backward compatibility) |
||
573 | * |
||
574 | * @return void |
||
575 | * @deprecated |
||
576 | */ |
||
577 | public function testNamespaceRename() |
||
578 | { |
||
579 | $serializer = new \Zumba\Util\JsonSerializer(); |
||
580 | |||
581 | $f = fopen(__FILE__, 'r'); |
||
582 | $this->setExpectedException('Zumba\Exception\JsonSerializerException'); |
||
583 | $this->serializer->serialize($f); |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * Test serialization of SplDoubleLinkedList |
||
588 | * |
||
589 | * @return void |
||
590 | */ |
||
591 | public function testSerializationOfSplDoublyLinkedList() |
||
599 | |||
600 | /** |
||
601 | * Test the guard around only supported classes tripping |
||
602 | * |
||
603 | * @return void |
||
604 | */ |
||
605 | public function testSerializeOfUnsupportedClass() |
||
615 | |||
616 | /** |
||
617 | * Test the guard around only supported classes working |
||
618 | * |
||
619 | * @return void |
||
620 | */ |
||
621 | public function testSerializeOfSupportedClass() |
||
630 | |||
631 | /** |
||
632 | * Test the guard around only supported classes tripping |
||
633 | * |
||
634 | * @return void |
||
635 | */ |
||
636 | public function testUnserializeOfUnsupportedClass() |
||
660 | |||
661 | /** |
||
662 | * Test the serialization of closures when not providing closure serializer |
||
663 | * |
||
664 | * @return void |
||
665 | */ |
||
666 | public function testUnserializeOfSupportedClass() |
||
687 | } |
||
688 |
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.