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 |
||
12 | class ViewCollection implements |
||
13 | ContainerInjectableInterface |
||
14 | { |
||
15 | use ContainerInjectableTrait; |
||
16 | |||
17 | |||
18 | |||
19 | /** |
||
20 | * @var array $views container for all views. |
||
21 | */ |
||
22 | private $views = []; |
||
23 | |||
24 | |||
25 | |||
26 | /** |
||
27 | * @var array $paths where to look for template files. |
||
28 | * @var string $suffix add to each template file name. |
||
29 | */ |
||
30 | private $paths = []; |
||
31 | private $suffix = ".php"; |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * Set paths to search through when looking for template files. |
||
37 | * |
||
38 | * @param array $paths with directories to search through. |
||
39 | * |
||
40 | * @return self |
||
41 | */ |
||
42 | 1 | public function setPaths(array $paths) : object |
|
52 | |||
53 | |||
54 | |||
55 | /** |
||
56 | * Set suffix to add last to template file givven, as a filename extension. |
||
57 | * |
||
58 | * @param string $suffix to use as file extension. |
||
59 | * |
||
60 | * @return self |
||
61 | */ |
||
62 | 1 | public function setSuffix(string $suffix) : object |
|
67 | |||
68 | |||
69 | |||
70 | /** |
||
71 | * Convert template to path to template file and check that it exists. |
||
72 | * |
||
73 | * @param string $template the name of the template file to include |
||
74 | * |
||
75 | * @throws Anax\View\Exception when template file is missing |
||
76 | * |
||
77 | * @return string as path to the template file |
||
78 | */ |
||
79 | public function getTemplateFile($template) |
||
95 | |||
96 | |||
97 | |||
98 | /** |
||
99 | * Add (create) a view to be included, pass optional data and put the |
||
100 | * view in an optional specific region (default region is "main") and |
||
101 | * pass an optional sort value where the highest value is rendered first. |
||
102 | * The $template can be a: |
||
103 | * filename (string), |
||
104 | * callback (array with key callback set to a callable array), |
||
105 | * view array (key value array with template, data, region, sort) |
||
106 | * |
||
107 | * @param array|string $template the name of the template file to include. |
||
108 | * @param array $data variables to make available to the view, |
||
109 | * default is empty. |
||
110 | * @param string $region which region to attach the view, default |
||
111 | * is "main". |
||
112 | * @param integer $sort which order to display the views. |
||
113 | * |
||
114 | * @return self for chaining. |
||
115 | */ |
||
116 | public function add( |
||
151 | |||
152 | |||
153 | |||
154 | /** |
||
155 | * Add a callback to be rendered as a view. |
||
156 | * |
||
157 | * @param string $callback function to call to get the content of the view |
||
158 | * @param array $data variables to make available to the view, default is empty |
||
159 | * @param string $region which region to attach the view |
||
160 | * @param int $sort which order to display the views |
||
161 | * |
||
162 | * @return $this |
||
163 | */ |
||
164 | View Code Duplication | public function addCallback($callback, $data = [], $region = "main", $sort = 0) |
|
172 | |||
173 | |||
174 | |||
175 | /** |
||
176 | * Add a string as a view. |
||
177 | * |
||
178 | * @param string $content the content |
||
179 | * @param string $region which region to attach the view |
||
180 | * @param int $sort which order to display the views |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | View Code Duplication | public function addString($content, $region = "main", $sort = 0) |
|
192 | |||
193 | |||
194 | |||
195 | /** |
||
196 | * Check if a region has views to render. |
||
197 | * |
||
198 | * @param string $region which region to check |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function hasContent($region) |
||
206 | |||
207 | |||
208 | |||
209 | /** |
||
210 | * Render all views for a specific region. |
||
211 | * |
||
212 | * @param string $region which region to use |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | public function render($region = "main") |
||
237 | |||
238 | |||
239 | /** |
||
240 | * Render all views for a specific region and buffer the result. |
||
241 | * |
||
242 | * @param string $region which region to use. |
||
243 | * |
||
244 | * @return string with the buffered results. |
||
245 | */ |
||
246 | public function renderBuffered($region = "main") |
||
254 | } |
||
255 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: