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 |
||
| 13 | class View |
||
| 14 | { |
||
| 15 | private $viewPath; |
||
| 16 | private $params = array(); |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param array $params |
||
| 20 | */ |
||
| 21 | 4 | public function __construct(array $params = array()) |
|
| 25 | |||
| 26 | /** |
||
| 27 | * Define multiple value for key (associative array) |
||
| 28 | * @param array $params |
||
| 29 | * @return $this |
||
| 30 | */ |
||
| 31 | 6 | public function addParams(array $params = array()) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Define a value for a specific key |
||
| 39 | * @param string $name |
||
| 40 | * @param mixed $value |
||
| 41 | * @throws UnexpectedValue |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | 2 | View Code Duplication | public function setParam($name, $value) |
| 55 | |||
| 56 | /** |
||
| 57 | * Get defined value for a specific key |
||
| 58 | * @param string $name |
||
| 59 | * @throws UnexpectedValue |
||
| 60 | * @return mixed |
||
| 61 | */ |
||
| 62 | 4 | View Code Duplication | public function getParam($name) |
| 72 | |||
| 73 | /** |
||
| 74 | * Return defined params |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | 4 | public function getParams() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Return string of interpreted template |
||
| 84 | * @return string |
||
| 85 | * @throws UnexpectedValue |
||
| 86 | */ |
||
| 87 | 2 | public function render() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Define view to use |
||
| 110 | * @param string $viewPath Full path to view |
||
| 111 | * @throws UnexpectedValue |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | 3 | View Code Duplication | public function setViewPath($viewPath) |
| 126 | |||
| 127 | /** |
||
| 128 | * Return defined view path |
||
| 129 | * @return mixed |
||
| 130 | */ |
||
| 131 | 3 | public function getViewPath() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Implements object use |
||
| 138 | * @param string $param |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | 1 | public function __get($param) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Implements object use |
||
| 148 | * @param string $param |
||
| 149 | * @param mixed $value |
||
| 150 | * @throws UnexpectedValue |
||
| 151 | * @return View |
||
| 152 | */ |
||
| 153 | 1 | public function __set($param, $value) |
|
| 157 | } |
||
| 158 |
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.