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 ProjectModel 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 ProjectModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ProjectModel extends Model |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * SQL table name for projects. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | const TABLE = 'projects'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Value for active project. |
||
| 32 | * |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | const ACTIVE = 1; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Value for inactive project. |
||
| 39 | * |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | const INACTIVE = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Value for private project. |
||
| 46 | * |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | const TYPE_PRIVATE = 1; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Value for team project. |
||
| 53 | * |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | const TYPE_TEAM = 0; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get a project by the id. |
||
| 60 | * |
||
| 61 | * @param int $project_id Project id |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function getById($project_id) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get a project by id with owner name. |
||
| 72 | * |
||
| 73 | * @param int $project_id Project id |
||
| 74 | * |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function getByIdWithOwner($project_id) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get a project by the name. |
||
| 88 | * |
||
| 89 | * @param string $name Project name |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | public function getByName($name) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get a project by the identifier (code). |
||
| 100 | * |
||
| 101 | * @param string $identifier |
||
| 102 | * |
||
| 103 | * @return array|bool |
||
| 104 | */ |
||
| 105 | public function getByIdentifier($identifier) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Fetch project data by using the token. |
||
| 116 | * |
||
| 117 | * @param string $token Token |
||
| 118 | * |
||
| 119 | * @return array|bool |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function getByToken($token) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Return the first project from the database (no sorting). |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | public function getFirst() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Return true if the project is private. |
||
| 142 | * |
||
| 143 | * @param int $project_id Project id |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function isPrivate($project_id) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get all projects. |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function getAll() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get all projects with given Ids. |
||
| 164 | * |
||
| 165 | * @param int[] $project_ids |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | public function getAllByIds(array $project_ids) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get all project ids. |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public function getAllIds() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Return the list of all projects. |
||
| 190 | * |
||
| 191 | * @param bool $prepend If true, prepend to the list the value 'None' |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function getList($prepend = true) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get all projects with all its data for a given status. |
||
| 206 | * |
||
| 207 | * @param int $status Project status: self::ACTIVE or self:INACTIVE |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function getAllByStatus($status) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get a list of project by status. |
||
| 222 | * |
||
| 223 | * @param int $status Project status: self::ACTIVE or self:INACTIVE |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function getListByStatus($status) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Return the number of projects by status. |
||
| 238 | * |
||
| 239 | * @param int $status Status: self::ACTIVE or self:INACTIVE |
||
| 240 | * |
||
| 241 | * @return int |
||
| 242 | */ |
||
| 243 | public function countByStatus($status) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Gather some task metrics for a given project. |
||
| 253 | * |
||
| 254 | * @param int $project_id Project id |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | public function getTaskStats($project_id) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get stats for each column of a project. |
||
| 279 | * |
||
| 280 | * @param array $project |
||
| 281 | * |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | public function getColumnStats(array &$project) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Apply column stats to a collection of projects (filter callback). |
||
| 300 | * |
||
| 301 | * @param array $projects |
||
| 302 | * |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public function applyColumnStats(array $projects) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get project summary for a list of project. |
||
| 316 | * |
||
| 317 | * @param array $project_ids List of project id |
||
| 318 | * |
||
| 319 | * @return \PicoDb\Table |
||
| 320 | */ |
||
| 321 | public function getQueryColumnStats(array $project_ids) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Create a project. |
||
| 337 | * |
||
| 338 | * @param array $values Form values |
||
| 339 | * @param int $user_id User who create the project |
||
| 340 | * @param bool $add_user Automatically add the user |
||
| 341 | * |
||
| 342 | * @return int Project id |
||
| 343 | */ |
||
| 344 | public function create(array $values, $user_id = 0, $add_user = false) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Check if the project have been modified. |
||
| 386 | * |
||
| 387 | * @param int $project_id Project id |
||
| 388 | * @param int $timestamp Timestamp |
||
| 389 | * |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | public function isModifiedSince($project_id, $timestamp) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Update modification date. |
||
| 402 | * |
||
| 403 | * @param int $project_id Project id |
||
| 404 | * |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | public function updateModificationDate($project_id) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Update a project. |
||
| 416 | * |
||
| 417 | * @param array $values Form values |
||
| 418 | * |
||
| 419 | * @return bool |
||
| 420 | */ |
||
| 421 | public function update(array $values) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Remove a project. |
||
| 443 | * |
||
| 444 | * @param int $project_id Project id |
||
| 445 | * |
||
| 446 | * @return bool |
||
| 447 | */ |
||
| 448 | public function remove($project_id) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Return true if the project exists. |
||
| 455 | * |
||
| 456 | * @param int $project_id Project id |
||
| 457 | * |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function exists($project_id) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Enable a project. |
||
| 467 | * |
||
| 468 | * @param int $project_id Project id |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | View Code Duplication | public function enable($project_id) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Disable a project. |
||
| 483 | * |
||
| 484 | * @param int $project_id Project id |
||
| 485 | * |
||
| 486 | * @return bool |
||
| 487 | */ |
||
| 488 | View Code Duplication | public function disable($project_id) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Enable public access for a project. |
||
| 499 | * |
||
| 500 | * @param int $project_id Project id |
||
| 501 | * |
||
| 502 | * @return bool |
||
| 503 | */ |
||
| 504 | View Code Duplication | public function enablePublicAccess($project_id) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Disable public access for a project. |
||
| 515 | * |
||
| 516 | * @param int $project_id Project id |
||
| 517 | * |
||
| 518 | * @return bool |
||
| 519 | */ |
||
| 520 | View Code Duplication | public function disablePublicAccess($project_id) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Get available views. |
||
| 531 | * |
||
| 532 | * @param bool $prepend Prepend a default value |
||
| 533 | * |
||
| 534 | * @return array |
||
| 535 | */ |
||
| 536 | public function getViews($prepend = false) |
||
| 553 | } |
||
| 554 |
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.