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 |
||
6 | class Comment extends DataObject |
||
7 | { |
||
8 | private $time; |
||
9 | private $user; |
||
10 | private $comment; |
||
11 | private $visibility = "user"; |
||
12 | private $request; |
||
13 | |||
14 | /** |
||
15 | * @param integer $id |
||
16 | * @param null|PdoDatabase $database |
||
17 | * @return Comment[] |
||
18 | * @throws Exception |
||
19 | */ |
||
20 | public static function getForRequest($id, PdoDatabase $database = null) |
||
21 | { |
||
22 | if ($database == null) { |
||
23 | $database = gGetDb(); |
||
24 | } |
||
25 | |||
26 | if (User::getCurrent()->isAdmin() || User::getCurrent()->isCheckuser()) { |
||
27 | // current user is an admin or checkuser, so retrieve everything. |
||
28 | $statement = $database->prepare("SELECT * FROM comment WHERE request = :target;"); |
||
29 | } |
||
30 | else { |
||
31 | // current user isn't an admin, so limit to only those which are visible to users, and private comments |
||
32 | // the user has posted themselves. |
||
33 | $statement = $database->prepare(<<<SQL |
||
34 | SELECT * FROM comment |
||
35 | WHERE request = :target AND (visibility = 'user' OR user = :userid); |
||
36 | SQL |
||
37 | ); |
||
38 | $statement->bindValue(":userid", User::getCurrent()->getId()); |
||
39 | } |
||
40 | |||
41 | $statement->bindValue(":target", $id); |
||
42 | |||
43 | $statement->execute(); |
||
44 | |||
45 | $result = array(); |
||
46 | /** @var Comment $v */ |
||
47 | foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
||
48 | $v->isNew = false; |
||
49 | $v->setDatabase($database); |
||
50 | $result[] = $v; |
||
51 | } |
||
52 | |||
53 | return $result; |
||
54 | } |
||
55 | |||
56 | public function save() |
||
57 | { |
||
58 | if ($this->isNew) { |
||
59 | // insert |
||
60 | $statement = $this->dbObject->prepare(<<<SQL |
||
61 | INSERT INTO comment ( time, user, comment, visibility, request ) |
||
62 | VALUES ( CURRENT_TIMESTAMP(), :user, :comment, :visibility, :request ); |
||
63 | SQL |
||
64 | ); |
||
65 | $statement->bindValue(":user", $this->user); |
||
66 | $statement->bindValue(":comment", $this->comment); |
||
67 | $statement->bindValue(":visibility", $this->visibility); |
||
68 | $statement->bindValue(":request", $this->request); |
||
69 | |||
70 | if ($statement->execute()) { |
||
71 | $this->isNew = false; |
||
72 | $this->id = $this->dbObject->lastInsertId(); |
||
|
|||
73 | } |
||
74 | else { |
||
75 | throw new Exception($statement->errorInfo()); |
||
76 | } |
||
77 | } |
||
78 | else { |
||
79 | // update |
||
80 | $statement = $this->dbObject->prepare(<<<SQL |
||
81 | UPDATE comment |
||
82 | SET comment = :comment, visibility = :visibility |
||
83 | WHERE id = :id; |
||
84 | SQL |
||
85 | ); |
||
86 | $statement->bindValue(":id", $this->id); |
||
87 | $statement->bindValue(":comment", $this->comment); |
||
88 | $statement->bindValue(":visibility", $this->visibility); |
||
89 | |||
90 | if (!$statement->execute()) { |
||
91 | throw new Exception($statement->errorInfo()); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public function getTime() |
||
99 | } |
||
100 | |||
101 | public function getUser() |
||
102 | { |
||
103 | return $this->user; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Summary of getUserObject |
||
108 | * @return User|null |
||
109 | */ |
||
110 | public function getUserObject() |
||
111 | { |
||
112 | return User::getById($this->user, $this->dbObject); |
||
113 | } |
||
114 | |||
115 | public function setUser($user) |
||
116 | { |
||
117 | $this->user = $user; |
||
118 | } |
||
119 | |||
120 | public function getComment() |
||
121 | { |
||
122 | return $this->comment; |
||
123 | } |
||
124 | |||
125 | public function setComment($comment) |
||
128 | } |
||
129 | |||
130 | public function getVisibility() |
||
131 | { |
||
132 | return $this->visibility; |
||
133 | } |
||
134 | |||
135 | public function setVisibility($visibility) |
||
136 | { |
||
137 | $this->visibility = $visibility; |
||
138 | } |
||
139 | |||
140 | public function getRequest() |
||
141 | { |
||
142 | return $this->request; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Summary of getRequestObject |
||
147 | * @return Request|null |
||
148 | */ |
||
149 | public function getRequestObject() |
||
150 | { |
||
151 | return Request::getById($this->request, $this->dbObject); |
||
152 | } |
||
153 | |||
154 | public function setRequest($request) |
||
157 | } |
||
158 | } |
||
159 |
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.