Complex classes like Entities often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Entities, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class Entities |
||
43 | { |
||
44 | private $wsClient; |
||
45 | |||
46 | /** |
||
47 | * Class constructor |
||
48 | * @param object $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 array Entity data |
||
61 | */ |
||
62 | public function findOneByID($moduleName, $entityID, array $select = [ ]) |
||
73 | |||
74 | /** |
||
75 | * Retrieve the entity matching a list of constraints |
||
76 | * @param string $moduleName The name of the module / entity type |
||
77 | * @param array $params Data used to find a matching entry |
||
78 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
79 | * @return array The matching record |
||
80 | */ |
||
81 | public function findOne($moduleName, array $params, array $select = [ ]) |
||
88 | |||
89 | /** |
||
90 | * Retrieves the ID of the entity matching a list of constraints + prepends '<module_id>x' string to it |
||
91 | * @param string $moduleName The name of the module / entity type |
||
92 | * @param array $params Data used to find a matching entry |
||
93 | * @return string Type ID (a numeric ID + '<module_id>x') |
||
94 | */ |
||
95 | public function getID($moduleName, array $params) |
||
108 | |||
109 | /** |
||
110 | * Retrieve a numeric ID of the entity matching a list of constraints |
||
111 | * @param string $moduleName The name of the module / entity type |
||
112 | * @param array $params Data used to find a matching entry |
||
113 | * @return integer Numeric ID |
||
114 | */ |
||
115 | public function getNumericID($moduleName, array $params) |
||
123 | |||
124 | /** |
||
125 | * Creates an entity for the giving module |
||
126 | * @param string $moduleName Name of the module / entity type for which the entry has to be created |
||
127 | * @param array $params Entity data |
||
128 | * @return array Entity creation results |
||
129 | */ |
||
130 | public function createOne($moduleName, array $params) |
||
152 | |||
153 | /** |
||
154 | * Updates an entity |
||
155 | * @param string $moduleName The name of the module / entity type |
||
156 | * @param array $params Entity data |
||
157 | * @return array Entity update result |
||
158 | */ |
||
159 | public function updateOne($moduleName, $entityID, array $params) |
||
196 | |||
197 | /** |
||
198 | * Provides entity removal functionality |
||
199 | * @param string $moduleName The name of the module / entity type |
||
200 | * @param string $entityID The ID of the entity to delete |
||
201 | * @return array Removal status object |
||
202 | */ |
||
203 | public function deleteOne($moduleName, $entityID) |
||
209 | |||
210 | /** |
||
211 | * Retrieves multiple records using module name and a set of constraints |
||
212 | * @param string $moduleName The name of the module / entity type |
||
213 | * @param array $params Data used to find matching entries |
||
214 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
215 | * @return integer $limit Limit the list of entries to N records (acts like LIMIT in SQL) |
||
216 | * @return integer $offset Integer values to specify the offset of the query |
||
217 | * @return array The array containing matching entries or false if nothing was found |
||
218 | */ |
||
219 | public function findMany($moduleName, array $params, array $select = [ ], $limit = 0, $offset = 0) |
||
239 | |||
240 | /** |
||
241 | * Sync will return a sync result object containing details of changes after modifiedTime |
||
242 | * @param integer [$modifiedTime = null] The date of the first change |
||
243 | * @param string [$moduleName = null] The name of the module / entity type |
||
244 | * @param string [$syncType = null] Sync type determines the scope of the query |
||
245 | * @return array Sync result object |
||
246 | */ |
||
247 | public function sync($modifiedTime = null, $moduleName = null, $syncType = null) |
||
267 | |||
268 | /** |
||
269 | * Builds the query using the supplied parameters |
||
270 | * @access public |
||
271 | * @static |
||
272 | * @param string $moduleName The name of the module / entity type |
||
273 | * @param array $params Data used to find matching entries |
||
274 | * @return string $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
275 | * @return integer $limit Limit the list of entries to N records (acts like LIMIT in SQL) |
||
276 | * @return integer $offset Integer values to specify the offset of the query |
||
277 | * @return string The query build out of the supplied parameters |
||
278 | */ |
||
279 | public static function getQueryString($moduleName, array $params, array $select = [ ], $limit = 0, $offset = 0) |
||
301 | } |
||
302 |
Adding braces to control structures avoids accidental mistakes as your code changes: