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 |
||
| 20 | class LogsBinding extends AbstractBinding |
||
| 21 | { |
||
| 22 | use LogsBindingTrait, DevicesBindingTrait, EntriesBindingTrait; |
||
| 23 | |||
| 24 | const INCLUDE_DEVICE = 'device'; |
||
| 25 | const INCLUDE_ENTRIES = 'entries'; |
||
| 26 | const INCLUDE_RECENT_ENTRIES = 'recent'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Construct a logs binding from a database connector |
||
| 30 | * |
||
| 31 | * @param PDO $database |
||
| 32 | * |
||
| 33 | * @throws BindingException |
||
| 34 | */ |
||
| 35 | public function __construct(PDO $database) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Retrieve all logs from database |
||
| 42 | * |
||
| 43 | * By default, logs contain a device sub-object |
||
| 44 | * |
||
| 45 | * @param array $params |
||
| 46 | * |
||
| 47 | * @return Object[] |
||
| 48 | */ |
||
| 49 | public function all($params = [self::SCOPE_INCLUDE => [self::INCLUDE_DEVICE]]) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Retrieve log by ID from database |
||
| 56 | * |
||
| 57 | * By default the log contains a device sub-object |
||
| 58 | * |
||
| 59 | * @param int|string $id |
||
| 60 | * @param array $params |
||
| 61 | * |
||
| 62 | * @return Log|null |
||
| 63 | */ |
||
| 64 | public function get($id, $params = [self::SCOPE_INCLUDE => [self::INCLUDE_DEVICE]]) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Instantiate a log retrieved via `get()` |
||
| 71 | * |
||
| 72 | * @used-by LogsBinding::instantiateListedObject() |
||
| 73 | * |
||
| 74 | * @param array $databaseRow |
||
| 75 | * @param array $params |
||
| 76 | * |
||
| 77 | * @return Log |
||
| 78 | */ |
||
| 79 | protected function instantiateObject($databaseRow, $params) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Instantiate a log retrieved via `all()` or `listByDevice()` |
||
| 104 | * |
||
| 105 | * @used-by LogsBinding::listByDevice() |
||
| 106 | * |
||
| 107 | * @param array $databaseRow |
||
| 108 | * @param array $params |
||
| 109 | * |
||
| 110 | * @uses LogsBinding::instantiateObject() |
||
| 111 | * |
||
| 112 | * @return Log |
||
| 113 | */ |
||
| 114 | protected function instantiateListedObject($databaseRow, $params) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Retrieve all logs associated with a specific device, by device ID |
||
| 121 | * |
||
| 122 | * By default, the logs retrieved will _not_ contain a device sub-object |
||
| 123 | * |
||
| 124 | * @param string|integer $id Numeric device ID |
||
| 125 | * @param array $params (Optional) Associative array of additional request parameters |
||
| 126 | * |
||
| 127 | * @uses LogsBinding::instantiateListedObject() |
||
| 128 | * |
||
| 129 | * @return Log[] |
||
| 130 | */ |
||
| 131 | public function listByDevice($id, $params = []) |
||
| 149 | } |
||
| 150 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.