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 = [ ]) |
||
74 | |||
75 | /** |
||
76 | * Retrieve the entity matching a list of constraints |
||
77 | * @param string $moduleName The name of the module / entity type |
||
78 | * @param array $params Data used to find a matching entry |
||
79 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
80 | * @return array The matching record |
||
81 | */ |
||
82 | public function findOne($moduleName, array $params, array $select = [ ]) |
||
89 | |||
90 | /** |
||
91 | * Retrieves the ID of the entity matching a list of constraints + prepends '<module_id>x' string to it |
||
92 | * @param string $moduleName The name of the module / entity type |
||
93 | * @param array $params Data used to find a matching entry |
||
94 | * @return string Type ID (a numeric ID + '<module_id>x') |
||
95 | */ |
||
96 | public function getID($moduleName, array $params) |
||
109 | |||
110 | /** |
||
111 | * Retrieve a numeric ID of the entity matching a list of constraints |
||
112 | * @param string $moduleName The name of the module / entity type |
||
113 | * @param array $params Data used to find a matching entry |
||
114 | * @return integer Numeric ID |
||
115 | */ |
||
116 | public function getNumericID($moduleName, array $params) |
||
124 | |||
125 | /** |
||
126 | * Creates an entity for the giving module |
||
127 | * @param string $moduleName Name of the module / entity type for which the entry has to be created |
||
128 | * @param array $params Entity data |
||
129 | * @return array Entity creation results |
||
130 | */ |
||
131 | public function createOne($moduleName, array $params) |
||
153 | |||
154 | /** |
||
155 | * Updates an entity |
||
156 | * @param string $moduleName The name of the module / entity type |
||
157 | * @param array $params Entity data |
||
158 | * @return array Entity update result |
||
159 | */ |
||
160 | public function updateOne($moduleName, $entityID, array $params) |
||
197 | |||
198 | /** |
||
199 | * Provides entity removal functionality |
||
200 | * @param string $moduleName The name of the module / entity type |
||
201 | * @param string $entityID The ID of the entity to delete |
||
202 | * @return array Removal status object |
||
203 | */ |
||
204 | public function deleteOne($moduleName, $entityID) |
||
210 | |||
211 | /** |
||
212 | * Retrieves multiple records using module name and a set of constraints |
||
213 | * @param string $moduleName The name of the module / entity type |
||
214 | * @param array $params Data used to find matching entries |
||
215 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
216 | * @return integer $limit Limit the list of entries to N records (acts like LIMIT in SQL) |
||
217 | * @return integer $offset Integer values to specify the offset of the query |
||
218 | * @return array The array containing matching entries or false if nothing was found |
||
219 | */ |
||
220 | public function findMany($moduleName, array $params, array $select = [ ], $limit = 0, $offset = 0) |
||
240 | |||
241 | /** |
||
242 | * Sync will return a sync result object containing details of changes after modifiedTime |
||
243 | * @param integer [$modifiedTime = null] The date of the first change |
||
244 | * @param string [$moduleName = null] The name of the module / entity type |
||
245 | * @param string [$syncType = null] Sync type determines the scope of the query |
||
246 | * @return array Sync result object |
||
247 | */ |
||
248 | public function sync($modifiedTime = null, $moduleName = null, $syncType = null) |
||
268 | |||
269 | /** |
||
270 | * Builds the query using the supplied parameters |
||
271 | * @access public |
||
272 | * @static |
||
273 | * @param string $moduleName The name of the module / entity type |
||
274 | * @param array $params Data used to find matching entries |
||
275 | * @return string $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
276 | * @return integer $limit Limit the list of entries to N records (acts like LIMIT in SQL) |
||
277 | * @return integer $offset Integer values to specify the offset of the query |
||
278 | * @return string The query build out of the supplied parameters |
||
279 | */ |
||
280 | public static function getQueryString($moduleName, array $params, array $select = [ ], $limit = 0, $offset = 0) |
||
302 | } |
||
303 |