Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
21 | class JsonHalSerializerTest extends TestCase |
||
22 | { |
||
23 | public function testSerializeLinks() |
||
24 | { |
||
25 | $links = array( |
||
26 | new Link('self', '/users/42', array('awesome' => 'exactly')), |
||
27 | new Link('foo', '/bar'), |
||
28 | new Link('foo', '/baz'), |
||
29 | new Link('bar', '/foo'), |
||
30 | new Link('bar', '/baz'), |
||
31 | new Link('bar', '/buzz'), |
||
32 | ); |
||
33 | |||
34 | $expectedSerializedLinks = array( |
||
35 | 'self' => array( |
||
36 | 'href' => '/users/42', |
||
37 | 'awesome' => 'exactly', |
||
38 | ), |
||
39 | 'foo' => array( |
||
40 | array('href' => '/bar'), |
||
41 | array('href' => '/baz'), |
||
42 | ), |
||
43 | 'bar' => array( |
||
44 | array('href' => '/foo'), |
||
45 | array('href' => '/baz'), |
||
46 | array('href' => '/buzz'), |
||
47 | ), |
||
48 | ); |
||
49 | |||
50 | $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
||
51 | |||
52 | $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor'); |
||
53 | $jsonSerializationVisitorProphecy |
||
54 | ->addData('_links', $expectedSerializedLinks) |
||
55 | ->shouldBeCalledTimes(1) |
||
56 | ; |
||
57 | |||
58 | $jsonHalSerializer = new JsonHalSerializer(); |
||
59 | $jsonHalSerializer->serializeLinks( |
||
60 | $links, |
||
61 | $jsonSerializationVisitorProphecy->reveal(), |
||
62 | $contextProphecy->reveal() |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | public function testSerializeEmbeddeds() |
||
67 | { |
||
68 | $acceptArguments = array( |
||
69 | array('name' => 'John'), |
||
70 | array('name' => 'Bar'), |
||
71 | array('name' => 'Baz'), |
||
72 | array('name' => 'Foo'), |
||
73 | array('name' => 'Baz'), |
||
74 | array('name' => 'Buzz'), |
||
75 | ); |
||
76 | |||
77 | $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
||
78 | foreach ($acceptArguments as $arg) { |
||
79 | $contextProphecy |
||
80 | ->accept($arg) |
||
81 | ->willReturnArgument() |
||
82 | ; |
||
83 | } |
||
84 | $contextProphecy->pushPropertyMetadata(Argument::type('Hateoas\Serializer\Metadata\RelationPropertyMetadata'))->shouldBeCalled(); |
||
85 | $contextProphecy->popPropertyMetadata()->shouldBeCalled(); |
||
86 | |||
87 | $embeddeds = array( |
||
88 | new Embedded('friend', array('name' => 'John')), |
||
89 | new Embedded('foo', array('name' => 'Bar')), |
||
90 | new Embedded('foo', array('name' => 'Baz')), |
||
91 | new Embedded('bar', array('name' => 'Foo')), |
||
92 | new Embedded('bar', array('name' => 'Baz')), |
||
93 | new Embedded('bar', array('name' => 'Buzz')), |
||
94 | ); |
||
95 | |||
96 | $expectedEmbeddedded = array( |
||
97 | 'friend' => array('name' => 'John'), |
||
98 | 'foo' => array( |
||
99 | array('name' => 'Bar'), |
||
100 | array('name' => 'Baz'), |
||
101 | ), |
||
102 | 'bar' => array( |
||
103 | array('name' => 'Foo'), |
||
104 | array('name' => 'Baz'), |
||
105 | array('name' => 'Buzz'), |
||
106 | ), |
||
107 | ); |
||
108 | |||
109 | $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor'); |
||
110 | $jsonSerializationVisitorProphecy |
||
111 | ->addData('_embedded', $expectedEmbeddedded) |
||
112 | ->shouldBeCalledTimes(1) |
||
113 | ; |
||
114 | |||
115 | $jsonHalSerializer = new JsonHalSerializer(); |
||
116 | $jsonHalSerializer->serializeEmbeddeds( |
||
117 | $embeddeds, |
||
118 | $jsonSerializationVisitorProphecy->reveal(), |
||
119 | $contextProphecy->reveal() |
||
120 | ); |
||
121 | } |
||
122 | |||
123 | public function testSerializeCuriesWithOneLinkShouldBeAnArray() |
||
157 | |||
158 | public function testSerializeCuriesWithMultipleEntriesShouldBeAnArray() |
||
197 | |||
198 | public function testSerializeAdrienBrault() |
||
199 | { |
||
244 | |||
245 | public function testSerializeInlineJson() |
||
280 | |||
281 | View Code Duplication | public function testGh236() |
|
312 | } |
||
313 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.