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 array The array containing matching entries or false if nothing was found |
||
217 | */ |
||
218 | public function findMany($moduleName, array $params, array $select = [ ], $limit = 0) |
||
219 | { |
||
220 | if (!is_array($params) || (!empty($params) && !is_assoc_array($params))) { |
||
221 | throw new WSException( |
||
222 | "You have to specify at least one search parameter (prop => value) |
||
223 | in order to be able to retrieve entity(ies)" |
||
224 | ); |
||
225 | } |
||
226 | |||
227 | // Builds the query |
||
228 | $query = self::getQueryString($moduleName, $params, $select, $limit); |
||
229 | |||
230 | // Run the query |
||
231 | $records = $this->wsClient->runQuery($query); |
||
232 | if (false === $records || !is_array($records) || empty($records)) { |
||
233 | return null; |
||
234 | } |
||
235 | |||
236 | return $records; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Sync will return a sync result object containing details of changes after modifiedTime |
||
241 | * @param integer [$modifiedTime = null] The date of the first change |
||
242 | * @param string [$moduleName = null] The name of the module / entity type |
||
243 | * @return array Sync result object |
||
244 | */ |
||
245 | public function sync($modifiedTime = null, $moduleName = null) |
||
261 | |||
262 | /** |
||
263 | * Builds the query using the supplied parameters |
||
264 | * @access public |
||
265 | * @static |
||
266 | * @param string $moduleName The name of the module / entity type |
||
267 | * @param array $params Data used to find matching entries |
||
268 | * @return string $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
269 | * @return string $limit limit the list of entries to N records (acts like LIMIT in SQL) |
||
270 | * @return string The query build out of the supplied parameters |
||
271 | */ |
||
272 | public static function getQueryString($moduleName, array $params, array $select = [ ], $limit = 0) |
||
293 | } |
||
294 |
Adding braces to control structures avoids accidental mistakes as your code changes: