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 |
||
15 | class YoutubeServiceAdapter extends AbstractServiceAdapter |
||
16 | { |
||
17 | const THUMBNAIL_DEFAULT = 'default'; |
||
|
|||
18 | const THUMBNAIL_STANDARD_DEFINITION = 'sddefault'; |
||
19 | const THUMBNAIL_MEDIUM_QUALITY = 'mqdefault'; |
||
20 | const THUMBNAIL_HIGH_QUALITY = 'hqdefault'; |
||
21 | const THUMBNAIL_MAX_QUALITY = 'maxresdefault'; |
||
22 | |||
23 | /** |
||
24 | * @param string $url |
||
25 | * @param string $pattern |
||
26 | * @param EmbedRendererInterface $renderer |
||
27 | */ |
||
28 | 14 | public function __construct($url, $pattern, EmbedRendererInterface $renderer) |
|
41 | |||
42 | /** |
||
43 | * Returns the service name (ie: "Youtube" or "Vimeo"). |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 1 | public function getServiceName() |
|
51 | |||
52 | /** |
||
53 | * Returns if the service has a thumbnail image. |
||
54 | * |
||
55 | * @return bool |
||
56 | */ |
||
57 | 1 | public function hasThumbnail() |
|
61 | |||
62 | /** |
||
63 | * @param string $size |
||
64 | * @param bool $forceSecure |
||
65 | * @return string |
||
66 | * @throws InvalidThumbnailSizeException |
||
67 | */ |
||
68 | 2 | View Code Duplication | public function getThumbnail($size, $forceSecure = false) |
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | 3 | public function getThumbNailSizes() |
|
90 | |||
91 | /** |
||
92 | * @param bool $forceAutoplay |
||
93 | * @param bool $forceSecure |
||
94 | * @return string |
||
95 | */ |
||
96 | 2 | public function getEmbedUrl($forceAutoplay = false, $forceSecure = false) |
|
107 | |||
108 | /** |
||
109 | * Returns the small thumbnail's url. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 1 | public function getSmallThumbnail($forceSecure = false) |
|
117 | |||
118 | /** |
||
119 | * Returns the medium thumbnail's url. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | 1 | public function getMediumThumbnail($forceSecure = false) |
|
127 | |||
128 | /** |
||
129 | * Returns the large thumbnail's url. |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | 1 | public function getLargeThumbnail($forceSecure = false) |
|
137 | |||
138 | /** |
||
139 | * Returns the largest thumnbnaail's url. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | 1 | public function getLargestThumbnail($forceSecure = false) |
|
147 | |||
148 | /** |
||
149 | * @return bool |
||
150 | */ |
||
151 | 1 | public function isEmbeddable() |
|
155 | |||
156 | /** |
||
157 | * @param bool $forceAutoplay |
||
158 | * @return array |
||
159 | */ |
||
160 | 2 | private function generateQuerystring($forceAutoplay = false) |
|
171 | } |
||
172 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.