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 |
||
29 | class Page |
||
30 | { |
||
31 | /** |
||
32 | * @var Meta |
||
33 | */ |
||
34 | public $meta; |
||
35 | |||
36 | /** |
||
37 | * The page's ID. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $id; |
||
42 | |||
43 | /** |
||
44 | * The template path of this page. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $templatePath; |
||
49 | |||
50 | /** |
||
51 | * The variables of this page, which will be available in the rendered template. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $variables = []; |
||
56 | |||
57 | /** |
||
58 | * The adapters of this page. |
||
59 | * Adapters will transform a page's variables and/or the page itself into one or more pages. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $adapters; |
||
64 | |||
65 | /** |
||
66 | * An array containing a list of parsed variables. |
||
67 | * |
||
68 | * @see setVariableIsParsed |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $parsedVariables = []; |
||
73 | |||
74 | /** |
||
75 | * @var Header[] |
||
76 | */ |
||
77 | private $headers = []; |
||
78 | |||
79 | /** |
||
80 | * Construct a new page |
||
81 | * |
||
82 | * @param string $id |
||
83 | * @param array $data |
||
84 | * |
||
85 | * @param Meta|null $meta |
||
86 | * |
||
87 | * @throws TemplateNotFoundException |
||
88 | */ |
||
89 | public function __construct($id, array $data = [], Meta $meta = null) { |
||
108 | |||
109 | /** |
||
110 | * Defines a variable as parsed. |
||
111 | * Parsed variables will be ignored by Stitcher when compiling the website. |
||
112 | * Adapters can define parsed variables to indicate Stitcher it should skip parsing that variable during compile |
||
113 | * time. |
||
114 | * |
||
115 | * @param $name |
||
116 | * |
||
117 | * @return Page |
||
118 | * |
||
119 | * @see \Brendt\Stitcher\Stitcher::parseVariables |
||
120 | * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
||
121 | * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
||
122 | */ |
||
123 | public function setVariableIsParsed($name) { |
||
128 | |||
129 | /** |
||
130 | * Check whether a variable is parsed or not. |
||
131 | * Parsed variables will be ignored by Stitcher during compile time. |
||
132 | * |
||
133 | * @param $name |
||
134 | * |
||
135 | * @return bool |
||
136 | * |
||
137 | * @see \Brendt\Stitcher\Stitcher::parseVariables |
||
138 | */ |
||
139 | public function isParsedVariable($name) { |
||
142 | |||
143 | /** |
||
144 | * Get the ID of this page |
||
145 | * |
||
146 | * @return string |
||
147 | * |
||
148 | * @see \Brendt\Stitcher\Stitcher::stitch |
||
149 | */ |
||
150 | public function getId() { |
||
153 | |||
154 | /** |
||
155 | * Get the template path of this page. |
||
156 | * |
||
157 | * @return string |
||
158 | * |
||
159 | * @see \Brendt\Stitcher\Stitcher::stitch |
||
160 | */ |
||
161 | public function getTemplatePath() { |
||
164 | |||
165 | /** |
||
166 | * Get the variables of this page. |
||
167 | * |
||
168 | * @return array |
||
169 | * |
||
170 | * @see \Brendt\Stitcher\Stitcher::stitch |
||
171 | * @see \Brendt\Stitcher\Stitcher::parseVariables |
||
172 | */ |
||
173 | public function getVariables() { |
||
176 | |||
177 | /** |
||
178 | * Get the adapters of this page. |
||
179 | * |
||
180 | * @return array |
||
181 | * |
||
182 | * @see \Brendt\Stitcher\Stitcher::parseAdapters |
||
183 | */ |
||
184 | public function getAdapters() { |
||
187 | |||
188 | /** |
||
189 | * Get an adapter configuration by name. |
||
190 | * |
||
191 | * @param $name |
||
192 | * |
||
193 | * @return array |
||
194 | * |
||
195 | * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
||
196 | * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
||
197 | * @see \Brendt\Stitcher\controller\DevController::run |
||
198 | */ |
||
199 | public function getAdapterConfig($name) { |
||
206 | |||
207 | /** |
||
208 | * Get a variable by name. |
||
209 | * |
||
210 | * @param $name |
||
211 | * |
||
212 | * @return mixed|null |
||
213 | * |
||
214 | * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
||
215 | * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
||
216 | */ |
||
217 | public function getVariable($name) { |
||
224 | |||
225 | /** |
||
226 | * Set the value of a variable. |
||
227 | * |
||
228 | * @param $name |
||
229 | * @param $value |
||
230 | * |
||
231 | * @return Page |
||
232 | * |
||
233 | * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
||
234 | * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
||
235 | * @see \Brendt\Stitcher\Stitcher::parseVariables |
||
236 | */ |
||
237 | public function setVariableValue($name, $value) { |
||
242 | |||
243 | /** |
||
244 | * Remove an adapter. |
||
245 | * |
||
246 | * @param $name |
||
247 | * |
||
248 | * @return Page |
||
249 | * |
||
250 | * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
||
251 | * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
||
252 | */ |
||
253 | public function removeAdapter($name) { |
||
260 | |||
261 | /** |
||
262 | * Set the ID of this page. |
||
263 | * An page's ID can be re-set after constructing when an adapter is creating other pages based on an existing page. |
||
264 | * |
||
265 | * @param string $id |
||
266 | * |
||
267 | * @return Page |
||
268 | * |
||
269 | * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
||
270 | * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
||
271 | */ |
||
272 | public function setId($id) { |
||
277 | |||
278 | /** |
||
279 | * @param Header $header |
||
280 | */ |
||
281 | public function addHeader(Header $header) { |
||
284 | |||
285 | /** |
||
286 | * @return Header[] |
||
287 | */ |
||
288 | public function getHeaders() : array { |
||
291 | |||
292 | /** |
||
293 | * @param array $data |
||
294 | * |
||
295 | * @todo Create separate meta crawlers |
||
296 | */ |
||
297 | public function parseMeta(array $data) { |
||
326 | |||
327 | } |
||
328 |
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.