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 |
||
| 38 | 1 | class FlashNotifier extends Nette\Object |
|
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var Storage\IStorage |
||
| 42 | */ |
||
| 43 | protected $storage; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $useTranslator; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Localization\ITranslator |
||
| 52 | */ |
||
| 53 | protected $translator; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param bool $useTranslator |
||
| 57 | * @param Storage\IStorage $storage |
||
| 58 | * @param Localization\ITranslator|NULL $translator |
||
| 59 | */ |
||
| 60 | public function __construct( |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Flash a success message |
||
| 72 | * |
||
| 73 | * @param string $message |
||
| 74 | * @param string|NULL $title |
||
| 75 | * |
||
| 76 | * @return Entities\IMessage |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function success($message, $title = NULL) : Entities\IMessage |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Flash an information message |
||
| 88 | * |
||
| 89 | * @param string $message |
||
| 90 | * @param string|NULL $title |
||
| 91 | * |
||
| 92 | * @return Entities\IMessage |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function info($message, $title = NULL) : Entities\IMessage |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Flash a warning message |
||
| 104 | * |
||
| 105 | * @param string $message |
||
| 106 | * @param string|NULL $title |
||
| 107 | * |
||
| 108 | * @return Entities\IMessage |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function warning($message, $title = NULL) : Entities\IMessage |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Flash an error message |
||
| 120 | * |
||
| 121 | * @param string $message |
||
| 122 | * @param string|NULL $title |
||
| 123 | * |
||
| 124 | * @return Entities\IMessage |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function error($message, $title = NULL) : Entities\IMessage |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Add an "important" flash to the session |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function important() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Flash an overlay modal |
||
| 146 | * |
||
| 147 | * @param string $message |
||
| 148 | * @param string|NULL $title |
||
| 149 | * |
||
| 150 | * @return Entities\IMessage |
||
| 151 | */ |
||
| 152 | public function overlay($message, $title = NULL) : Entities\IMessage |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $message |
||
| 171 | * @param string $level |
||
| 172 | * @param string|NULL $title |
||
| 173 | * @param boolean $overlay |
||
| 174 | * @param int|NULL $count |
||
| 175 | * @param array $parameters |
||
| 176 | * |
||
| 177 | * @return Entities\IMessage |
||
| 178 | */ |
||
| 179 | public function message($message, $level = 'info', $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Flash a general message |
||
| 186 | * |
||
| 187 | * @param string $message |
||
| 188 | * @param string $level |
||
| 189 | * @param string|NULL $title |
||
| 190 | * @param boolean $overlay |
||
| 191 | * @param int|NULL $count |
||
| 192 | * @param array $parameters |
||
| 193 | * |
||
| 194 | * @return Entities\IMessage |
||
| 195 | */ |
||
| 196 | public function setMessage($message, $level = 'info', $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param Entities\IMessage $flash |
||
| 263 | * @param Entities\IMessage[] $messages |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | private function checkUnique(Entities\IMessage $flash, array $messages) : bool |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param array $attributes |
||
| 280 | * @param string $type |
||
| 281 | * @param mixed $default |
||
| 282 | * |
||
| 283 | * @return mixed |
||
| 284 | */ |
||
| 285 | private function checkForAttribute(array $attributes, string $type, $default) |
||
| 319 | } |
||
| 320 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.