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 |
||
| 8 | class EmailTemplate extends DataObject |
||
| 9 | { |
||
| 10 | const CREATED = "created"; |
||
| 11 | const NOT_CREATED = "not created"; |
||
| 12 | const NONE = null; |
||
| 13 | |||
| 14 | private $name; |
||
| 15 | private $text; |
||
| 16 | private $jsquestion; |
||
| 17 | private $active = 1; |
||
| 18 | private $preloadonly = 0; |
||
| 19 | private $defaultaction = self::NOT_CREATED; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Gets active non-preload templates |
||
| 23 | * @param string $defaultAction Default action to take (EmailTemplate::CREATED or EmailTemplate::NOT_CREATED) |
||
| 24 | * @param PdoDatabase $database |
||
| 25 | * @return array|false |
||
| 26 | */ |
||
| 27 | public static function getActiveTemplates($defaultAction, PdoDatabase $database = null) |
||
| 28 | { |
||
| 29 | if ($database == null) { |
||
| 30 | $database = gGetDb(); |
||
| 31 | } |
||
| 32 | |||
| 33 | global $createdid; |
||
| 34 | |||
| 35 | $statement = $database->prepare(<<<SQL |
||
| 36 | SELECT * FROM `emailtemplate` |
||
| 37 | WHERE defaultaction = :forcreated AND active = 1 AND preloadonly = 0 AND id != :createdid; |
||
| 38 | SQL |
||
| 39 | ); |
||
| 40 | $statement->bindValue(":createdid", $createdid); |
||
| 41 | $statement->bindValue(":forcreated", $defaultAction); |
||
| 42 | |||
| 43 | $statement->execute(); |
||
| 44 | |||
| 45 | $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
||
| 46 | |||
| 47 | /** @var EmailTemplate $t */ |
||
| 48 | foreach ($resultObject as $t) { |
||
| 49 | $t->setDatabase($database); |
||
| 50 | $t->isNew = false; |
||
| 51 | } |
||
| 52 | |||
| 53 | return $resultObject; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Gets active non-preload and preload templates |
||
| 58 | * @param bool|string $defaultAction Default action to take (EmailTemplate::CREATED or EmailTemplate::NOT_CREATED) |
||
| 59 | * @param PdoDatabase $database |
||
| 60 | * @return array|false |
||
| 61 | */ |
||
| 62 | public static function getAllActiveTemplates($defaultAction, PdoDatabase $database = null) |
||
| 63 | { |
||
| 64 | if ($database == null) { |
||
| 65 | $database = gGetDb(); |
||
| 66 | } |
||
| 67 | |||
| 68 | $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE defaultaction = :forcreated AND active = 1;"); |
||
| 69 | |||
| 70 | if ($defaultAction === false) { |
||
| 71 | $statement = $database->prepare( |
||
| 72 | "SELECT * FROM `emailtemplate` WHERE defaultaction not in ('created', 'not created') AND active = 1;"); |
||
| 73 | } |
||
| 74 | |||
| 75 | $statement->bindValue(":forcreated", $defaultAction); |
||
| 76 | |||
| 77 | $statement->execute(); |
||
| 78 | |||
| 79 | $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
||
| 80 | |||
| 81 | /** @var EmailTemplate $t */ |
||
| 82 | foreach ($resultObject as $t) { |
||
| 83 | $t->setDatabase($database); |
||
| 84 | $t->isNew = false; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $resultObject; |
||
| 88 | } |
||
| 89 | |||
| 90 | public static function getByName($name, PdoDatabase $database) |
||
| 91 | { |
||
| 92 | $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE name = :name LIMIT 1;"); |
||
| 93 | $statement->bindValue(":name", $name); |
||
| 94 | |||
| 95 | $statement->execute(); |
||
| 96 | |||
| 97 | $resultObject = $statement->fetchObject(get_called_class()); |
||
| 98 | |||
| 99 | if ($resultObject != false) { |
||
| 100 | $resultObject->isNew = false; |
||
| 101 | $resultObject->setDatabase($database); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $resultObject; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function save() |
||
| 108 | { |
||
| 109 | if ($this->isNew) { |
||
| 110 | // insert |
||
| 111 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 112 | INSERT INTO `emailtemplate` (name, text, jsquestion, defaultaction, active, preloadonly) |
||
| 113 | VALUES (:name, :text, :jsquestion, :defaultaction, :active, :preloadonly); |
||
| 114 | SQL |
||
| 115 | ); |
||
| 116 | $statement->bindValue(":name", $this->name); |
||
| 117 | $statement->bindValue(":text", $this->text); |
||
| 118 | $statement->bindValue(":jsquestion", $this->jsquestion); |
||
| 119 | $statement->bindValue(":defaultaction", $this->defaultaction); |
||
| 120 | $statement->bindValue(":active", $this->active); |
||
| 121 | $statement->bindValue(":preloadonly", $this->preloadonly); |
||
| 122 | |||
| 123 | if ($statement->execute()) { |
||
| 124 | $this->isNew = false; |
||
| 125 | $this->id = $this->dbObject->lastInsertId(); |
||
|
|
|||
| 126 | } |
||
| 127 | else { |
||
| 128 | throw new Exception($statement->errorInfo()); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | else { |
||
| 132 | // update |
||
| 133 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 134 | UPDATE `emailtemplate` |
||
| 135 | SET name = :name, |
||
| 136 | text = :text, |
||
| 137 | jsquestion = :jsquestion, |
||
| 138 | defaultaction = :defaultaction, |
||
| 139 | active = :active, |
||
| 140 | preloadonly = :preloadonly |
||
| 141 | WHERE id = :id; |
||
| 142 | SQL |
||
| 143 | ); |
||
| 144 | $statement->bindValue(":id", $this->id); |
||
| 145 | $statement->bindValue(":name", $this->name); |
||
| 146 | $statement->bindValue(":text", $this->text); |
||
| 147 | $statement->bindValue(":jsquestion", $this->jsquestion); |
||
| 148 | $statement->bindValue(":defaultaction", $this->defaultaction); |
||
| 149 | $statement->bindValue(":active", $this->active); |
||
| 150 | $statement->bindValue(":preloadonly", $this->preloadonly); |
||
| 151 | |||
| 152 | if (!$statement->execute()) { |
||
| 153 | throw new Exception($statement->errorInfo()); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Override delete() from DataObject |
||
| 160 | */ |
||
| 161 | public function delete() |
||
| 162 | { |
||
| 163 | throw new Exception("You shouldn't be doing that, you'll break logs."); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getName() |
||
| 167 | { |
||
| 168 | return $this->name; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function setName($name) |
||
| 172 | { |
||
| 173 | $this->name = $name; |
||
| 174 | } |
||
| 175 | |||
| 176 | public function getText() |
||
| 177 | { |
||
| 178 | return $this->text; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function setText($text) |
||
| 184 | } |
||
| 185 | |||
| 186 | public function getJsquestion() |
||
| 187 | { |
||
| 188 | return $this->jsquestion; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function setJsquestion($jsquestion) |
||
| 192 | { |
||
| 193 | $this->jsquestion = $jsquestion; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getDefaultAction() |
||
| 200 | { |
||
| 201 | return $this->defaultaction; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $defaultAction |
||
| 206 | */ |
||
| 207 | public function setDefaultAction($defaultAction) |
||
| 208 | { |
||
| 209 | $this->defaultaction = $defaultAction; |
||
| 210 | } |
||
| 211 | |||
| 212 | public function getActive() |
||
| 213 | { |
||
| 214 | return $this->active == 1; |
||
| 215 | } |
||
| 216 | |||
| 217 | public function setActive($active) |
||
| 218 | { |
||
| 219 | $this->active = $active ? 1 : 0; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function getPreloadOnly() |
||
| 223 | { |
||
| 224 | return $this->preloadonly == 1; |
||
| 225 | } |
||
| 226 | |||
| 227 | public function setPreloadOnly($preloadonly) |
||
| 230 | } |
||
| 231 | |||
| 232 | public function getObjectDescription() |
||
| 233 | { |
||
| 234 | $safeName = htmlentities($this->name); |
||
| 235 | $id = $this->id; |
||
| 236 | return "<a href=\"acc.php?action=emailmgmt&edit={$id}\">Email Template #{$id} ({$safeName})</a>"; |
||
| 237 | } |
||
| 238 | } |
||
| 239 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.