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 SwimlaneModel 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 SwimlaneModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class SwimlaneModel extends Model |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * SQL table name. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | const TABLE = 'swimlanes'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Value for active swimlanes. |
||
| 30 | * |
||
| 31 | * @var int |
||
| 32 | */ |
||
| 33 | const ACTIVE = 1; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Value for inactive swimlanes. |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | const INACTIVE = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get a swimlane by the id. |
||
| 44 | * |
||
| 45 | * @param int $swimlane_id Swimlane id |
||
| 46 | * |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | public function getById($swimlane_id) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get the swimlane name by the id. |
||
| 56 | * |
||
| 57 | * @param int $swimlane_id Swimlane id |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function getNameById($swimlane_id) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get a swimlane id by the project and the name. |
||
| 68 | * |
||
| 69 | * @param int $project_id Project id |
||
| 70 | * @param string $name Name |
||
| 71 | * |
||
| 72 | * @return int |
||
| 73 | */ |
||
| 74 | public function getIdByName($project_id, $name) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get a swimlane by the project and the name. |
||
| 84 | * |
||
| 85 | * @param int $project_id Project id |
||
| 86 | * @param string $name Swimlane name |
||
| 87 | * |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | public function getByName($project_id, $name) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get first active swimlane for a project. |
||
| 100 | * |
||
| 101 | * @param int $project_id |
||
| 102 | * |
||
| 103 | * @return array|null |
||
| 104 | */ |
||
| 105 | public function getFirstActiveSwimlane($project_id) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get default swimlane properties. |
||
| 118 | * |
||
| 119 | * @param int $project_id Project id |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function getDefault($project_id) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get all swimlanes for a given project. |
||
| 140 | * |
||
| 141 | * @param int $project_id Project id |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | public function getAll($project_id) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the list of swimlanes by status. |
||
| 156 | * |
||
| 157 | * @param int $project_id Project id |
||
| 158 | * @param int $status Status |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | public function getAllByStatus($project_id, $status = self::ACTIVE) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get active swimlanes. |
||
| 180 | * |
||
| 181 | * @param int $project_id Project id |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | public function getSwimlanes($project_id) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get list of all swimlanes. |
||
| 214 | * |
||
| 215 | * @param int $project_id Project id |
||
| 216 | * @param bool $prepend Prepend default value |
||
| 217 | * @param bool $only_active Return only active swimlanes |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function getList($project_id, $prepend = false, $only_active = false) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Add a new swimlane. |
||
| 244 | * |
||
| 245 | * @param array $values Form values |
||
| 246 | * |
||
| 247 | * @return int|bool |
||
| 248 | */ |
||
| 249 | public function create($values) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Update a swimlane. |
||
| 262 | * |
||
| 263 | * @param array $values Form values |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public function update(array $values) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Update the default swimlane. |
||
| 277 | * |
||
| 278 | * @param array $values Form values |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | public function updateDefault(array $values) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Enable the default swimlane. |
||
| 295 | * |
||
| 296 | * @param int $project_id |
||
| 297 | * |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | public function enableDefault($project_id) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Disable the default swimlane. |
||
| 312 | * |
||
| 313 | * @param int $project_id |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | public function disableDefault($project_id) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get the last position of a swimlane. |
||
| 329 | * |
||
| 330 | * @param int $project_id |
||
| 331 | * |
||
| 332 | * @return int |
||
| 333 | */ |
||
| 334 | public function getLastPosition($project_id) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Disable a swimlane. |
||
| 345 | * |
||
| 346 | * @param int $project_id Project id |
||
| 347 | * @param int $swimlane_id Swimlane id |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | public function disable($project_id, $swimlane_id) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Enable a swimlane. |
||
| 371 | * |
||
| 372 | * @param int $project_id Project id |
||
| 373 | * @param int $swimlane_id Swimlane id |
||
| 374 | * |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | public function enable($project_id, $swimlane_id) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Remove a swimlane. |
||
| 390 | * |
||
| 391 | * @param int $project_id Project id |
||
| 392 | * @param int $swimlane_id Swimlane id |
||
| 393 | * |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | View Code Duplication | public function remove($project_id, $swimlane_id) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Update swimlane positions after disabling or removing a swimlane. |
||
| 419 | * |
||
| 420 | * @param int $project_id Project id |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | public function updatePositions($project_id) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Change swimlane position. |
||
| 450 | * |
||
| 451 | * @param int $project_id |
||
| 452 | * @param int $swimlane_id |
||
| 453 | * @param int $position |
||
| 454 | * |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | View Code Duplication | public function changePosition($project_id, $swimlane_id, $position) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Duplicate Swimlane to project. |
||
| 489 | * |
||
| 490 | * @param int $project_from Project Template |
||
| 491 | * @param int $project_to Project that receives the copy |
||
| 492 | * |
||
| 493 | * @return int|bool |
||
| 494 | */ |
||
| 495 | public function duplicate($project_from, $project_to) |
||
| 515 | } |
||
| 516 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.