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 InterfaceMessage extends DataObject |
||
| 9 | { |
||
| 10 | private $content; |
||
| 11 | private $updatecounter; |
||
| 12 | private $description; |
||
| 13 | private $type; |
||
| 14 | |||
| 15 | const SITENOTICE = '31'; |
||
| 16 | const DECL_BANNED = '19'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get a message. |
||
| 20 | * |
||
| 21 | * This is going to be used as a new way of dealing with saved messages for #28 |
||
| 22 | * |
||
| 23 | * The basic idea is there's a key stored in a new column, and we do lookups on that |
||
| 24 | * instead of a possibly variable auto-incrementing ID. |
||
| 25 | * |
||
| 26 | * We can use class constants so the keys are defined in one place only for now, and for |
||
| 27 | * now we are using the auto-incrementing ID as the value of the key, so this function |
||
| 28 | * just uses getById() at the moment. |
||
| 29 | * |
||
| 30 | * @param mixed $key |
||
| 31 | * @return mixed |
||
| 32 | */ |
||
| 33 | public static function get($key) |
||
| 34 | { |
||
| 35 | /** @var InterfaceMessage $message */ |
||
| 36 | $message = self::getById($key, gGetDb()); |
||
| 37 | return $message->getContentForDisplay(); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function save() |
||
| 41 | { |
||
| 42 | if ($this->isNew) { |
||
| 43 | // insert |
||
| 44 | $statement = $this->dbObject->prepare("INSERT INTO interfacemessage (updatecounter, description, type, content) VALUES (0, :desc, :type, :content);"); |
||
| 45 | $statement->bindValue(":type", $this->type); |
||
| 46 | $statement->bindValue(":desc", $this->description); |
||
| 47 | $statement->bindValue(":content", $this->content); |
||
| 48 | if ($statement->execute()) { |
||
| 49 | $this->isNew = false; |
||
| 50 | $this->id = $this->dbObject->lastInsertId(); |
||
|
|
|||
| 51 | } |
||
| 52 | else { |
||
| 53 | throw new Exception($statement->errorInfo()); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | else { |
||
| 57 | // update |
||
| 58 | $statement = $this->dbObject->prepare("UPDATE interfacemessage SET type = :type, description = :desc, content = :content, updatecounter = updatecounter + 1 WHERE id = :id;"); |
||
| 59 | $statement->bindValue(":id", $this->id); |
||
| 60 | $statement->bindValue(":type", $this->type); |
||
| 61 | $statement->bindValue(":desc", $this->description); |
||
| 62 | $statement->bindValue(":content", $this->content); |
||
| 63 | |||
| 64 | if (!$statement->execute()) { |
||
| 65 | throw new Exception($statement->errorInfo()); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | public function getContent() |
||
| 71 | { |
||
| 72 | return $this->content; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function getContentForDisplay() |
||
| 76 | { |
||
| 77 | global $baseurl; |
||
| 78 | |||
| 79 | $message = $this->content; |
||
| 80 | |||
| 81 | if (strpos($message, "%VERSION%") !== false) { |
||
| 82 | $message = str_replace('%VERSION%', Environment::getToolVersion(), $message); |
||
| 83 | } |
||
| 84 | |||
| 85 | $message = str_replace('%TSURL%', $baseurl, $message); |
||
| 86 | return $message; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function setContent($content) |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getUpdateCounter() |
||
| 95 | { |
||
| 96 | return $this->updatecounter; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function getDescription() |
||
| 102 | } |
||
| 103 | |||
| 104 | public function setDescription($description) |
||
| 105 | { |
||
| 106 | $this->description = $description; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function getType() |
||
| 110 | { |
||
| 111 | return $this->type; |
||
| 112 | } |
||
| 113 | |||
| 114 | public function setType($type) |
||
| 115 | { |
||
| 116 | $this->type = $type; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getObjectDescription() |
||
| 122 | } |
||
| 123 | } |
||
| 124 |
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.