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 |
||
17 | trait ContextualTemplateTrait |
||
18 | { |
||
19 | /** |
||
20 | * The current rendering / data context. |
||
21 | * |
||
22 | * @var ModelInterface|null |
||
23 | */ |
||
24 | protected $contextObject; |
||
25 | |||
26 | /** |
||
27 | * The route group path (base URI). |
||
28 | * |
||
29 | * @var string|null |
||
30 | */ |
||
31 | protected $routeGroup; |
||
32 | |||
33 | /** |
||
34 | * The route endpoint path (path URI). |
||
35 | * |
||
36 | * @var string|null |
||
37 | */ |
||
38 | protected $routeEndpoint; |
||
39 | |||
40 | /** |
||
41 | * Track the state of context creation. |
||
42 | * |
||
43 | * @var boolean |
||
44 | */ |
||
45 | protected $isCreatingContext = false; |
||
46 | |||
47 | /** |
||
48 | * The class name of the section model. |
||
49 | * |
||
50 | * A fully-qualified PHP namespace. Used for the model factory. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $genericContextClass = Model::class; |
||
55 | |||
56 | /** |
||
57 | * Set the class name of the generic context model. |
||
58 | * |
||
59 | * @param string $className The class name of the section model. |
||
60 | * @throws InvalidArgumentException If the class name is not a string. |
||
61 | * @return AbstractPropertyDisplay Chainable |
||
62 | */ |
||
63 | public function setGenericContextClass($className) |
||
75 | |||
76 | /** |
||
77 | * Retrieve the class name of the generic context model. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function genericContextClass() |
||
85 | |||
86 | /** |
||
87 | * Set the current renderable object relative to the context. |
||
88 | * |
||
89 | * @param ModelInterface $context The context / view to render the template with. |
||
90 | * @return self |
||
91 | */ |
||
92 | public function setContextObject(ModelInterface $context) |
||
98 | |||
99 | /** |
||
100 | * Retrieve the current object relative to the context. |
||
101 | * |
||
102 | * This method is meant to be reimplemented in a child template controller |
||
103 | * to return the resolved object that the module considers "the context". |
||
104 | * |
||
105 | * @return ModelInterface|null |
||
106 | */ |
||
107 | public function contextObject() |
||
115 | |||
116 | /** |
||
117 | * Create a generic object relative to the context. |
||
118 | * |
||
119 | * @return ModelInterface|null |
||
120 | */ |
||
121 | protected function createGenericContext() |
||
158 | |||
159 | /** |
||
160 | * Retrieve the current URI of the context. |
||
161 | * |
||
162 | * @return UriInterface|string|null |
||
163 | */ |
||
164 | View Code Duplication | public function currentUrl() |
|
174 | |||
175 | /** |
||
176 | * Append a path to the base URI. |
||
177 | * |
||
178 | * @param string $path The base path. |
||
179 | * @return self |
||
180 | */ |
||
181 | public function setRouteGroup($path) |
||
193 | |||
194 | /** |
||
195 | * Append a path to the URI. |
||
196 | * |
||
197 | * @param string $path The main path. |
||
198 | * @return self |
||
199 | */ |
||
200 | public function setRouteEndpoint($path) |
||
212 | |||
213 | /** |
||
214 | * Retrieve the base URI of the project. |
||
215 | * |
||
216 | * @return UriInterface|null |
||
217 | */ |
||
218 | abstract public function baseUrl(); |
||
219 | |||
220 | /** |
||
221 | * Retrieve the title of the page (from the context). |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | abstract public function title(); |
||
226 | |||
227 | /** |
||
228 | * Retrieve the translator service. |
||
229 | * |
||
230 | * @see \Charcoal\Translator\TranslatorAwareTrait |
||
231 | * @return \Charcoal\Translator\Translator |
||
232 | */ |
||
233 | abstract protected function translator(); |
||
234 | |||
235 | /** |
||
236 | * Retrieve the object model factory. |
||
237 | * |
||
238 | * @return \Charcoal\Factory\FactoryInterface |
||
239 | */ |
||
240 | abstract public function modelFactory(); |
||
241 | } |
||
242 |
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.