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 |
||
42 | class Entities |
||
43 | { |
||
44 | private $wsClient = null; |
||
45 | |||
46 | /** |
||
47 | * Class constructor |
||
48 | * @param string $wsClient Parent WSClient instance |
||
49 | */ |
||
50 | public function __construct($wsClient) |
||
54 | |||
55 | /** |
||
56 | * Retrieves an entity by ID |
||
57 | * @param string $moduleName The name of the module / entity type |
||
58 | * @param string $entityID The ID of the entity to retrieve |
||
59 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
60 | * @return boolean Entity data |
||
61 | */ |
||
62 | public function findOneByID($moduleName, $entityID, array $select = [ ]) |
||
66 | |||
67 | /** |
||
68 | * Retrieve the entity matching a list of constraints |
||
69 | * @param string $moduleName The name of the module / entity type |
||
70 | * @param array $params Data used to find a matching entry |
||
71 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
72 | * @return integer The matching record |
||
73 | */ |
||
74 | View Code Duplication | public function findOne($moduleName, array $params, array $select = [ ]) |
|
82 | |||
83 | /** |
||
84 | * Retrieves the ID of the entity matching a list of constraints + prepends '<module_id>x' string to it |
||
85 | * @param string $moduleName The name of the module / entity type |
||
86 | * @param array $params Data used to find a matching entry |
||
87 | * @return integer Type ID (a numeric ID + '<module_id>x') |
||
88 | */ |
||
89 | View Code Duplication | public function getID($moduleName, array $params) |
|
97 | |||
98 | /** |
||
99 | * Retrieve a numeric ID of the entity matching a list of constraints |
||
100 | * @param string $moduleName The name of the module / entity type |
||
101 | * @param array $params Data used to find a matching entry |
||
102 | * @return integer Numeric ID |
||
103 | */ |
||
104 | public function getNumericID($moduleName, array $params) |
||
112 | |||
113 | /** |
||
114 | * Creates an entity for the giving module |
||
115 | * @param string $moduleName Name of the module / entity type for which the entry has to be created |
||
116 | * @param array $params Entity data |
||
117 | * @return array Entity creation results |
||
118 | */ |
||
119 | public function createOne($moduleName, array $params) |
||
141 | |||
142 | /** |
||
143 | * Updates an entity |
||
144 | * @param string $moduleName The name of the module / entity type |
||
145 | * @param array $params Entity data |
||
146 | * @return array Entity update result |
||
147 | */ |
||
148 | public function updateOne($moduleName, $entityID, array $params) |
||
185 | |||
186 | /** |
||
187 | * Provides entity removal functionality |
||
188 | * @param string $moduleName The name of the module / entity type |
||
189 | * @param string $entityID The ID of the entity to delete |
||
190 | * @return array Removal status object |
||
191 | */ |
||
192 | public function deleteOne($moduleName, $entityID) |
||
198 | |||
199 | /** |
||
200 | * Retrieves multiple records using module name and a set of constraints |
||
201 | * @param string $moduleName The name of the module / entity type |
||
202 | * @param array $params Data used to find matching entries |
||
203 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
204 | * @return integer $limit limit the list of entries to N records (acts like LIMIT in SQL) |
||
205 | * @return bool|array The array containing matching entries or false if nothing was found |
||
206 | */ |
||
207 | public function findMany($moduleName, array $params, array $select = [ ], $limit = 0) |
||
227 | |||
228 | /** |
||
229 | * Sync will return a sync result object containing details of changes after modifiedTime |
||
230 | * @param integer [$modifiedTime = null] The date of the first change |
||
231 | * @param string [$moduleName = null] The name of the module / entity type |
||
232 | * @return array Sync result object |
||
233 | */ |
||
234 | public function sync($modifiedTime = null, $moduleName = null) |
||
250 | |||
251 | /** |
||
252 | * Builds the query using the supplied parameters |
||
253 | * @access public |
||
254 | * @static |
||
255 | * @param string $moduleName The name of the module / entity type |
||
256 | * @param array $params Data used to find matching entries |
||
257 | * @return string $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
258 | * @return string $limit limit the list of entries to N records (acts like LIMIT in SQL) |
||
259 | * @return string The query build out of the supplied parameters |
||
260 | */ |
||
261 | public static function getQueryString($moduleName, array $params, array $select = [ ], $limit = 0) |
||
276 | } |
||
277 |
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.