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 |
||
23 | trait SetterTrait |
||
24 | { |
||
25 | /** |
||
26 | * Set permission. |
||
27 | * @param string $name |
||
28 | * @param string $description |
||
29 | * @return Item |
||
30 | */ |
||
31 | 8 | public function setPermission($name, $description = null) |
|
32 | { |
||
33 | 8 | $permission = $this->getPermission($name) ?: $this->createPermission($name); |
|
34 | 8 | if ($description) { |
|
35 | $permission->description = $description; |
||
36 | } |
||
37 | 8 | $this->add($permission); |
|
38 | |||
39 | 8 | return $permission; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Set role. |
||
44 | * @param string $name |
||
45 | * @param string $description |
||
46 | * @return Item |
||
47 | */ |
||
48 | 8 | public function setRole($name, $description = null) |
|
49 | { |
||
50 | 8 | $role = $this->getRole($name) ?: $this->createRole($name); |
|
51 | 8 | if ($description) { |
|
52 | $role->description = $description; |
||
53 | } |
||
54 | 8 | $this->add($role); |
|
55 | |||
56 | 8 | return $role; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Set child. |
||
61 | * @param string|Item $parent |
||
62 | * @param string|Item $child |
||
63 | * @return bool |
||
64 | */ |
||
65 | 8 | public function setChild($parent, $child) |
|
66 | { |
||
67 | 8 | View Code Duplication | if (is_string($parent)) { |
68 | 8 | $name = $parent; |
|
69 | 8 | $parent = $this->getItem($parent); |
|
70 | 8 | if (is_null($parent)) { |
|
71 | throw new InvalidParamException("Unknown parent:$name at setChild"); |
||
72 | } |
||
73 | 8 | } |
|
74 | 8 | View Code Duplication | if (is_string($child)) { |
75 | $name = $child; |
||
76 | $child = $this->getItem($child); |
||
77 | if (is_null($child)) { |
||
78 | throw new InvalidParamException("Unknown child:$name at setChild"); |
||
79 | } |
||
80 | } |
||
81 | 8 | if (isset($this->children[$parent->name][$child->name])) { |
|
82 | return false; |
||
83 | } |
||
84 | |||
85 | 8 | return $this->addChild($parent, $child); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Assigns an item (role or permission) to a user. |
||
90 | * @param string|Item $item |
||
91 | * @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
||
92 | * @throws \Exception when given wrong item name |
||
93 | * @return Assignment|null the assignment object or `null` when assignment was not found by name |
||
94 | */ |
||
95 | 16 | public function setAssignment($item, $userId) |
|
96 | { |
||
97 | try { |
||
98 | 16 | if (is_string($item)) { |
|
99 | 16 | $item = $this->findItem($item); |
|
100 | 16 | } |
|
101 | 16 | } catch (InvalidParamException $e) { |
|
102 | Yii::warning('Role or permission "' . $item . '" does not exist'); |
||
103 | return null; |
||
104 | } |
||
105 | |||
106 | if (isset($this->assignments[$userId][$item->name])) { |
||
107 | 16 | return $this->assignments[$userId][$item->name]; |
|
108 | } |
||
109 | |||
110 | return $this->assign($item, $userId); |
||
111 | 16 | } |
|
112 | |||
113 | protected function findItem($name, $description = null) |
||
125 | |||
126 | /** |
||
127 | * Assigns items to a user. |
||
128 | * @param string|array $items |
||
129 | * @param string|integer $userId |
||
130 | */ |
||
131 | public function setAssignments($items, $userId) |
||
132 | 6 | { |
|
133 | if (is_string($items)) { |
||
134 | 6 | $items = explode(',', $items); |
|
135 | 6 | } |
|
136 | 6 | foreach ($items as $item) { |
|
137 | 6 | $this->setAssignment($item, $userId); |
|
138 | 6 | } |
|
139 | 6 | } |
|
140 | 6 | ||
141 | /** |
||
142 | * Returns all assignments in the system. |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getAllAssignments() |
||
149 | |||
150 | /** |
||
151 | * Returns all items in the system. |
||
152 | * @return array |
||
153 | */ |
||
154 | public function getAllItems() |
||
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.