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 |
||
| 12 | class ModelService extends AbstractService { |
||
| 13 | |||
| 14 | private $models = null; |
||
| 15 | private $schema = null; |
||
| 16 | |||
| 17 | /** @var Database */ |
||
| 18 | private $database = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Returns the propel schema. The three locations, where the schema is looked up in: |
||
| 22 | * |
||
| 23 | * 1. --schema option (if available) |
||
| 24 | * 2. database/schema.xml |
||
| 25 | * 3. core/database/schema.xml |
||
| 26 | * |
||
| 27 | * @throws \RuntimeException |
||
| 28 | * @return string the path to the schema |
||
| 29 | */ |
||
| 30 | public function getSchema() { |
||
| 31 | 12 | $input = $this->io->getInput(); |
|
| 32 | 12 | if ($this->schema === null) { |
|
| 33 | 12 | $workDir = $this->service->getProject()->getRootPath(); |
|
| 34 | 12 | $schema = null; |
|
| 35 | 12 | $schemas = [ |
|
| 36 | $input->hasOption('schema') ? $input->getOption('schema') : '', |
||
| 37 | 12 | $workDir . '/database/schema.xml', |
|
| 38 | 12 | $workDir . '/core/database/schema.xml', |
|
| 39 | $workDir . '/vendor/keeko/core/database/schema.xml' |
||
| 40 | 12 | ]; |
|
| 41 | 12 | foreach ($schemas as $path) { |
|
| 42 | 12 | if (file_exists($path)) { |
|
| 43 | 12 | $schema = $path; |
|
| 44 | 12 | break; |
|
| 45 | } |
||
| 46 | 12 | } |
|
| 47 | 12 | $this->schema = $schema; |
|
| 48 | |||
| 49 | 12 | if ($schema === null) { |
|
| 50 | $locations = implode(', ', $schemas); |
||
| 51 | throw new \RuntimeException(sprintf('Can\'t find schema in these locations: %s', $locations)); |
||
| 52 | } |
||
| 53 | 12 | } |
|
| 54 | |||
| 55 | 12 | return $this->schema; |
|
| 56 | } |
||
| 57 | |||
| 58 | 3 | public function isCoreSchema() { |
|
| 61 | |||
| 62 | public function hasSchema() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the propel database |
||
| 69 | * |
||
| 70 | * @return Database |
||
| 71 | */ |
||
| 72 | 12 | public function getDatabase() { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Returns the tableName for a given name |
||
| 84 | * |
||
| 85 | * @param String $name tableName or modelName |
||
| 86 | * @return String tableName |
||
| 87 | */ |
||
| 88 | 11 | public function getTableName($name) { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Returns all model names |
||
| 99 | * |
||
| 100 | * @return String[] an array of modelName |
||
| 101 | */ |
||
| 102 | public function getModelNames() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns the propel models from the database, where table namespace matches package namespace |
||
| 114 | * |
||
| 115 | * @return ArrayList<Table> |
||
|
1 ignored issue
–
show
|
|||
| 116 | */ |
||
| 117 | 1 | public function getModels() { |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the model for the given name |
||
| 136 | * |
||
| 137 | * @param String $name modelName or tableName |
||
| 138 | * @return Table |
||
| 139 | */ |
||
| 140 | 7 | public function getModel($name) { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Returns the model names for a given package |
||
| 154 | * |
||
| 155 | * @param PackageSchema $package a package to search models for, if omitted global package is used |
||
| 156 | * @return array array with string of model names |
||
| 157 | */ |
||
| 158 | 8 | public function getPackageModelNames(PackageSchema $package = null) { |
|
| 181 | 1 | ||
| 182 | /** |
||
| 183 | * Checks whether the given model exists |
||
| 184 | * |
||
| 185 | * @param String $name tableName or modelName |
||
| 186 | * @return boolean |
||
| 187 | */ |
||
| 188 | public function hasModel($name) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Retrieves the model name for the given package in two steps: |
||
| 194 | * |
||
| 195 | * 1. Check if it is passed as cli parameter |
||
| 196 | * 2. Retrieve it from the package name |
||
| 197 | * |
||
| 198 | * @return String |
||
| 199 | */ |
||
| 200 | public function getModelName() { |
||
| 213 | 10 | ||
| 214 | 10 | /** |
|
| 215 | 10 | * Parses the model name from a given action name |
|
| 216 | 10 | * |
|
| 217 | 10 | * @param ActionSchema $action |
|
| 218 | 10 | * @return String modelName |
|
| 219 | */ |
||
| 220 | public function getModelNameByAction(ActionSchema $action) { |
||
| 228 | 5 | ||
| 229 | 5 | /** |
|
| 230 | 5 | * Returns the full model object name, including namespace |
|
| 231 | 5 | * |
|
| 232 | * @param ActionSchema $action |
||
| 233 | 5 | * @return String fullModelObjectName |
|
| 234 | */ |
||
| 235 | public function getFullModelObjectName(ActionSchema $action) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the operation (verb) of the action (if existent) |
||
| 246 | * |
||
| 247 | * @param ActionSchema $action |
||
| 248 | * @return string|null |
||
| 249 | */ |
||
| 250 | public function getOperationByAction(ActionSchema $action) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns wether the given action refers to a model. |
||
| 261 | * |
||
| 262 | 5 | * Examples: |
|
| 263 | 5 | * |
|
| 264 | 5 | * Action: user-create => model: user |
|
| 265 | * Action: recover-password => no model |
||
| 266 | * |
||
| 267 | * @param ActionSchema $action |
||
| 268 | * @return boolean |
||
| 269 | */ |
||
| 270 | public function isModelAction(ActionSchema $action) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns whether this is a crud operation action |
||
| 277 | * (create, read, update, delete, list) |
||
| 278 | * |
||
| 279 | * @param ActionSchema $action |
||
| 280 | * @return boolean |
||
| 281 | */ |
||
| 282 | public function isCrudAction(ActionSchema $action) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Returns all model relationships. |
||
| 290 | * |
||
| 291 | * |
||
| 292 | * The returned array looks like: |
||
| 293 | * [ |
||
| 294 | * 'one' => [ |
||
| 295 | * [ |
||
| 296 | * 'type' => 'one', |
||
| 297 | * 'fk' => $fk |
||
| 298 | * ], |
||
| 299 | * [ |
||
| 300 | * 'type' => 'one', |
||
| 301 | * 'fk' => $fk2 |
||
| 302 | * ], |
||
| 303 | * ... |
||
| 304 | * ], |
||
| 305 | * 'many' => [ |
||
| 306 | * [ |
||
| 307 | * 'type' => 'many', |
||
| 308 | * 'fk' => $fk3, |
||
| 309 | * 'cfk' => $cfk |
||
| 310 | * ], |
||
| 311 | * [ |
||
| 312 | * 'type' => 'many', |
||
| 313 | * 'fk' => $fk4, |
||
| 314 | * 'cfk' => $cfk2 |
||
| 315 | * ], |
||
| 316 | * ... |
||
| 317 | * ], |
||
| 318 | * 'all' => [...] // both of above |
||
| 319 | * ] |
||
| 320 | * |
||
| 321 | * |
||
| 322 | * @param Table $model |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | public function getRelationships(Table $model) { |
||
| 371 | } |
||
| 372 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.