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 |
||
| 15 | class EmailTemplate extends DataObject |
||
| 16 | { |
||
| 17 | const CREATED = "created"; |
||
| 18 | const NOT_CREATED = "not created"; |
||
| 19 | const NONE = null; |
||
| 20 | private $name; |
||
| 21 | private $text; |
||
| 22 | /** @var string|null */ |
||
| 23 | private $jsquestion; |
||
| 24 | private $active = 1; |
||
| 25 | private $preloadonly = 0; |
||
| 26 | private $defaultaction = self::NOT_CREATED; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Gets active non-preload templates |
||
| 30 | * |
||
| 31 | * @param string $defaultAction Default action to take (EmailTemplate::CREATED or EmailTemplate::NOT_CREATED) |
||
| 32 | * @param PdoDatabase $database |
||
| 33 | * |
||
| 34 | * @return array|false |
||
| 35 | */ |
||
| 36 | View Code Duplication | public static function getActiveTemplates($defaultAction, PdoDatabase $database) |
|
|
|
|||
| 37 | { |
||
| 38 | global $createdid; |
||
| 39 | |||
| 40 | $statement = $database->prepare(<<<SQL |
||
| 41 | SELECT * FROM `emailtemplate` |
||
| 42 | WHERE defaultaction = :forcreated AND active = 1 AND preloadonly = 0 AND id != :createdid; |
||
| 43 | SQL |
||
| 44 | ); |
||
| 45 | $statement->bindValue(":createdid", $createdid); |
||
| 46 | $statement->bindValue(":forcreated", $defaultAction); |
||
| 47 | |||
| 48 | $statement->execute(); |
||
| 49 | |||
| 50 | $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
||
| 51 | |||
| 52 | /** @var EmailTemplate $t */ |
||
| 53 | foreach ($resultObject as $t) { |
||
| 54 | $t->setDatabase($database); |
||
| 55 | } |
||
| 56 | |||
| 57 | return $resultObject; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Gets active non-preload and preload templates, optionally filtered by the default action. |
||
| 62 | * |
||
| 63 | * @param null|bool|string $defaultAction Default action to take (EmailTemplate::CREATED, |
||
| 64 | * EmailTemplate::NOT_CREATED, or EmailTemplate::NONE), or optionally null to |
||
| 65 | * just get everything. |
||
| 66 | * @param PdoDatabase $database |
||
| 67 | * |
||
| 68 | * @return array|false |
||
| 69 | */ |
||
| 70 | public static function getAllActiveTemplates($defaultAction, PdoDatabase $database) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Gets all the unactive templates |
||
| 99 | * |
||
| 100 | * @param PdoDatabase $database |
||
| 101 | * |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | View Code Duplication | public static function getAllInactiveTemplates(PdoDatabase $database) |
|
| 105 | { |
||
| 106 | $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE active = 0;"); |
||
| 107 | $statement->execute(); |
||
| 108 | |||
| 109 | $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
||
| 110 | |||
| 111 | /** @var EmailTemplate $t */ |
||
| 112 | foreach ($resultObject as $t) { |
||
| 113 | $t->setDatabase($database); |
||
| 114 | } |
||
| 115 | |||
| 116 | return $resultObject; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $name |
||
| 121 | * @param PdoDatabase $database |
||
| 122 | * |
||
| 123 | * @return EmailTemplate|false |
||
| 124 | */ |
||
| 125 | public static function getByName($name, PdoDatabase $database) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return EmailTemplate |
||
| 143 | */ |
||
| 144 | public static function getDroppedTemplate() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @throws Exception |
||
| 156 | */ |
||
| 157 | public function save() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Override delete() from DataObject |
||
| 219 | */ |
||
| 220 | public function delete() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function getName() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $name |
||
| 235 | */ |
||
| 236 | public function setName($name) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getText() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $text |
||
| 251 | */ |
||
| 252 | public function setText($text) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return string|null |
||
| 259 | */ |
||
| 260 | public function getJsquestion() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $jsquestion |
||
| 267 | */ |
||
| 268 | public function setJsquestion($jsquestion) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getDefaultAction() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $defaultAction |
||
| 283 | */ |
||
| 284 | public function setDefaultAction($defaultAction) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function getActive() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param bool $active |
||
| 299 | */ |
||
| 300 | public function setActive($active) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | public function getPreloadOnly() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param bool $preloadonly |
||
| 315 | */ |
||
| 316 | public function setPreloadOnly($preloadonly) |
||
| 320 | } |
||
| 321 |
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.