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 | class CViewContainer implements \Anax\DI\IInjectionAware |
||
| 10 | { |
||
| 11 | use \Anax\TConfigure, |
||
| 12 | \Anax\DI\TInjectionAware; |
||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | /** |
||
| 17 | * Properties |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | private $views = []; // Array for all views |
||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | /** |
||
| 25 | * Convert template to path to template file. |
||
| 26 | * |
||
| 27 | * @param string $template the name of the template file to include |
||
| 28 | * |
||
| 29 | * @throws Anax\View\Exception when template file is missing |
||
| 30 | * |
||
| 31 | * @return string as path to the template file |
||
| 32 | */ |
||
| 33 | public function getTemplateFile($template) |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Add a view to be included as a template file. |
||
| 52 | * |
||
| 53 | * @param string $template the name of the template file to include |
||
| 54 | * @param array $data variables to make available to the view, default is empty |
||
| 55 | * @param string $region which region to attach the view |
||
| 56 | * @param int $sort which order to display the views |
||
| 57 | * |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | public function add($template, $data = [], $region = "main", $sort = 0) |
||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Add a callback to be rendered as a view. |
||
| 102 | * |
||
| 103 | * @param string $callback function to call to get the content of the view |
||
| 104 | * @param array $data variables to make available to the view, default is empty |
||
| 105 | * @param string $region which region to attach the view |
||
| 106 | * @param int $sort which order to display the views |
||
| 107 | * |
||
| 108 | * @return $this |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function addCallback($callback, $data = [], $region = "main", $sort = 0) |
|
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Add a string as a view. |
||
| 124 | * |
||
| 125 | * @param string $content the content |
||
| 126 | * @param string $region which region to attach the view |
||
| 127 | * @param int $sort which order to display the views |
||
| 128 | * |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function addString($content, $region = "main", $sort = 0) |
|
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Check if a region has views to render. |
||
| 145 | * |
||
| 146 | * @param string $region which region to check |
||
| 147 | * |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | public function hasContent($region) |
||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Render all views for a specific region. |
||
| 159 | * |
||
| 160 | * @param string $region which region to use |
||
| 161 | * |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function render($region = "main") |
||
| 187 | } |
||
| 188 |
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: