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 |
||
| 13 | class ModelService extends AbstractService { |
||
| 14 | |||
| 15 | private $models = null; |
||
| 16 | private $schema = null; |
||
| 17 | |||
| 18 | /** @var Database */ |
||
| 19 | private $database = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Returns the propel schema. The three locations, where the schema is looked up in: |
||
| 23 | * |
||
| 24 | * 1. --schema option (if available) |
||
| 25 | * 2. res/database/schema.xml |
||
| 26 | * 3. <keeko/core>/res/database/schema.xml |
||
| 27 | * |
||
| 28 | * @throws \RuntimeException |
||
| 29 | * @return string the path to the schema |
||
| 30 | */ |
||
| 31 | 12 | public function getSchema() { |
|
| 32 | 12 | $input = $this->io->getInput(); |
|
| 33 | 12 | if ($this->schema === null) { |
|
| 34 | 12 | $workDir = $this->service->getProject()->getRootPath(); |
|
| 35 | 12 | $schema = null; |
|
| 36 | $schemas = [ |
||
| 37 | 12 | $input->hasOption('schema') ? $input->getOption('schema') : '', |
|
| 38 | 12 | $workDir . '/database/schema.xml', |
|
| 39 | $workDir . '/res/database/schema.xml', |
||
| 40 | 12 | $workDir . '/vendor/keeko/core/database/schema.xml', |
|
| 41 | 12 | $workDir . '/vendor/keeko/core/res/database/schema.xml' |
|
| 42 | 12 | ]; |
|
| 43 | 12 | foreach ($schemas as $path) { |
|
| 44 | 12 | if (file_exists($path)) { |
|
| 45 | $schema = $path; |
||
| 46 | 12 | break; |
|
| 47 | 12 | } |
|
| 48 | } |
||
| 49 | 12 | $this->schema = $schema; |
|
| 50 | |||
| 51 | if ($schema === null) { |
||
| 52 | $locations = implode(', ', $schemas); |
||
| 53 | 12 | throw new \RuntimeException(sprintf('Can\'t find schema in these locations: %s', $locations)); |
|
| 54 | } |
||
| 55 | 12 | } |
|
| 56 | |||
| 57 | return $this->schema; |
||
| 58 | 3 | } |
|
| 59 | 3 | ||
| 60 | public function isCoreSchema() { |
||
| 63 | |||
| 64 | public function hasSchema() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns the propel database |
||
| 71 | * |
||
| 72 | 12 | * @return Database |
|
| 73 | 12 | */ |
|
| 74 | 12 | public function getDatabase() { |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Returns the tableName for a given name |
||
| 86 | * |
||
| 87 | * @param String $name tableName or modelName |
||
| 88 | 11 | * @return String tableName |
|
| 89 | 11 | */ |
|
| 90 | 11 | public function getTableName($name) { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns all model names |
||
| 101 | * |
||
| 102 | * @return String[] an array of modelName |
||
| 103 | */ |
||
| 104 | public function getModelNames() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns the propel models from the database, where table namespace matches package namespace |
||
| 116 | * |
||
| 117 | 1 | * @return ArrayList<Table> |
|
| 118 | 1 | */ |
|
| 119 | 1 | public function getModels() { |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the model for the given name |
||
| 138 | * |
||
| 139 | * @param String $name modelName or tableName |
||
| 140 | 7 | * @return Table |
|
| 141 | 7 | */ |
|
| 142 | 7 | public function getModel($name) { |
|
| 149 | 7 | ||
| 150 | /** |
||
| 151 | * Returns the model names for a given package |
||
| 152 | * |
||
| 153 | * @param PackageSchema $package a package to search models for, if omitted global package is used |
||
| 154 | * @return array array with string of model names |
||
| 155 | */ |
||
| 156 | public function getPackageModelNames(PackageSchema $package = null) { |
||
| 179 | 1 | ||
| 180 | /** |
||
| 181 | 1 | * Checks whether the given model exists |
|
| 182 | * |
||
| 183 | * @param String $name tableName or modelName |
||
| 184 | * @return boolean |
||
| 185 | */ |
||
| 186 | public function hasModel($name) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Retrieves the model name for the given package in two steps: |
||
| 192 | * |
||
| 193 | * 1. Check if it is passed as cli parameter |
||
| 194 | * 2. Retrieve it from the package name |
||
| 195 | * |
||
| 196 | * @return String |
||
| 197 | */ |
||
| 198 | public function getModelName() { |
||
| 211 | |||
| 212 | 10 | /** |
|
| 213 | 10 | * Parses the model name from a given action name |
|
| 214 | 10 | * |
|
| 215 | 10 | * @param ActionSchema $action |
|
| 216 | 10 | * @return String modelName |
|
| 217 | 10 | */ |
|
| 218 | 10 | public function getModelNameByAction(ActionSchema $action) { |
|
| 226 | |||
| 227 | 5 | /** |
|
| 228 | 5 | * Returns the full model object name, including namespace |
|
| 229 | 5 | * |
|
| 230 | 5 | * @param ActionSchema $action |
|
| 231 | 5 | * @return String fullModelObjectName |
|
| 232 | */ |
||
| 233 | 5 | public function getFullModelObjectName(ActionSchema $action) { |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Returns the operation (verb) of the action (if existent) |
||
| 244 | * |
||
| 245 | * @param ActionSchema $action |
||
| 246 | * @return string|null |
||
| 247 | */ |
||
| 248 | public function getOperationByAction(ActionSchema $action) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Returns wether the given action refers to a model. |
||
| 259 | * |
||
| 260 | * Examples: |
||
| 261 | * |
||
| 262 | 5 | * Action: user-create => model: user |
|
| 263 | 5 | * Action: recover-password => no model |
|
| 264 | 5 | * |
|
| 265 | * @param ActionSchema $action |
||
| 266 | * @return boolean |
||
| 267 | */ |
||
| 268 | public function isModelAction(ActionSchema $action) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Returns whether this is a crud operation action |
||
| 275 | * (create, read, update, delete, list) |
||
| 276 | * |
||
| 277 | * @param ActionSchema $action |
||
| 278 | * @return boolean |
||
| 279 | */ |
||
| 280 | public function isCrudAction(ActionSchema $action) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns all model relationships. |
||
| 288 | * |
||
| 289 | * |
||
| 290 | * The returned array looks like: |
||
| 291 | * [ |
||
| 292 | * 'one' => [ |
||
| 293 | * [ |
||
| 294 | * 'type' => 'one', |
||
| 295 | * 'fk' => $fk |
||
| 296 | * ], |
||
| 297 | * [ |
||
| 298 | * 'type' => 'one', |
||
| 299 | * 'fk' => $fk2 |
||
| 300 | * ], |
||
| 301 | * ... |
||
| 302 | * ], |
||
| 303 | * 'many' => [ |
||
| 304 | * [ |
||
| 305 | * 'type' => 'many', |
||
| 306 | * 'fk' => $fk3, |
||
| 307 | * 'cfk' => $cfk |
||
| 308 | * ], |
||
| 309 | * [ |
||
| 310 | * 'type' => 'many', |
||
| 311 | * 'fk' => $fk4, |
||
| 312 | * 'cfk' => $cfk2 |
||
| 313 | * ], |
||
| 314 | * ... |
||
| 315 | * ], |
||
| 316 | * 'all' => [...] // both of above |
||
| 317 | * ] |
||
| 318 | * |
||
| 319 | * |
||
| 320 | * @param Table $model |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | public function getRelationships(Table $model) { |
||
| 388 | } |
||
| 389 |