Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | private $namespace = null;  | 
            ||
| 18 | |||
| 19 | /** @var Database */  | 
            ||
| 20 | private $database = null;  | 
            ||
| 21 | |||
| 22 | /**  | 
            ||
| 23 | * Returns the propel schema. The three locations, where the schema is looked up in:  | 
            ||
| 24 | *  | 
            ||
| 25 | * 1. --schema option (if available)  | 
            ||
| 26 | * 2. database/schema.xml  | 
            ||
| 27 | * 3. core/database/schema.xml  | 
            ||
| 28 | *  | 
            ||
| 29 | * @throws \RuntimeException  | 
            ||
| 30 | * @return string the path to the schema  | 
            ||
| 31 | 12 | */  | 
            |
| 32 | 12 | 	public function getSchema() { | 
            |
| 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>  | 
            |
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 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) { | 
            |
| 153 | |||
| 154 | /**  | 
            ||
| 155 | * Returns the model names for a given package  | 
            ||
| 156 | *  | 
            ||
| 157 | * @param PackageSchema $package a package to search models for, if omitted global package is used  | 
            ||
| 158 | 8 | * @return array array with string of model names  | 
            |
| 159 | 8 | */  | 
            |
| 160 | 	public function getPackageModelNames(PackageSchema $package = null) { | 
            ||
| 183 | |||
| 184 | /**  | 
            ||
| 185 | * Checks whether the given model exists  | 
            ||
| 186 | *  | 
            ||
| 187 | * @param String $name tableName or modelName  | 
            ||
| 188 | * @return boolean  | 
            ||
| 189 | */  | 
            ||
| 190 | 	public function hasModel($name) { | 
            ||
| 193 | |||
| 194 | /**  | 
            ||
| 195 | * Returns the root namespace for this package  | 
            ||
| 196 | *  | 
            ||
| 197 | * @return string the namespace  | 
            ||
| 198 | */  | 
            ||
| 199 | 	public function getRootNamespace() { | 
            ||
| 215 | 10 | ||
| 216 | 10 | /**  | 
            |
| 217 | 10 | * Retrieves the model name for the given package in two steps:  | 
            |
| 218 | 10 | *  | 
            |
| 219 | * 1. Check if it is passed as cli parameter  | 
            ||
| 220 | * 2. Retrieve it from the package name  | 
            ||
| 221 | *  | 
            ||
| 222 | * @return String  | 
            ||
| 223 | */  | 
            ||
| 224 | 	public function getModelName() { | 
            ||
| 237 | |||
| 238 | /**  | 
            ||
| 239 | * Parses the model name from a given action name  | 
            ||
| 240 | *  | 
            ||
| 241 | * @param ActionSchema $action  | 
            ||
| 242 | * @return String modelName  | 
            ||
| 243 | */  | 
            ||
| 244 | View Code Duplication | 	public function getModelNameByAction(ActionSchema $action) { | 
            |
| 252 | |||
| 253 | /**  | 
            ||
| 254 | * Returns the full model object name, including namespace  | 
            ||
| 255 | *  | 
            ||
| 256 | * @param ActionSchema $action  | 
            ||
| 257 | * @return String fullModelObjectName  | 
            ||
| 258 | */  | 
            ||
| 259 | 	public function getFullModelObjectName(ActionSchema $action) { | 
            ||
| 267 | |||
| 268 | /**  | 
            ||
| 269 | * Returns the operation (verb) of the action (if existent)  | 
            ||
| 270 | *  | 
            ||
| 271 | * @param ActionSchema $action  | 
            ||
| 272 | * @return string|null  | 
            ||
| 273 | */  | 
            ||
| 274 | View Code Duplication | 	public function getOperationByAction(ActionSchema $action) { | 
            |
| 282 | |||
| 283 | /**  | 
            ||
| 284 | * Returns wether the given action refers to a model.  | 
            ||
| 285 | *  | 
            ||
| 286 | * Examples:  | 
            ||
| 287 | *  | 
            ||
| 288 | * Action: user-create => model: user  | 
            ||
| 289 | * Action: recover-password => no model  | 
            ||
| 290 | *  | 
            ||
| 291 | * @param ActionSchema $action  | 
            ||
| 292 | * @return boolean  | 
            ||
| 293 | */  | 
            ||
| 294 | 	public function isModelAction(ActionSchema $action) { | 
            ||
| 298 | |||
| 299 | /**  | 
            ||
| 300 | * Returns whether this is a crud operation action  | 
            ||
| 301 | * (create, read, update, delete, list)  | 
            ||
| 302 | *  | 
            ||
| 303 | * @param ActionSchema $action  | 
            ||
| 304 | * @return boolean  | 
            ||
| 305 | */  | 
            ||
| 306 | 	public function isCrudAction(ActionSchema $action) { | 
            ||
| 311 | }  | 
            ||
| 312 | 
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.