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 |
||
| 17 | abstract class AbstractEndpointRepository implements |
||
| 18 | ConfigAwareInterface, |
||
| 19 | DatabaseAwareInterface, |
||
| 20 | RedisAwareInterface, |
||
| 21 | UuidAwareInterface |
||
| 22 | { |
||
| 23 | use ConfigAwareTrait; |
||
| 24 | use DatabaseAwareTrait; |
||
| 25 | use RedisAwareTrait; |
||
| 26 | use UuidAwareTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Determines the table that the DB is interfacing with |
||
| 30 | * |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | abstract public function getTable(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Determines the primary key of the table |
||
| 37 | * |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | abstract public function getPrimaryKey(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Determines the Result key of the table |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | abstract public function getResultKey(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Allows the ability to overload and swap the DB driver if required |
||
| 51 | * |
||
| 52 | * @return \Aura\Sql\ExtendedPdo |
||
| 53 | */ |
||
| 54 | protected function getDbDriver() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Allows the ability to overload and swap the DB driver if required |
||
| 61 | * |
||
| 62 | * @return \Aura\Sql\ExtendedPdo |
||
| 63 | */ |
||
| 64 | protected function getDbArchiveDriver() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Builds a new query factory ready for use with the QueryObjects |
||
| 71 | * |
||
| 72 | * @param string $type Type of query to use, such as Select, Update etc. |
||
| 73 | * @param bool $newOnly Whether we're getting a new query or using a raw one |
||
| 74 | * |
||
| 75 | * @return \Aura\SqlQuery\QueryInterface |
||
| 76 | */ |
||
| 77 | public function newQuery($type = 'single', $newOnly = false) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Executes the statement to the DB and returns the results |
||
| 98 | * |
||
| 99 | * @param \Aura\SqlQuery\AbstractQuery $query Query object to execute |
||
| 100 | * @param boolean $single Return a sinlge record or not |
||
| 101 | * @param boolean $object Return an object or not |
||
| 102 | * @param boolean $archive Flag to force to use the archive database |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function fireStatementAndReturn( |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Allows for Raw SQL firing without the query builder |
||
| 159 | * |
||
| 160 | * @param string $sql |
||
| 161 | * @param boolean $single |
||
| 162 | * |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | public function readRaw($sql, $single = false) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Reads a single record from the database |
||
| 178 | * |
||
| 179 | * @param string $id |
||
| 180 | * @param string $keyType |
||
| 181 | * @param boolean $object |
||
| 182 | * @param boolean $archive |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function readSinglebyId($id, $keyType = 'primary', $object = false, $archive = false) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Allows reading of elements by ID including those from the archive |
||
| 199 | * |
||
| 200 | * @param string $id |
||
| 201 | * @param string $keyType |
||
| 202 | * @param boolean $object |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public function readSingleByIdWithArchive($id, $keyType = 'primary', $object = false) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Reads all related records from the database |
||
| 216 | * |
||
| 217 | * @param string $id |
||
| 218 | * @param string $keyType Field to search on |
||
| 219 | * @param boolean $archive |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | View Code Duplication | public function readAllById($id, $keyType = 'primary', $archive = false) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Grab all data from the database including from the archive |
||
| 236 | * |
||
| 237 | * @param string $id |
||
| 238 | * @param string $keyType |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function readAllByIdWithArchive($id, $keyType = 'primary') |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Reads all records based off a simple where statement |
||
| 252 | * |
||
| 253 | * @param array $fields |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function readAllByFields($fields) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns all records with no filtering |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function readAll() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Reads the count of records based off a where statement |
||
| 284 | * |
||
| 285 | * @param array $fields |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function readCountByFields($fields) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Sets the proper key to search on based off a string |
||
| 308 | * |
||
| 309 | * @param string $key |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | public function returnKeyType($key) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Generates a quoted string which is appropiate for WHERE IN statements |
||
| 327 | * |
||
| 328 | * @param array $array |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function generateWhereInString(array $array) |
||
| 338 | } |
||
| 339 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: