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:
| 1 | <?php | ||
| 12 | class ModelService extends AbstractService { | ||
| 13 | |||
| 14 | private $models = null; | ||
|  | |||
| 15 | private $schema = null; | ||
| 16 | private $namespace = 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. database/schema.xml | ||
| 26 | * 3. core/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 . '/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() { | ||
| 63 | $vendorName = $this->packageService->getPackage()->getVendor(); | ||
| 64 | return $this->getSchema() !== null && ($this->isCoreSchema() ? $vendorName == 'keeko' : true); | ||
| 65 | } | ||
| 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 | * Checks whether the given model exists | ||
| 154 | * | ||
| 155 | * @param String $name tableName or modelName | ||
| 156 | * @return boolean | ||
| 157 | */ | ||
| 158 | 8 | 	public function hasModel($name) { | |
| 161 | |||
| 162 | /** | ||
| 163 | * Returns the root namespace for this package | ||
| 164 | * | ||
| 165 | * @return string the namespace | ||
| 166 | */ | ||
| 167 | 1 | 	public function getRootNamespace() { | |
| 183 | |||
| 184 | /** | ||
| 185 | * Retrieves the model name for the given package in two steps: | ||
| 186 | * | ||
| 187 | * 1. Check if it is passed as cli parameter | ||
| 188 | * 2. Retrieve it from the package name | ||
| 189 | * | ||
| 190 | * @return String | ||
| 191 | */ | ||
| 192 | 	public function getModelName() { | ||
| 193 | $input = $this->io->getInput(); | ||
| 194 | 		$modelName = $input->hasOption('model') ? $input->getOption('model') : null; | ||
| 195 | 		if ($modelName === null && $this->isCoreSchema()) { | ||
| 196 | $package = $this->service->getPackageService()->getPackage(); | ||
| 197 | $packageName = $package->getName(); | ||
| 198 | |||
| 199 | 			if ($this->hasModel($packageName)) { | ||
| 200 | $modelName = $packageName; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | return $modelName; | ||
| 204 | } | ||
| 205 | |||
| 206 | /** | ||
| 207 | * Parses the model name from a given action name | ||
| 208 | * | ||
| 209 | * @param ActionSchema $action | ||
| 210 | * @return String modelName | ||
| 211 | */ | ||
| 212 | 10 | View Code Duplication | 	public function getModelNameByAction(ActionSchema $action) { | 
| 213 | 10 | $actionName = $action->getName(); | |
| 214 | 10 | $modelName = null; | |
| 215 | 10 | 		if (($pos = strpos($actionName, '-')) !== false) { | |
| 216 | 10 | $modelName = substr($actionName, 0, $pos); | |
| 217 | 10 | } | |
| 218 | 10 | return $modelName; | |
| 219 | } | ||
| 220 | |||
| 221 | /** | ||
| 222 | * Returns the full model object name, including namespace | ||
| 223 | * | ||
| 224 | * @param ActionSchema $action | ||
| 225 | * @return String fullModelObjectName | ||
| 226 | */ | ||
| 227 | 5 | 	public function getFullModelObjectName(ActionSchema $action) { | |
| 235 | |||
| 236 | /** | ||
| 237 | * Returns the operation (verb) of the action (if existent) | ||
| 238 | * | ||
| 239 | * @param ActionSchema $action | ||
| 240 | * @return string|null | ||
| 241 | */ | ||
| 242 | View Code Duplication | 	public function getOperationByAction(ActionSchema $action) { | |
| 250 | |||
| 251 | /** | ||
| 252 | * Returns wether the given action refers to a model. | ||
| 253 | * | ||
| 254 | * Examples: | ||
| 255 | * | ||
| 256 | * Action: user-create => model: user | ||
| 257 | * Action: recover-password => no model | ||
| 258 | * | ||
| 259 | * @param ActionSchema $action | ||
| 260 | * @return boolean | ||
| 261 | */ | ||
| 262 | 5 | 	public function isModelAction(ActionSchema $action) { | |
| 266 | |||
| 267 | /** | ||
| 268 | * Returns whether this is a crud operation action | ||
| 269 | * (create, read, update, delete, list) | ||
| 270 | * | ||
| 271 | * @param ActionSchema $action | ||
| 272 | * @return boolean | ||
| 273 | */ | ||
| 274 | 	public function isCrudAction(ActionSchema $action) { | ||
| 279 | } | ||
| 280 | 
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.