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 |
||
| 19 | abstract class AbstractBinding extends Parameters |
||
| 20 | { |
||
| 21 | /** @var PDO Database connector */ |
||
| 22 | private $database; |
||
| 23 | |||
| 24 | /** @var string Name of database table bound to this object*/ |
||
| 25 | private $databaseTable; |
||
| 26 | |||
| 27 | /** @var string The class of all objects created by this binding */ |
||
| 28 | private $object = AbstractObject::class; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Binding constructor |
||
| 32 | * |
||
| 33 | * @param PDO $database Database connector |
||
| 34 | * @param string $databaseTable Name of the bound database table |
||
| 35 | * @param string $type Fully-qualified class name of bound object (e.g. `AbstractObject::class` or |
||
| 36 | * `"\Battis\SharedLogs\Object"`) |
||
| 37 | * |
||
| 38 | * @throws BindingException If `$databases` is not an instance of PDO |
||
| 39 | */ |
||
| 40 | public function __construct(PDO $database, $databaseTable, $type) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Initialize the database connector |
||
| 49 | * |
||
| 50 | * @param PDO $database Database connector |
||
| 51 | * |
||
| 52 | * @throws BindingException If `$database` is not an instance of PDO |
||
| 53 | */ |
||
| 54 | private function initDatabase(PDO $database) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The database connector |
||
| 68 | * |
||
| 69 | * @return PDO |
||
| 70 | */ |
||
| 71 | protected function database() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Initialize the name of the bound database table |
||
| 78 | * |
||
| 79 | * @param string $table Name of bound table in database |
||
| 80 | */ |
||
| 81 | private function initDatabaseTable($table) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The name of the bound database table |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | protected function databaseTable() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Initialize the bound object class |
||
| 101 | * |
||
| 102 | * @param string $type Fully-qualified class name of bound object (e.g. `Object::class` or |
||
| 103 | * `"\Battis\SharedLogs\Object"`) |
||
| 104 | */ |
||
| 105 | private function initObject($type) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Instantiate the bound object |
||
| 115 | * |
||
| 116 | * @param array ...$params |
||
| 117 | * @return AbstractObject |
||
| 118 | */ |
||
| 119 | protected function object(...$params) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Retrieve a single object from the bound database table |
||
| 126 | * |
||
| 127 | * @param integer|string $id A valid numeric object ID |
||
| 128 | * @param array $params (Optional) Associative array of additional request parameters |
||
| 129 | * |
||
| 130 | * @uses AbstractBinding::instantiateObject() |
||
| 131 | * |
||
| 132 | * @return AbstractObject|null `NULL` if no object is found in database |
||
| 133 | */ |
||
| 134 | public function get($id, $params = []) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Instantiate bound object when retrieved via `get()` |
||
| 152 | * |
||
| 153 | * Presumably similar to the instantiation when retrieved via `all()`, but there may be some desire to, for example, |
||
| 154 | * limit the depth of nested subobjects in the context of a list. |
||
| 155 | * |
||
| 156 | * @used-by AbstractBinding::get() |
||
| 157 | * |
||
| 158 | * @param array $databaseRow Bound row of database table |
||
| 159 | * @param array $params Associative array of additional request parameters |
||
| 160 | * |
||
| 161 | * @return AbstractObject |
||
| 162 | * |
||
| 163 | * @see AbstractBinding::instantiateListedObject() |
||
| 164 | */ |
||
| 165 | protected function instantiateObject($databaseRow, $params) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Retrieve all objects from bound database table |
||
| 172 | * |
||
| 173 | * @param array $params (Optional) Associative array of additional request parameters |
||
| 174 | * |
||
| 175 | * @uses AbstractBinding::listOrder() |
||
| 176 | * @uses AbstractBinding::instantiateListedObject() |
||
| 177 | * |
||
| 178 | * @return AbstractObject[] |
||
| 179 | */ |
||
| 180 | public function all($params = []) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Configure ordering of list of bound objects |
||
| 197 | * |
||
| 198 | * The default ordering is to sort the list by descending creation date (most recent first) |
||
| 199 | * |
||
| 200 | * @used-by AbstractBinding::all() |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | protected function listOrder() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Instantiate bound object when retrieved via `all()` |
||
| 211 | * |
||
| 212 | * Presumably similar to the instantiation when retrieved via `get()`, but there may be some desire to, for example, |
||
| 213 | * limit the depth of nested subobjects in the context of a list. |
||
| 214 | * |
||
| 215 | * @used-by AbstractBinding::all() |
||
| 216 | * |
||
| 217 | * @param array $databaseRow Associative array of fields from database to initialize object |
||
| 218 | * @param array $params Associative array of additional request parameters |
||
| 219 | * |
||
| 220 | * @return AbstractObject |
||
| 221 | * |
||
| 222 | * @see AbstractBinding::instantiateObject() |
||
| 223 | */ |
||
| 224 | protected function instantiateListedObject($databaseRow, $params) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Create a new instance of the bound object and store in database |
||
| 231 | * |
||
| 232 | * @param array $params Associative array of fields to initialize in created Object (will potentially be passed on |
||
| 233 | * as additional request parameters to `get()`) |
||
| 234 | * |
||
| 235 | * @uses AbstractBinding::getCreatedObject() |
||
| 236 | * |
||
| 237 | * @return AbstractObject|null `NULL` if the object cannot be created in the database |
||
| 238 | */ |
||
| 239 | public function create($params) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Retrieve a recently created object from database |
||
| 255 | * |
||
| 256 | * The default implementation of this method simply passes the ID on to `get()` to retrieve and return the recently |
||
| 257 | * created object. Presumably similar to `getUpdatedObject()` or `getDeletedObject()`. |
||
| 258 | * |
||
| 259 | * @used-by AbstractBinding::create() |
||
| 260 | * |
||
| 261 | * @param integer|string $id Numeric ID of recently created object in database |
||
| 262 | * @param array $params Associative array of additional request parameters |
||
| 263 | * |
||
| 264 | * @return AbstractObject|null `NULL` if object cannot be retrieved from database |
||
| 265 | * |
||
| 266 | * @see AbstractBinding::getUpdatedObject() |
||
| 267 | * @see AbstractBinding::getDeletedObject() |
||
| 268 | */ |
||
| 269 | protected function getCreatedObject($id, $params) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Update an existing object in the database |
||
| 276 | * |
||
| 277 | * TODO Disambiguate reasons for errors in updating -- separate "can't be found" from "found, but can't be changed" |
||
| 278 | * |
||
| 279 | * @param integer|string $id ID of the object to be updated |
||
| 280 | * @param array $params Associative array of fields to be updated (will also be potentially passed to `get()` as |
||
| 281 | * additional request parameters |
||
| 282 | * |
||
| 283 | * @uses AbstractBinding::getUpdatedObject() |
||
| 284 | * |
||
| 285 | * @return AbstractObject|null `NULL` if the specified object cannot be found or cannot be updated as requested |
||
| 286 | */ |
||
| 287 | public function update($id, $params) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Retrieve a recently updated object from database |
||
| 309 | * |
||
| 310 | * The default implementation of this method simply passes the ID on to `get()` to retrieve and return the recently |
||
| 311 | * updated object. Presumably similar to `getCreatedObject()` and `getDeletedObject()`. |
||
| 312 | * |
||
| 313 | * @param integer|string $id |
||
| 314 | * @param array $params |
||
| 315 | * @return AbstractObject|null |
||
| 316 | * |
||
| 317 | * @see AbstractBinding::getCreatedObject() |
||
| 318 | * @see AbstractBinding::getDeletedObject() |
||
| 319 | */ |
||
| 320 | protected function getUpdatedObject($id, $params) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Delete an object from the bound database |
||
| 327 | * |
||
| 328 | * TODO Disambiguate null responses of "can't find it" and "found it, but can't delete it" |
||
| 329 | * |
||
| 330 | * @param integer|string $id Numeric ID of the object to be deleted |
||
| 331 | * @param array $params (Optional) Associative array of additional request parameters |
||
| 332 | * |
||
| 333 | * @uses AbstractBinding::getDeletedObject() |
||
| 334 | * |
||
| 335 | * @return AbstractObject|null `NULL` if the object is not found or cannot be deleted |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function delete($id, $params = []) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Retrieve a soon-to-be deleted object from database |
||
| 355 | * |
||
| 356 | * The default implementation of this method simply passes the ID on to `get()` to retrieve and return the |
||
| 357 | * soon-to-be deleted object. Presumably similar to `getCreatedObject()` and `getUpdatedObject()`. |
||
| 358 | * |
||
| 359 | * @used-by AbstractBinding::delete() |
||
| 360 | * |
||
| 361 | * @param integer}string $id Numeric ID of the object to be retrieved |
||
|
|
|||
| 362 | * @param array $params Associative array of additional request parameters |
||
| 363 | * |
||
| 364 | * @return AbstractObject|null `NULL` if the object specified cannot be retrieved |
||
| 365 | * |
||
| 366 | * @see AbstractBinding::getCreatedObject() |
||
| 367 | * @see AbstractBinding::getUpdatedObject() |
||
| 368 | */ |
||
| 369 | protected function getDeletedObject($id, $params) |
||
| 373 | } |
||
| 374 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.