Complex classes like ModelService 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 ModelService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class ModelService extends AbstractService { |
||
18 | |||
19 | private $models = null; |
||
20 | private $schema = null; |
||
21 | |||
22 | /** @var Database */ |
||
23 | private $database = null; |
||
24 | |||
25 | private $relationships = null; |
||
26 | |||
27 | /** |
||
28 | * Returns the propel schema. The three locations, where the schema is looked up in: |
||
29 | * |
||
30 | * 1. --schema option (if available) |
||
31 | 12 | * 2. res/database/schema.xml |
|
32 | 12 | * 3. <keeko/core>/res/database/schema.xml |
|
33 | 12 | * |
|
34 | 12 | * @throws \RuntimeException |
|
35 | 12 | * @return string the path to the schema |
|
36 | */ |
||
37 | 12 | public function getSchema() { |
|
65 | |||
66 | public function isCoreSchema() { |
||
69 | |||
70 | public function hasSchema() { |
||
74 | 12 | ||
75 | 12 | /** |
|
76 | 12 | * Returns the propel database |
|
77 | 12 | * |
|
78 | * @return Database |
||
79 | 12 | */ |
|
80 | public function getDatabase() { |
||
89 | 11 | ||
90 | 11 | /** |
|
91 | 11 | * Returns the tableName for a given name |
|
92 | 11 | * |
|
93 | * @param String $name tableName or modelName |
||
94 | 11 | * @return String tableName |
|
95 | */ |
||
96 | public function getTableName($name) { |
||
104 | |||
105 | /** |
||
106 | * Returns all model names |
||
107 | * |
||
108 | * @return String[] an array of modelName |
||
109 | */ |
||
110 | public function getModelNames() { |
||
119 | 1 | ||
120 | 1 | /** |
|
121 | * Returns the propel models from the database, where table namespace matches package namespace |
||
122 | 1 | * |
|
123 | * @return ArrayList<Table> |
||
124 | 1 | */ |
|
125 | 1 | public function getModels() { |
|
141 | 7 | ||
142 | 7 | /** |
|
143 | * Returns the model for the given name |
||
144 | * |
||
145 | * @param String $name modelName or tableName |
||
146 | * @return Table |
||
147 | 7 | */ |
|
148 | public function getModel($name) { |
||
155 | |||
156 | /** |
||
157 | * Returns the model names for a given package |
||
158 | 8 | * |
|
159 | 8 | * @param PackageSchema $package a package to search models for, if omitted global package is used |
|
160 | * @return array array with string of model names |
||
161 | */ |
||
162 | public function getPackageModelNames(PackageSchema $package = null) { |
||
185 | |||
186 | /** |
||
187 | * Checks whether the given model exists |
||
188 | * |
||
189 | * @param String $name tableName or modelName |
||
190 | * @return boolean |
||
191 | */ |
||
192 | public function hasModel($name) { |
||
195 | |||
196 | /** |
||
197 | * Retrieves the model name for the given package in two steps: |
||
198 | * |
||
199 | * 1. Check if it is passed as cli parameter |
||
200 | * 2. Retrieve it from the package name |
||
201 | * |
||
202 | * @return String |
||
203 | */ |
||
204 | public function getModelName() { |
||
217 | 10 | ||
218 | 10 | /** |
|
219 | * Parses the model name from a given action name |
||
220 | * |
||
221 | * @param ActionSchema $action |
||
222 | * @return String modelName |
||
223 | */ |
||
224 | public function getModelNameByAction(ActionSchema $action) { |
||
232 | |||
233 | 5 | /** |
|
234 | * Returns the full model object name, including namespace |
||
235 | * |
||
236 | * @param ActionSchema $action |
||
237 | * @return String fullModelObjectName |
||
238 | */ |
||
239 | public function getFullModelObjectName(ActionSchema $action) { |
||
247 | |||
248 | /** |
||
249 | * Returns the operation (verb) of the action (if existent) |
||
250 | * |
||
251 | * @param ActionSchema $action |
||
252 | * @return string|null |
||
253 | */ |
||
254 | public function getOperationByAction(ActionSchema $action) { |
||
262 | 5 | ||
263 | 5 | /** |
|
264 | 5 | * Returns wether the given action refers to a model. |
|
265 | * |
||
266 | * Examples: |
||
267 | * |
||
268 | * Action: user-create => model: user |
||
269 | * Action: recover-password => no model |
||
270 | * |
||
271 | * @param ActionSchema $action |
||
272 | * @return boolean |
||
273 | */ |
||
274 | public function isModelAction(ActionSchema $action) { |
||
278 | |||
279 | /** |
||
280 | * Returns whether this is a crud operation action |
||
281 | * (create, read, update, delete, list) |
||
282 | * |
||
283 | * @param ActionSchema $action |
||
284 | * @return boolean |
||
285 | */ |
||
286 | public function isCrudAction(ActionSchema $action) { |
||
291 | |||
292 | /** |
||
293 | * Returns all model relationships. |
||
294 | * |
||
295 | * @param Table $model |
||
296 | * @return Relationships |
||
297 | */ |
||
298 | public function getRelationships(Table $model) { |
||
344 | |||
345 | /** |
||
346 | * Returns a relationship for a given foreign name on a given model |
||
347 | * |
||
348 | * @param Table $model |
||
349 | * @param string $foreignName |
||
350 | * @return Relationship |
||
351 | */ |
||
352 | public function getRelationship(Table $model, $foreignName) { |
||
356 | } |
||
357 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: