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 /** MicroView */ |
||
| 22 | abstract class View implements IView |
||
| 23 | { |
||
| 24 | /** @var array $styleScripts */ |
||
| 25 | public $styleScripts = []; |
||
| 26 | /** @var bool $asWidget */ |
||
| 27 | public $asWidget = false; |
||
| 28 | /** @var array $params */ |
||
| 29 | public $params = []; |
||
| 30 | /** @var array $stack */ |
||
| 31 | public $stack = []; |
||
| 32 | /** @var Module $module */ |
||
| 33 | public $module; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * @access public |
||
| 38 | * @result void |
||
| 39 | */ |
||
| 40 | public function __construct() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Add parameter into view |
||
| 46 | * |
||
| 47 | * @access public |
||
| 48 | * |
||
| 49 | * @param string $name parameter name |
||
| 50 | * @param mixed $value parameter value |
||
| 51 | * |
||
| 52 | * @return void |
||
| 53 | */ |
||
| 54 | public function addParameter($name, $value) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Widget |
||
| 61 | * |
||
| 62 | * @access public |
||
| 63 | * |
||
| 64 | * @param string $name widget name |
||
| 65 | * @param array $options options array |
||
| 66 | * @param bool $capture capture output |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | * @throws Exception |
||
| 70 | */ |
||
| 71 | public function widget($name, array $options = [], $capture = false) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Begin widget |
||
| 109 | * |
||
| 110 | * @access public |
||
| 111 | * |
||
| 112 | * @param string $name widget name |
||
| 113 | * @param array $options options array |
||
| 114 | * |
||
| 115 | * @return mixed |
||
| 116 | * @throws Exception |
||
| 117 | */ |
||
| 118 | public function beginWidget($name, array $options = []) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Ending widget |
||
| 137 | * |
||
| 138 | * @access public |
||
| 139 | * |
||
| 140 | * @param string $name widget name |
||
| 141 | * |
||
| 142 | * @throws Exception |
||
| 143 | */ |
||
| 144 | public function endWidget($name = '') |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Register JS script |
||
| 185 | * |
||
| 186 | * @access public |
||
| 187 | * |
||
| 188 | * @param string $source file name |
||
| 189 | * @param bool $isHead is head block |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function registerScript($source, $isHead = true) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Register JS file |
||
| 203 | * |
||
| 204 | * @access public |
||
| 205 | * |
||
| 206 | * @param string $source file name |
||
| 207 | * @param bool $isHead is head block |
||
| 208 | * |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | public function registerScriptFile($source, $isHead = true) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Register CSS code |
||
| 221 | * |
||
| 222 | * @access public |
||
| 223 | * |
||
| 224 | * @param string $source file name |
||
| 225 | * @param bool $isHead is head block |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function registerCss($source, $isHead = true) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Register CSS file |
||
| 239 | * |
||
| 240 | * @access public |
||
| 241 | * |
||
| 242 | * @param string $source file name |
||
| 243 | * @param bool $isHead is head block |
||
| 244 | * |
||
| 245 | * @return void |
||
| 246 | */ |
||
| 247 | public function registerCssFile($source, $isHead = true) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Insert styles and scripts into cache |
||
| 257 | * |
||
| 258 | * @access protected |
||
| 259 | * |
||
| 260 | * @param string $cache cache of generated page |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | protected function insertStyleScripts($cache) |
||
| 289 | } |
||
| 290 |
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.