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 CViewContainerBasic implements \Anax\DI\IInjectionAware |
||
| 10 | { |
||
| 11 | use \Anax\DI\TInjectionAware; |
||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | /** |
||
| 16 | * Properties |
||
| 17 | * |
||
| 18 | */ |
||
| 19 | private $views = []; // Array for all views |
||
| 20 | private $suffix; // Template file suffix |
||
| 21 | private $path; // Base path for views |
||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * Add a view to be included as a template file. |
||
| 27 | * |
||
| 28 | * @param string $template the name of the template file to include |
||
| 29 | * @param array $data variables to make available to the view, default is empty |
||
| 30 | * @param string $region which region to attach the view |
||
| 31 | * @param int $sort which order to display the views |
||
| 32 | * |
||
| 33 | * @return $this |
||
| 34 | */ |
||
| 35 | public function add($template, $data = [], $region = 'main', $sort = 0) |
||
| 36 | { |
||
| 37 | $view = $this->di->get('view'); |
||
| 38 | |||
| 39 | if (is_string($template)) { |
||
| 40 | $tpl = $this->path . $template . $this->suffix; |
||
| 41 | $type = 'file'; |
||
| 42 | } elseif (is_array($template)) { |
||
| 43 | $tpl = $template; |
||
| 44 | $type = 'callback'; |
||
| 45 | } |
||
| 46 | |||
| 47 | $view->set($tpl, $data, $sort, $type); |
||
| 48 | $view->setDI($this->di); |
||
| 49 | $this->views[$region][] = $view; |
||
| 50 | |||
| 51 | return $this; |
||
| 52 | } |
||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Add a callback to be rendered as a view. |
||
| 58 | * |
||
| 59 | * @param string $callback function to call to get the content of the view |
||
| 60 | * @param array $data variables to make available to the view, default is empty |
||
| 61 | * @param string $region which region to attach the view |
||
| 62 | * @param int $sort which order to display the views |
||
| 63 | * |
||
| 64 | * @return $this |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function addCallback($callback, $data = [], $region = 'main', $sort = 0) |
|
| 67 | { |
||
| 68 | $view = $this->di->get('view'); |
||
| 69 | |||
| 70 | $view->set(['callback' => $callback], $data, $sort, 'callback'); |
||
| 71 | $view->setDI($this->di); |
||
| 72 | $this->views[$region][] = $view; |
||
| 73 | |||
| 74 | return $this; |
||
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Add a string as a view. |
||
| 81 | * |
||
| 82 | * @param string $content the content |
||
| 83 | * @param string $region which region to attach the view |
||
| 84 | * @param int $sort which order to display the views |
||
| 85 | * |
||
| 86 | * @return $this |
||
| 87 | */ |
||
| 88 | View Code Duplication | public function addString($content, $region = 'main', $sort = 0) |
|
| 89 | { |
||
| 90 | $view = $this->di->get('view'); |
||
| 91 | $view->set($content, [], $sort, 'string'); |
||
| 92 | $view->setDI($this->di); |
||
| 93 | $this->views[$region][] = $view; |
||
| 94 | |||
| 95 | return $this; |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Set the suffix of the template files to include. |
||
| 102 | * |
||
| 103 | * @param string $suffix file suffix of template files, append to filenames for template files |
||
| 104 | * |
||
| 105 | * @return $this |
||
| 106 | */ |
||
| 107 | public function setFileSuffix($suffix) |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * Set base path where to find views. |
||
| 115 | * |
||
| 116 | * @param string $path where all views reside |
||
| 117 | * |
||
| 118 | * @return $this |
||
| 119 | * @throws \Exception |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function setBasePath($path) |
|
| 122 | { |
||
| 123 | if (!is_dir($path)) { |
||
| 124 | throw new \Exception("Base path for views is not a directory: " . $path); |
||
| 125 | } |
||
| 126 | $this->path = rtrim($path, '/') . '/'; |
||
| 127 | } |
||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * Check if a region has views to render. |
||
| 133 | * |
||
| 134 | * @param string $region which region to check |
||
| 135 | * |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | public function hasContent($region) |
||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Render all views for a specific region. |
||
| 147 | * |
||
| 148 | * @param string $region which region to use |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function render($region = 'main') |
||
| 175 | } |
||
| 176 |
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: