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 |
||
| 20 | class ViewController |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Holds the view variables |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $variable = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Holds the view template |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $template = ''; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Holds the parent template |
||
| 37 | * @var ViewController |
||
| 38 | */ |
||
| 39 | private $parentTemplate = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Holds the registered view helper |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | public $viewHelper = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Set template for this view |
||
| 49 | * @param string $template |
||
| 50 | * @return self |
||
| 51 | * @throws ConstantMissingException |
||
| 52 | * @throws FileNotFoundException |
||
| 53 | */ |
||
| 54 | public function setTemplate(string $template = '') :self |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Add javascript from outside |
||
| 74 | * @param string $file |
||
| 75 | * @return self |
||
| 76 | */ |
||
| 77 | public function addScript($file) :self |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Add stylesheet from outside |
||
| 85 | * @param string $file |
||
| 86 | * @return self |
||
| 87 | */ |
||
| 88 | public function addStylesheet($file) :self |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Return current template |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getTemplate() :string |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Set a single variable |
||
| 105 | * @param string $key |
||
| 106 | * @param string|array $value |
||
| 107 | */ |
||
| 108 | public function setVariable(string $key = '', $value = '') |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get a single variable |
||
| 115 | * @param $key |
||
| 116 | * @return string|array |
||
| 117 | */ |
||
| 118 | public function getVariable($key) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Check if variable exists |
||
| 129 | * @param string $key |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public function hasVariable(string $key) :bool |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set many variables at once |
||
| 143 | * @param array $variables |
||
| 144 | * @return self |
||
| 145 | */ |
||
| 146 | public function setVariables(array $variables = []) :self |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get all variables |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function getVariables() :array |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Define parent template |
||
| 166 | * @param ViewController $view |
||
| 167 | */ |
||
| 168 | public function setParentTemplate(ViewController $view) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get parent template |
||
| 175 | * @return ViewController |
||
| 176 | */ |
||
| 177 | public function getParentTemplate() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Strip spaces and tabs from output |
||
| 184 | * @param $output |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | private function cleanOutput($output) :string |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Render the current view |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function render() :string |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Magic method for providing a view helper |
||
| 217 | * @param $name |
||
| 218 | * @param $arguments |
||
| 219 | * @return string |
||
| 220 | * @throws FileNotFoundException |
||
| 221 | * @throws ViewHelperIncompatibleException |
||
| 222 | * @throws ClassNotFoundException |
||
| 223 | */ |
||
| 224 | public function __call($name, $arguments) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Destructor |
||
| 253 | */ |
||
| 254 | public function __destruct() |
||
| 259 | |||
| 260 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.