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 |
||
32 | trait CrudTrait |
||
33 | { |
||
34 | /** |
||
35 | * removes a user from a project. |
||
36 | * |
||
37 | * @param int $userId |
||
38 | * |
||
39 | * @return mixed |
||
40 | */ |
||
41 | 1 | public function unassignUser($userId) |
|
45 | |||
46 | /** |
||
47 | * Create a new project. |
||
48 | * |
||
49 | * @param array $input |
||
50 | * |
||
51 | * @return $this |
||
52 | */ |
||
53 | 42 | public function createProject(array $input = []) |
|
54 | { |
||
55 | 42 | $this->fill($input)->save(); |
|
56 | |||
57 | 42 | if (!empty($input['columns'])) { |
|
58 | $this->saveTags($input['columns']); |
||
59 | |||
60 | unset($input['columns']); |
||
61 | } |
||
62 | |||
63 | /* Assign selected users to the project */ |
||
64 | 42 | if (isset($input['user']) && count($input['user']) > 0) { |
|
65 | 24 | foreach ($input['user'] as $id) { |
|
66 | 24 | $this->assignUser($id); |
|
67 | } |
||
68 | } |
||
69 | |||
70 | 42 | return $this; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Update project details. |
||
75 | * |
||
76 | * @param array $attributes |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | 1 | public function update(array $attributes = []) |
|
81 | { |
||
82 | 1 | if (array_key_exists('columns', $attributes)) { |
|
83 | 1 | $this->saveTags($attributes['columns']); |
|
84 | |||
85 | 1 | unset($attributes['columns']); |
|
86 | } |
||
87 | |||
88 | 1 | return parent::update($attributes); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Save the project tags. |
||
93 | * |
||
94 | * @param string $tagString |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | 1 | public function saveTags($tagString) |
|
99 | { |
||
100 | // Transform the user input tags into tag objects |
||
101 | // Filter out invalid tags entered by the user |
||
102 | 1 | $tags = new Collection(array_map('trim', explode(',', $tagString))); |
|
103 | $tags = $tags->transform(function ($tagNameOrId) { |
||
104 | 1 | return Tag::find($tagNameOrId); |
|
105 | 1 | })->filter(function ($tag) { |
|
106 | 1 | return $tag instanceof Tag; |
|
107 | 1 | })->merge((new Tag())->getOpenAndCloseTags()); |
|
108 | |||
109 | // Delete all existing |
||
110 | 1 | $this->kanbanTags()->detach(); |
|
111 | |||
112 | // Save tags |
||
113 | 1 | $kanbanTags = $this->kanbanTags(); |
|
114 | 1 | $count = $tags->count(); |
|
115 | 1 | View Code Duplication | foreach ($tags as $position => $tag) { |
116 | 1 | $position = $tag->name === Tag::STATUS_OPEN ? -1 : $position; |
|
117 | 1 | $position = $tag->name === Tag::STATUS_CLOSED ? $count + 1 : $position; |
|
118 | 1 | $kanbanTags->attach([$tag->id => ['position' => $position]]); |
|
119 | } |
||
120 | |||
121 | 1 | return true; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Assign a user to a project. |
||
126 | * |
||
127 | * @param int $userId |
||
128 | * @param int $roleId |
||
129 | * |
||
130 | * @return Project\User |
||
131 | */ |
||
132 | 25 | public function assignUser($userId, $roleId = 0) |
|
139 | |||
140 | /** |
||
141 | * Delete a project. |
||
142 | * |
||
143 | * @return void |
||
144 | * |
||
145 | * @throws \Exception |
||
146 | */ |
||
147 | 1 | public function delete() |
|
158 | } |
||
159 |
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.