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 SearchLog extends AbstractModel |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The search identifier this specific search log belongs to. |
||
| 19 | * @var string $searchIdent |
||
| 20 | */ |
||
| 21 | private $searchIdent; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The searched keyword. |
||
| 25 | * @var string $keyword |
||
| 26 | */ |
||
| 27 | private $keyword; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Number of search results. |
||
| 31 | * @var array $numResults |
||
| 32 | */ |
||
| 33 | private $numResults; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Detailed results, if available. |
||
| 37 | * @var array $results |
||
| 38 | */ |
||
| 39 | private $results; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Timestamp of the search (date-time when the search was performed). |
||
| 43 | * @var DateTimeInterface |
||
| 44 | */ |
||
| 45 | private $ts; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Client IP. |
||
| 49 | * @var string $ip |
||
| 50 | */ |
||
| 51 | private $ip; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Client session ID, if any. |
||
| 55 | * @var string $sessionId |
||
| 56 | */ |
||
| 57 | private $sessionId; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The language code. |
||
| 61 | * @var string $lang |
||
| 62 | */ |
||
| 63 | private $lang; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The search origin; an identifier representing where the search was executed from. |
||
| 67 | * @var string $origin |
||
| 68 | */ |
||
| 69 | private $origin; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $ident The search identifier. |
||
| 73 | * @throws InvalidArgumentException If the search ident is not a string. |
||
| 74 | * @return SearchLog Chainable |
||
| 75 | */ |
||
| 76 | public function setSearchIdent($ident) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function searchIdent() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $kw The searched term / keyword. |
||
| 97 | * @throws InvalidArgumentException If the keyword is not a string. |
||
| 98 | * @return SearchLog Chainable |
||
| 99 | */ |
||
| 100 | public function setKeyword($kw) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function keyword() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param integer $num The number of results from search. |
||
| 121 | * @return SearchLog Chainable |
||
| 122 | */ |
||
| 123 | public function setNumResults($num) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return integer |
||
| 131 | */ |
||
| 132 | public function numResults() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param mixed $results The search results data, if available. |
||
| 139 | * @throws InvalidArgumentException If the results is not an array or invalid JSON. |
||
| 140 | * @return SearchLog Chainable |
||
| 141 | */ |
||
| 142 | public function setResults($results) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | public function results() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param DateTimeInterface|string|null $ts The timestamp (date-time the search occured). |
||
| 173 | * @throws InvalidArgumentException If ts is not a valid date-time. |
||
| 174 | * @return SearchLog Chainable |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function setTs($ts) |
|
| 177 | { |
||
| 178 | if ($ts === null) { |
||
| 179 | $this->ts = null; |
||
| 180 | return $this; |
||
| 181 | } |
||
| 182 | if (is_string($ts)) { |
||
| 183 | $ts = new DateTime($ts); |
||
| 184 | } |
||
| 185 | if (!($ts instanceof DateTimeInterface)) { |
||
| 186 | throw new InvalidArgumentException( |
||
| 187 | 'Invalid "ts" value. Must be a date/time string or a DateTime object.' |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | $this->ts = $ts; |
||
| 191 | return $this; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return DateTime |
||
| 196 | */ |
||
| 197 | public function ts() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $ip The IP address of the client that searched. |
||
| 204 | * @return SearchLog Chainable |
||
| 205 | */ |
||
| 206 | public function setIp($ip) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function ip() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $sessionId The session identifier. Typically, `session_id()`. |
||
| 222 | * @throws InvalidArgumentException If the session id is not a string. |
||
| 223 | * @return SearchLog Chainable |
||
| 224 | */ |
||
| 225 | public function setSessionId($sessionId) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function sessionId() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $lang The language code. |
||
| 250 | * @return SearchLog Chainable |
||
| 251 | */ |
||
| 252 | public function setLang($lang) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function lang() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $origin The origin is an identifier representing where the search was executed. |
||
| 268 | * @throws InvalidArgumentException If the origin is not a string. |
||
| 269 | * @return SearchLog Chainable |
||
| 270 | */ |
||
| 271 | public function setOrigin($origin) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function origin() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return boolean |
||
| 294 | */ |
||
| 295 | public function preSave() |
||
| 311 | } |
||
| 312 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..