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 |
||
| 8 | class ViewContainer implements |
||
| 9 | \Anax\Common\ConfigureInterface, |
||
| 10 | \Anax\Common\AppInjectableInterface |
||
| 11 | { |
||
| 12 | use \Anax\Common\ConfigureTrait, |
||
| 13 | \Anax\Common\AppInjectableTrait; |
||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | /** @var [] $views Array for all views. */ |
||
|
|
|||
| 18 | private $views = []; |
||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | /** |
||
| 23 | * Convert template to path to template file. |
||
| 24 | * |
||
| 25 | * @param string $template the name of the template file to include |
||
| 26 | * |
||
| 27 | * @throws Anax\View\Exception when template file is missing |
||
| 28 | * |
||
| 29 | * @return string as path to the template file |
||
| 30 | */ |
||
| 31 | public function getTemplateFile($template) |
||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Add (create) a view to be included, pass optional data and put the |
||
| 50 | * view in an optional specific region (default region is "main") and |
||
| 51 | * pass an optional sort value where the highest value is rendered first. |
||
| 52 | * The $template can be a: |
||
| 53 | * filename (string), |
||
| 54 | * callback (array with key callback set to a callable array), |
||
| 55 | * view array (key value array with template, data, region, sort) |
||
| 56 | * |
||
| 57 | * @param string $template the name of the template file to include. |
||
| 58 | * @param array $data variables to make available to the view, |
||
| 59 | * default is empty. |
||
| 60 | * @param string $region which region to attach the view, default is |
||
| 61 | * "main". |
||
| 62 | * @param integer $sort which order to display the views. |
||
| 63 | * |
||
| 64 | * @return self for chaining. |
||
| 65 | */ |
||
| 66 | public function add($template, $data = [], $region = "main", $sort = 0) |
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Add a callback to be rendered as a view. |
||
| 106 | * |
||
| 107 | * @param string $callback function to call to get the content of the view |
||
| 108 | * @param array $data variables to make available to the view, default is empty |
||
| 109 | * @param string $region which region to attach the view |
||
| 110 | * @param int $sort which order to display the views |
||
| 111 | * |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function addCallback($callback, $data = [], $region = "main", $sort = 0) |
|
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Add a string as a view. |
||
| 127 | * |
||
| 128 | * @param string $content the content |
||
| 129 | * @param string $region which region to attach the view |
||
| 130 | * @param int $sort which order to display the views |
||
| 131 | * |
||
| 132 | * @return $this |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function addString($content, $region = "main", $sort = 0) |
|
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Check if a region has views to render. |
||
| 147 | * |
||
| 148 | * @param string $region which region to check |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function hasContent($region) |
||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Set the app object to inject into view rendering phase. |
||
| 161 | * |
||
| 162 | * @param object $app with framework resources. |
||
| 163 | * |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function setApp($app) |
||
| 171 | |||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Render all views for a specific region. |
||
| 176 | * |
||
| 177 | * @param string $region which region to use |
||
| 178 | * |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | public function render($region = "main") |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * Render all views for a specific region and buffer the result. |
||
| 206 | * |
||
| 207 | * @param string $region which region to use. |
||
| 208 | * |
||
| 209 | * @return string with the buffered results. |
||
| 210 | */ |
||
| 211 | public function renderBuffered($region = "main") |
||
| 219 | } |
||
| 220 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.