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. |
||
| 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) |
||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Add (create) a view to be included, pass optional data and put the |
||
| 95 | * view in an optional specific region (default region is "main") and |
||
| 96 | * pass an optional sort value where the highest value is rendered first. |
||
| 97 | * The $template can be a: |
||
| 98 | * filename (string), |
||
| 99 | * callback (array with key callback set to a callable array), |
||
| 100 | * view array (key value array with template, data, region, sort) |
||
| 101 | * |
||
| 102 | * @param array|string $template the name of the template file to include. |
||
| 103 | * @param array $data variables to make available to the view, |
||
| 104 | * default is empty. |
||
| 105 | * @param string $region which region to attach the view, default |
||
| 106 | * is "main". |
||
| 107 | * @param integer $sort which order to display the views. |
||
| 108 | * |
||
| 109 | * @return self for chaining. |
||
| 110 | */ |
||
| 111 | public function add( |
||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Add a callback to be rendered as a view. |
||
| 154 | * |
||
| 155 | * @param string $callback function to call to get the content of the view |
||
| 156 | * @param array $data variables to make available to the view, default is empty |
||
| 157 | * @param string $region which region to attach the view |
||
| 158 | * @param int $sort which order to display the views |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function addCallback($callback, $data = [], $region = "main", $sort = 0) |
|
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Add a string as a view. |
||
| 175 | * |
||
| 176 | * @param string $content the content |
||
| 177 | * @param string $region which region to attach the view |
||
| 178 | * @param int $sort which order to display the views |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | View Code Duplication | public function addString($content, $region = "main", $sort = 0) |
|
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Check if a region has views to render. |
||
| 195 | * |
||
| 196 | * @param string $region which region to check |
||
| 197 | * |
||
| 198 | * @return $this |
||
| 199 | */ |
||
| 200 | public function hasContent($region) |
||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * Render all views for a specific region. |
||
| 209 | * |
||
| 210 | * @param string $region which region to use |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | public function render($region = "main") |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Render all views for a specific region and buffer the result. |
||
| 239 | * |
||
| 240 | * @param string $region which region to use. |
||
| 241 | * |
||
| 242 | * @return string with the buffered results. |
||
| 243 | */ |
||
| 244 | public function renderBuffered($region = "main") |
||
| 252 | } |
||
| 253 |
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: