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 |
||
16 | class DataItem extends PermalinkDocument implements |
||
17 | \ArrayAccess, |
||
18 | \IteratorAggregate, |
||
19 | TwigDocumentInterface, |
||
20 | JailedDocumentInterface |
||
21 | { |
||
22 | protected $data; |
||
23 | |||
24 | private $namespace; |
||
25 | private $pageView; |
||
26 | |||
27 | public function __construct($filePath) |
||
33 | |||
34 | public function getIterator() |
||
38 | |||
39 | public function getData() |
||
43 | |||
44 | public function getName() |
||
48 | |||
49 | public function evaluateFrontMatter($variables = array()) |
||
76 | |||
77 | protected function buildPermalink() |
||
81 | |||
82 | /// |
||
83 | // Jailed Document implementation |
||
84 | /// |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function createJail() |
||
95 | |||
96 | /// |
||
97 | // ArrayAccess implementation |
||
98 | /// |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function offsetExists($offset) |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function offsetGet($offset) |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function offsetSet($offset, $value) |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function offsetUnset($offset) |
||
131 | |||
132 | /// |
||
133 | // Twig Document implementation |
||
134 | /// |
||
135 | |||
136 | public function getNamespace() |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function setNamespace($namespace) |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function setPageView(&$pageView) |
||
156 | |||
157 | public function isDraft() |
||
161 | |||
162 | public function refreshFileContent() |
||
183 | |||
184 | /// |
||
185 | // File parsing helpers |
||
186 | /// |
||
187 | |||
188 | /** |
||
189 | * Convert from CSV into an associative array. |
||
190 | * |
||
191 | * @param string $content CSV formatted text |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | private function fromCsv($content) |
||
208 | |||
209 | /** |
||
210 | * Convert from JSON into an associative array. |
||
211 | * |
||
212 | * @param string $content JSON formatted text |
||
213 | * |
||
214 | * @return array |
||
215 | */ |
||
216 | private function fromJson($content) |
||
220 | |||
221 | /** |
||
222 | * Convert from XML into an associative array. |
||
223 | * |
||
224 | * @param string $content XML formatted text |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | private function fromXml($content) |
||
232 | |||
233 | /** |
||
234 | * Convert from YAML into an associative array. |
||
235 | * |
||
236 | * @param string $content YAML formatted text |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | private function fromYaml($content) |
||
244 | |||
245 | /** |
||
246 | * An alias for handling `*.yml` files. |
||
247 | * |
||
248 | * @param string $content YAML formatted text |
||
249 | * |
||
250 | * @return array |
||
251 | */ |
||
252 | private function fromYml($content) |
||
256 | |||
257 | /** |
||
258 | * @param string $extension |
||
259 | * |
||
260 | * @todo 0.1.0 Create a help page on the main stakx website for this topic and link to it |
||
261 | * |
||
262 | * @throws DependencyMissingException |
||
263 | */ |
||
264 | private function handleDependencies($extension) |
||
271 | } |
||
272 |
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.