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 DataItem extends ReadableDocument implements CollectableItem, TemplateReadyDocument, PermalinkDocument |
||
16 | { |
||
17 | use CollectableItemTrait; |
||
18 | use PermalinkDocumentTrait; |
||
19 | |||
20 | /** @var DataTransformerInterface */ |
||
21 | protected $dataTransformer; |
||
22 | |||
23 | /** @var array */ |
||
24 | protected $frontMatter; |
||
25 | |||
26 | /** @var array */ |
||
27 | protected $data; |
||
28 | |||
29 | /** |
||
30 | * DataItem constructor. |
||
31 | */ |
||
32 | 7 | public function __construct(File $file) |
|
38 | |||
39 | /** |
||
40 | * Set the transformer used to convert the file contents into an array,. |
||
41 | */ |
||
42 | 7 | public function setDataTransformer(DataTransformerManager $manager) |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function evaluateFrontMatter(array $variables = [], array $complexVariables = []) |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function buildPermalink($force = false) |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 6 | public function readContents($mixed) |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 4 | public function getContent() |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 2 | public function isDraft() |
|
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 4 | public function createJail() |
|
128 | |||
129 | /// |
||
130 | // JsonSerializable implementation |
||
131 | /// |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function jsonSerialize() |
||
140 | |||
141 | /// |
||
142 | // IteratorAggregate implementation |
||
143 | /// |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 1 | public function getIterator() |
|
152 | |||
153 | /// |
||
154 | // ArrayAccess implementation |
||
155 | /// |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | public function offsetExists($offset) |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | 2 | View Code Duplication | public function offsetGet($offset) |
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function offsetSet($offset, $value) |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function offsetUnset($offset) |
||
205 | } |
||
206 |
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.