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 |
||
| 19 | class Notice implements NoticeInterface |
||
| 20 | { |
||
| 21 | |||
| 22 | const ERROR = 'error'; |
||
| 23 | |||
| 24 | const SUCCESS = 'success'; |
||
| 25 | |||
| 26 | const ATTENTION = 'attention'; // alias for warning |
||
| 27 | |||
| 28 | const INFORMATION = 'information'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string $type |
||
| 32 | */ |
||
| 33 | private $type; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string $message |
||
| 38 | */ |
||
| 39 | private $message; |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string $file |
||
| 44 | */ |
||
| 45 | private $file; |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string $func |
||
| 50 | */ |
||
| 51 | private $func; |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string $line |
||
| 56 | */ |
||
| 57 | private $line; |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * @var boolean $dismissible |
||
| 62 | */ |
||
| 63 | private $dismissible; |
||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Notice constructor. |
||
| 69 | * |
||
| 70 | * @param string $type |
||
| 71 | * @param string $message |
||
| 72 | * @param bool $dismissible |
||
| 73 | * @param string $file |
||
| 74 | * @param string $func |
||
| 75 | * @param string $line |
||
| 76 | * @throws InvalidDataTypeException |
||
| 77 | */ |
||
| 78 | public function __construct($type, $message, $dismissible = true, $file = '', $func = '', $line = '') |
||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | private function types() |
||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function type() |
||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function message() |
||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function file() |
||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function func() |
||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function line() |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | public function isDismissible() |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $type |
||
| 168 | * @throws InvalidDataTypeException |
||
| 169 | */ |
||
| 170 | private function setType($type) |
||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * gets the $invalid_type_message string |
||
| 186 | */ |
||
| 187 | private function invalidTypeMessage() |
||
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $message |
||
| 206 | * @throws InvalidDataTypeException |
||
| 207 | */ |
||
| 208 | View Code Duplication | private function setMessage($message) |
|
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $file |
||
| 224 | * @throws InvalidDataTypeException |
||
| 225 | */ |
||
| 226 | private function setFile($file) |
||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $func |
||
| 242 | * @throws InvalidDataTypeException |
||
| 243 | */ |
||
| 244 | private function setFunc($func) |
||
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * @param int $line |
||
| 260 | * @throws InvalidDataTypeException |
||
| 261 | */ |
||
| 262 | private function setLine($line) |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * @param boolean $dismissible |
||
| 278 | */ |
||
| 279 | private function setDismissible($dismissible = true) |
||
| 283 | |||
| 284 | } |
||
| 285 |