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 |
||
21 | class EntriesBinding extends Binding |
||
22 | { |
||
23 | use EntriesBindingTrait, LogsBindingTrait, UsersBindingTrait; |
||
24 | |||
25 | const INCLUDE_USER = 'user'; |
||
26 | const INCLUDE_LOG = 'log'; |
||
27 | |||
28 | /** |
||
29 | * Constuct an entries 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 an entry by ID |
||
42 | * |
||
43 | * By default, entries contain both log and user sub-objects. |
||
44 | * |
||
45 | * @param int|string $id |
||
46 | * @param array $params |
||
47 | * |
||
48 | * @return Entry|null |
||
49 | */ |
||
50 | public function get($id, $params = [self::INCLUDE => [self::INCLUDE_LOG, self::INCLUDE_USER]]) |
||
54 | |||
55 | /** |
||
56 | * Instantiate an entry retrieved via `get()` |
||
57 | * |
||
58 | * @used-by EntriesBinding::instantiateListedObject() |
||
59 | * |
||
60 | * @param array $databaseRow |
||
61 | * @param array $params |
||
62 | * |
||
63 | * @return Entry |
||
64 | */ |
||
65 | protected function instantiateObject($databaseRow, $params) |
||
79 | |||
80 | /** |
||
81 | * Instantiate an entry retrieved via `all()` |
||
82 | * |
||
83 | * @param array $databaseRow |
||
84 | * @param array $params |
||
85 | * |
||
86 | * @uses EntriesBinding::instantiateObject() |
||
87 | * |
||
88 | * @return Entry |
||
89 | */ |
||
90 | protected function instantiateListedObject($databaseRow, $params) |
||
94 | |||
95 | /** |
||
96 | * Retrieve all entries in a specific log, by log ID |
||
97 | * |
||
98 | * By default, entries retrieved by this method contain user sub-object, but _not_ a log sub-object. |
||
99 | * |
||
100 | * @param integer|string $id Numeric log ID |
||
101 | * @param array $params (Optional) Associative array of additional request parameters |
||
102 | * @return Entry[] |
||
103 | */ |
||
104 | View Code Duplication | public function listByLog($id, $params = [self::INCLUDE => [self::INCLUDE_USER]]) |
|
122 | |||
123 | /** |
||
124 | * Retrieve most recent entry of a specific log |
||
125 | * |
||
126 | * "Most recent" is detemermined by the listOrder() method, which defaults to descending creation date. By default, |
||
127 | * entries retrieved by this method contain user sub-object, but _not_ a log sub-object. |
||
128 | * |
||
129 | * @param integer|string $id Numeric log ID |
||
130 | * @param array $params (Optional) Associative array of additional request parameters |
||
131 | * @return Entry|null |
||
132 | */ |
||
133 | public function recent($id, $params = [self::INCLUDE => [self::INCLUDE_USER]]) |
||
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.