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 GeoLocation extends DataObject |
||
| 9 | { |
||
| 10 | private $address; |
||
| 11 | private $data; |
||
| 12 | private $creation; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param string $address |
||
| 16 | * @param PdoDatabase $database |
||
| 17 | * @param bool $forUpdate |
||
| 18 | * @return GeoLocation |
||
| 19 | */ |
||
| 20 | public static function getByAddress($address, PdoDatabase $database, $forUpdate = false) |
||
| 21 | { |
||
| 22 | $lockMode = $forUpdate ? ' FOR UPDATE' : ''; |
||
| 23 | $sql = "SELECT * FROM geolocation WHERE address = :id LIMIT 1" . $lockMode; |
||
| 24 | |||
| 25 | $statement = $database->prepare($sql); |
||
| 26 | $statement->bindValue(":id", $address); |
||
| 27 | |||
| 28 | $statement->execute(); |
||
| 29 | |||
| 30 | $resultObject = $statement->fetchObject(get_called_class()); |
||
| 31 | |||
| 32 | if ($resultObject != false) { |
||
| 33 | $resultObject->isNew = false; |
||
| 34 | $resultObject->setDatabase($database); |
||
| 35 | } |
||
| 36 | |||
| 37 | return $resultObject; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function save() |
||
| 41 | { |
||
| 42 | if ($this->isNew) { |
||
| 43 | // insert |
||
| 44 | $statement = $this->dbObject->prepare("INSERT INTO `geolocation` (address, data) VALUES (:address, :data) ON DUPLICATE KEY UPDATE address = address;"); |
||
| 45 | $statement->bindValue(":address", $this->address); |
||
| 46 | $statement->bindValue(":data", $this->data); |
||
| 47 | if ($statement->execute()) { |
||
| 48 | $this->isNew = false; |
||
| 49 | $this->id = $this->dbObject->lastInsertId(); |
||
|
|
|||
| 50 | } |
||
| 51 | else { |
||
| 52 | throw new Exception($statement->errorInfo()); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | else { |
||
| 56 | // update |
||
| 57 | $statement = $this->dbObject->prepare("UPDATE `geolocation` SET address = :address, data = :data WHERE id = :id;"); |
||
| 58 | $statement->bindValue(":address", $this->address); |
||
| 59 | $statement->bindValue(":id", $this->id); |
||
| 60 | $statement->bindValue(":data", $this->data); |
||
| 61 | |||
| 62 | if (!$statement->execute()) { |
||
| 63 | throw new Exception($statement->errorInfo()); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getAddress() |
||
| 69 | { |
||
| 70 | return $this->address; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $address |
||
| 75 | */ |
||
| 76 | public function setAddress($address) |
||
| 79 | } |
||
| 80 | |||
| 81 | public function getData() |
||
| 84 | } |
||
| 85 | |||
| 86 | public function setData($data) |
||
| 89 | } |
||
| 90 | |||
| 91 | public function getCreation() |
||
| 92 | { |
||
| 94 | } |
||
| 95 | } |
||
| 96 |
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.