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 |
||
9 | class PaginatedRepresentationTest extends RepresentationTestCase |
||
10 | { |
||
11 | View Code Duplication | public function testSerialize() |
|
|
|||
12 | { |
||
13 | $collection = new PaginatedRepresentation( |
||
14 | new CollectionRepresentation( |
||
15 | array( |
||
16 | 'Adrien', |
||
17 | 'William', |
||
18 | ), |
||
19 | 'authors', |
||
20 | 'users' |
||
21 | ), |
||
22 | '/authors', |
||
23 | array( |
||
24 | 'query' => 'willdurand/Hateoas', |
||
25 | ), |
||
26 | 3, |
||
27 | 20, |
||
28 | 17, |
||
29 | null, |
||
30 | null, |
||
31 | false, |
||
32 | 100 |
||
33 | ); |
||
34 | |||
35 | $this->assertSame( |
||
36 | <<<XML |
||
37 | <?xml version="1.0" encoding="UTF-8"?> |
||
38 | <collection page="3" limit="20" pages="17" total="100"> |
||
39 | <users rel="authors"> |
||
40 | <entry><![CDATA[Adrien]]></entry> |
||
41 | <entry><![CDATA[William]]></entry> |
||
42 | </users> |
||
43 | <link rel="self" href="/authors?query=willdurand%2FHateoas&page=3&limit=20"/> |
||
44 | <link rel="first" href="/authors?query=willdurand%2FHateoas&page=1&limit=20"/> |
||
45 | <link rel="last" href="/authors?query=willdurand%2FHateoas&page=17&limit=20"/> |
||
46 | <link rel="next" href="/authors?query=willdurand%2FHateoas&page=4&limit=20"/> |
||
47 | <link rel="previous" href="/authors?query=willdurand%2FHateoas&page=2&limit=20"/> |
||
48 | </collection> |
||
49 | |||
50 | XML |
||
51 | , |
||
52 | $this->hateoas->serialize($collection, 'xml') |
||
53 | ); |
||
54 | $this->assertSame( |
||
55 | <<<XML |
||
56 | <?xml version="1.0" encoding="UTF-8"?> |
||
57 | <collection page="3" limit="20" pages="17" total="100" href="/authors?query=willdurand%2FHateoas&page=3&limit=20"> |
||
58 | <resource rel="authors"><![CDATA[Adrien]]></resource> |
||
59 | <resource rel="authors"><![CDATA[William]]></resource> |
||
60 | <link rel="first" href="/authors?query=willdurand%2FHateoas&page=1&limit=20"/> |
||
61 | <link rel="last" href="/authors?query=willdurand%2FHateoas&page=17&limit=20"/> |
||
62 | <link rel="next" href="/authors?query=willdurand%2FHateoas&page=4&limit=20"/> |
||
63 | <link rel="previous" href="/authors?query=willdurand%2FHateoas&page=2&limit=20"/> |
||
64 | </collection> |
||
65 | |||
66 | XML |
||
67 | , |
||
68 | $this->halHateoas->serialize($collection, 'xml') |
||
69 | ); |
||
70 | $this->assertSame( |
||
71 | <<<XML |
||
72 | <?xml version="1.0" encoding="UTF-8"?> |
||
73 | <users page="3" limit="20" pages="17" total="100"> |
||
74 | <users rel="authors"> |
||
75 | <entry><![CDATA[Adrien]]></entry> |
||
76 | <entry><![CDATA[William]]></entry> |
||
77 | </users> |
||
78 | <link rel="self" href="/authors?query=willdurand%2FHateoas&page=3&limit=20"/> |
||
79 | <link rel="first" href="/authors?query=willdurand%2FHateoas&page=1&limit=20"/> |
||
80 | <link rel="last" href="/authors?query=willdurand%2FHateoas&page=17&limit=20"/> |
||
81 | <link rel="next" href="/authors?query=willdurand%2FHateoas&page=4&limit=20"/> |
||
82 | <link rel="previous" href="/authors?query=willdurand%2FHateoas&page=2&limit=20"/> |
||
83 | </users> |
||
84 | |||
85 | XML |
||
86 | , |
||
87 | $this->hateoas->serialize(new UsersRepresentation($collection), 'xml') |
||
88 | ); |
||
89 | $this->assertSame( |
||
90 | <<<XML |
||
91 | <?xml version="1.0" encoding="UTF-8"?> |
||
92 | <users page="3" limit="20" pages="17" total="100" href="/authors?query=willdurand%2FHateoas&page=3&limit=20"> |
||
93 | <resource rel="authors"><![CDATA[Adrien]]></resource> |
||
94 | <resource rel="authors"><![CDATA[William]]></resource> |
||
95 | <link rel="first" href="/authors?query=willdurand%2FHateoas&page=1&limit=20"/> |
||
96 | <link rel="last" href="/authors?query=willdurand%2FHateoas&page=17&limit=20"/> |
||
97 | <link rel="next" href="/authors?query=willdurand%2FHateoas&page=4&limit=20"/> |
||
98 | <link rel="previous" href="/authors?query=willdurand%2FHateoas&page=2&limit=20"/> |
||
99 | </users> |
||
100 | |||
101 | XML |
||
102 | , |
||
103 | $this->halHateoas->serialize(new UsersRepresentation($collection), 'xml') |
||
104 | ); |
||
105 | $this->assertSame( |
||
106 | '{' |
||
107 | .'"page":3,' |
||
108 | .'"limit":20,' |
||
109 | .'"pages":17,' |
||
110 | .'"total":100,' |
||
111 | .'"_links":{' |
||
112 | .'"self":{' |
||
113 | .'"href":"\/authors?query=willdurand%2FHateoas&page=3&limit=20"' |
||
114 | .'},' |
||
115 | .'"first":{' |
||
116 | .'"href":"\/authors?query=willdurand%2FHateoas&page=1&limit=20"' |
||
117 | .'},' |
||
118 | .'"last":{' |
||
119 | .'"href":"\/authors?query=willdurand%2FHateoas&page=17&limit=20"' |
||
120 | .'},' |
||
121 | .'"next":{' |
||
122 | .'"href":"\/authors?query=willdurand%2FHateoas&page=4&limit=20"' |
||
123 | .'},' |
||
124 | .'"previous":{' |
||
125 | .'"href":"\/authors?query=willdurand%2FHateoas&page=2&limit=20"' |
||
126 | .'}' |
||
127 | .'},' |
||
128 | .'"_embedded":{' |
||
129 | .'"authors":[' |
||
130 | .'"Adrien",' |
||
131 | .'"William"' |
||
132 | .']' |
||
133 | .'}' |
||
134 | .'}', |
||
135 | $this->halHateoas->serialize($collection, 'json') |
||
136 | ); |
||
137 | } |
||
138 | |||
139 | View Code Duplication | public function testGenerateAbsoluteURIs() |
|
229 | } |
||
230 |
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.