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 |
||
30 | trait QueryTrait |
||
31 | { |
||
32 | /** |
||
33 | * Returns public users. |
||
34 | * |
||
35 | * @return Eloquent\Collection |
||
36 | */ |
||
37 | 4 | public function activeUsers() |
|
43 | |||
44 | /** |
||
45 | * Returns user projects with activities details eager loaded. |
||
46 | * |
||
47 | * @param int $status |
||
48 | * |
||
49 | * @return Relations\HasMany |
||
50 | */ |
||
51 | 10 | public function projectsWidthActivities($status = Project::STATUS_OPEN) |
|
61 | |||
62 | /** |
||
63 | * Returns projects with issues details eager loaded. |
||
64 | * |
||
65 | * @param int $status |
||
66 | * |
||
67 | * @return Relations\HasMany |
||
68 | */ |
||
69 | 1 | public function projectsWidthIssues($status = Project::STATUS_OPEN) |
|
87 | |||
88 | /** |
||
89 | * Returns collection of issues grouped by tags. |
||
90 | * |
||
91 | * @param $tagIds |
||
92 | * |
||
93 | * @return mixed |
||
94 | */ |
||
95 | public function issuesGroupByTags($tagIds) |
||
111 | |||
112 | /** |
||
113 | * Load user permissions. |
||
114 | * |
||
115 | * @return Eloquent\Collection |
||
116 | */ |
||
117 | 60 | protected function loadPermissions() |
|
125 | } |
||
126 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.