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 |
||
| 34 | 1 | class FlashNotifier |
|
| 35 | { |
||
| 36 | /** |
||
| 37 | * Implement nette smart magic |
||
| 38 | */ |
||
| 39 | 1 | use Nette\SmartObject; |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @var Storage\IStorage |
||
| 43 | */ |
||
| 44 | protected $storage; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $useTranslator; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Localization\ITranslator|NULL |
||
| 53 | */ |
||
| 54 | protected $translator; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param bool $useTranslator |
||
| 58 | * @param Storage\IStorage $storage |
||
| 59 | * @param Localization\ITranslator|NULL $translator |
||
| 60 | */ |
||
| 61 | public function __construct( |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Flash a success message |
||
| 73 | * |
||
| 74 | * @param string $message |
||
| 75 | * @param string|NULL $title |
||
| 76 | * |
||
| 77 | * @return Entities\IMessage |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function success($message, $title = NULL) : Entities\IMessage |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Flash an information message |
||
| 89 | * |
||
| 90 | * @param string $message |
||
| 91 | * @param string|NULL $title |
||
| 92 | * |
||
| 93 | * @return Entities\IMessage |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function info($message, $title = NULL) : Entities\IMessage |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Flash a warning message |
||
| 105 | * |
||
| 106 | * @param string $message |
||
| 107 | * @param string|NULL $title |
||
| 108 | * |
||
| 109 | * @return Entities\IMessage |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function warning($message, $title = NULL) : Entities\IMessage |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Flash an error message |
||
| 121 | * |
||
| 122 | * @param string $message |
||
| 123 | * @param string|NULL $title |
||
| 124 | * |
||
| 125 | * @return Entities\IMessage |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function error($message, $title = NULL) : Entities\IMessage |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Add an "important" flash to the session |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function important() : void |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Flash an overlay modal |
||
| 147 | * |
||
| 148 | * @param string $message |
||
| 149 | * @param string|NULL $title |
||
| 150 | * |
||
| 151 | * @return Entities\IMessage |
||
| 152 | */ |
||
| 153 | public function overlay($message, $title = NULL) : Entities\IMessage |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $message |
||
| 172 | * @param string $level |
||
| 173 | * @param string|NULL $title |
||
| 174 | * @param bool $overlay |
||
| 175 | * @param int|NULL $count |
||
| 176 | * @param array $parameters |
||
| 177 | * |
||
| 178 | * @return Entities\IMessage |
||
| 179 | */ |
||
| 180 | public function message($message, $level = Entities\IMessage::LEVEL_INFO, $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Flash a general message |
||
| 187 | * |
||
| 188 | * @param string $message |
||
| 189 | * @param string $level |
||
| 190 | * @param string|NULL $title |
||
| 191 | * @param bool $overlay |
||
| 192 | * @param int|NULL $count |
||
| 193 | * @param array $parameters |
||
| 194 | * |
||
| 195 | * @return Entities\IMessage |
||
| 196 | */ |
||
| 197 | public function setMessage($message, $level = Entities\IMessage::LEVEL_INFO, $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param Entities\IMessage $flash |
||
| 255 | * @param Entities\IMessage[] $messages |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | private function checkUnique(Entities\IMessage $flash, array $messages) : bool |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param array $attributes |
||
| 272 | * @param string $type |
||
| 273 | * @param mixed $default |
||
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | private function checkForAttribute(array $attributes, string $type, $default) |
||
| 311 | } |
||
| 312 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.