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 | RepeatableItem, |
||
18 | TrackableDocument, |
||
19 | TwigDocument |
||
20 | { |
||
21 | protected $data; |
||
22 | |||
23 | private $namespace; |
||
24 | private $pageView; |
||
25 | |||
26 | 8 | public function __construct($filePath) |
|
27 | { |
||
28 | 8 | $this->namespace = ''; |
|
29 | |||
30 | 8 | parent::__construct($filePath); |
|
31 | 6 | } |
|
32 | |||
33 | 4 | public function getData() |
|
34 | { |
||
35 | 4 | return $this->data; |
|
36 | } |
||
37 | |||
38 | 4 | public function getObjectName() |
|
39 | { |
||
40 | 4 | return $this->getBaseName(); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function evaluateFrontMatter($variables = array()) |
||
47 | { |
||
48 | $workspace = array_merge($this->data, $variables); |
||
49 | $parser = new Parser($workspace, array( |
||
50 | 'filename' => $this->getFileName(), |
||
51 | 'basename' => $this->getBaseName(), |
||
52 | )); |
||
53 | |||
54 | if (!is_null($parser) && $parser->hasExpansion()) |
||
55 | { |
||
56 | throw new \LogicException('The permalink for this item has not been set.'); |
||
57 | } |
||
58 | |||
59 | $permalink = $workspace['permalink']; |
||
60 | |||
61 | View Code Duplication | if (is_array($permalink)) |
|
|
|||
62 | { |
||
63 | $this->permalink = $permalink[0]; |
||
64 | array_shift($permalink); |
||
65 | $this->redirects = $permalink; |
||
66 | } |
||
67 | else |
||
68 | { |
||
69 | $this->permalink = $permalink; |
||
70 | $this->redirects = array(); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | protected function buildPermalink() |
||
81 | |||
82 | /// |
||
83 | // Twig Document implementation |
||
84 | /// |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 2 | public function getNamespace() |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 2 | public function setNamespace($namespace) |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | public function setParentPageView(PageView &$pageView) |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 2 | public function isDraft() |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 8 | public function refreshFileContent() |
|
143 | |||
144 | /// |
||
145 | // File parsing helpers |
||
146 | /// |
||
147 | |||
148 | /** |
||
149 | * Convert from CSV into an associative array. |
||
150 | * |
||
151 | * @param string $content CSV formatted text |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | 1 | private function fromCsv($content) |
|
168 | |||
169 | /** |
||
170 | * Convert from JSON into an associative array. |
||
171 | * |
||
172 | * @param string $content JSON formatted text |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | 1 | private function fromJson($content) |
|
180 | |||
181 | /** |
||
182 | * Convert from XML into an associative array. |
||
183 | * |
||
184 | * @param string $content XML formatted text |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | 1 | private function fromXml($content) |
|
192 | |||
193 | /** |
||
194 | * Convert from YAML into an associative array. |
||
195 | * |
||
196 | * @param string $content YAML formatted text |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 3 | private function fromYaml($content) |
|
204 | |||
205 | /** |
||
206 | * An alias for handling `*.yml` files. |
||
207 | * |
||
208 | * @param string $content YAML formatted text |
||
209 | * |
||
210 | * @return array |
||
211 | */ |
||
212 | 3 | private function fromYml($content) |
|
216 | |||
217 | /** |
||
218 | * Check for any dependencies needed to parse for a specific file extension |
||
219 | * |
||
220 | * @param string $extension |
||
221 | * |
||
222 | * @todo 0.1.0 Create a help page on the main stakx website for this topic and link to it |
||
223 | * |
||
224 | * @throws DependencyMissingException |
||
225 | */ |
||
226 | 6 | private function handleDependencies($extension) |
|
233 | |||
234 | /// |
||
235 | // Jailed Document implementation |
||
236 | /// |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | 4 | public function createJail() |
|
249 | |||
250 | /// |
||
251 | // IteratorAggregate implementation |
||
252 | /// |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | 1 | public function getIterator() |
|
261 | |||
262 | /// |
||
263 | // ArrayAccess implementation |
||
264 | /// |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function offsetExists($offset) |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | 2 | public function offsetGet($offset) |
|
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | public function offsetSet($offset, $value) |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | public function offsetUnset($offset) |
||
297 | } |
||
298 |
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.