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 |
||
9 | trait ViewHelperTrait |
||
10 | { |
||
11 | /** |
||
12 | * Shortcut to create an url for a static asset. |
||
13 | * |
||
14 | * @param string $url url to use when creating the url. |
||
15 | * |
||
16 | * @return string as resulting url. |
||
17 | */ |
||
18 | public function asset($url = "") |
||
22 | |||
23 | |||
24 | |||
25 | /** |
||
26 | * Shortcut to create an url for routing in the framework. |
||
27 | * |
||
28 | * @param null|string $url url to use when creating the url. |
||
29 | * |
||
30 | * @return string as resulting url. |
||
31 | */ |
||
32 | public function url($url = "") |
||
36 | |||
37 | |||
38 | |||
39 | /** |
||
40 | * Render a view with an optional data set of variables. |
||
41 | * |
||
42 | * @param string $template the template file, or array |
||
43 | * @param array $data variables to make available to the |
||
44 | * view, default is empty |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | public function renderView($template, $data = []) |
||
55 | |||
56 | |||
57 | |||
58 | /** |
||
59 | * Check if the region in the view container has views to render. |
||
60 | * |
||
61 | * @param string $region to check |
||
62 | * |
||
63 | * @return boolean true or false |
||
64 | */ |
||
65 | public function regionHasContent($region) |
||
69 | |||
70 | |||
71 | |||
72 | /** |
||
73 | * Render views, from the view container, in the region. |
||
74 | * |
||
75 | * @param string $region to render in |
||
76 | * |
||
77 | * @return boolean true or false |
||
78 | */ |
||
79 | public function renderRegion($region) |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * Create a class attribute from a string or array. |
||
88 | * |
||
89 | * @param string|array $args variable amount of classlists. |
||
90 | * |
||
91 | * @return string as complete class attribute |
||
92 | */ |
||
93 | 5 | View Code Duplication | public function classList(...$args) |
108 | |||
109 | |||
110 | |||
111 | /** |
||
112 | * Get current url, without querystring. |
||
113 | * |
||
114 | * @return string as resulting url. |
||
115 | */ |
||
116 | public function currentUrl() |
||
120 | |||
121 | |||
122 | |||
123 | /** |
||
124 | * Get current route. |
||
125 | * |
||
126 | * @return string as resulting route. |
||
127 | */ |
||
128 | public function currentRoute() |
||
132 | |||
133 | |||
134 | |||
135 | /** |
||
136 | * Load content from a route and return details to view. |
||
137 | * |
||
138 | * @param string $route to load content from. |
||
139 | * |
||
140 | * @return array with values to extract in view. |
||
141 | */ |
||
142 | // public function getContentForRoute($route) |
||
143 | // { |
||
144 | // $content = $this->di->get("content")->contentForInternalRoute($route); |
||
145 | // return $content->views["main"]["data"]; |
||
146 | // } |
||
147 | |||
148 | |||
149 | |||
150 | /** |
||
151 | * Wrap a HTML element with start and end. |
||
152 | * |
||
153 | * @param string $text with content |
||
154 | * @param string $tag HTML tag to search for |
||
155 | * @param string $start wrap start part |
||
156 | * @param string $end wrap end part |
||
157 | * @param number $count hits to search for |
||
158 | * |
||
159 | * @return array with values to extract in view. |
||
160 | */ |
||
161 | // public function wrapElementWithStartEnd($text, $tag, $start, $end, $count) |
||
162 | // { |
||
163 | // return $this->di->get("textFilter")->wrapElementWithStartEnd($text, $tag, $start, $end, $count); |
||
164 | // } |
||
165 | |||
166 | |||
167 | |||
168 | /** |
||
169 | * Wrap content of a HTML element with start and end. |
||
170 | * |
||
171 | * @param string $text with content |
||
172 | * @param string $tag HTML tag to search for |
||
173 | * @param string $start wrap start part |
||
174 | * @param string $end wrap end part |
||
175 | * @param number $count hits to search for |
||
176 | * |
||
177 | * @return array with values to extract in view. |
||
178 | */ |
||
179 | // public function wrapElementContentWithStartEnd($text, $tag, $start, $end, $count) |
||
180 | // { |
||
181 | // return $this->di->get("textFilter")->wrapElementContentWithStartEnd($text, $tag, $start, $end, $count); |
||
182 | // } |
||
183 | |||
184 | |||
185 | |||
186 | /** |
||
187 | * Extrat the publish or update date for the article. |
||
188 | * |
||
189 | * @param array $dates a collection of possible date values. |
||
190 | * |
||
191 | * @return array with values for showing the date. |
||
192 | */ |
||
193 | // public function getPublishedDate($dates) |
||
194 | // { |
||
195 | // $defaults = [ |
||
196 | // "revision" => [], |
||
197 | // "published" => null, |
||
198 | // "updated" => null, |
||
199 | // "created" => null, |
||
200 | // ]; |
||
201 | // $dates = array_merge($defaults, $dates); |
||
202 | // |
||
203 | // if ($dates["revision"]) { |
||
204 | // return [t("Latest revision"), key($dates["revision"])]; |
||
205 | // } elseif ($dates["published"]) { |
||
206 | // return [t("Published"), $dates["published"]]; |
||
207 | // } elseif ($dates["updated"]) { |
||
208 | // return [t("Updated"), $dates["updated"]]; |
||
209 | // } elseif ($dates["created"]) { |
||
210 | // return [t("Created"), $dates["created"]]; |
||
211 | // } |
||
212 | // |
||
213 | // return [t("Missing pubdate."), null]; |
||
214 | // } |
||
215 | } |
||
216 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: