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 |
||
13 | class Category extends Model |
||
14 | { |
||
15 | |||
16 | public function getSource() |
||
17 | { |
||
18 | return "tree_category"; |
||
19 | } |
||
20 | |||
21 | protected $translateModel = 'Tree\Model\Translate\CategoryTranslate'; // translate |
||
22 | |||
23 | private $id; |
||
24 | private $root = 'articles'; |
||
25 | private $parent_id; |
||
26 | private $slug; |
||
27 | private $depth = 0; |
||
28 | private $left_key; |
||
29 | private $right_key; |
||
30 | private $created_at; |
||
31 | private $updated_at; |
||
32 | public $title; // translate |
||
33 | |||
34 | public static $roots = [ |
||
35 | 'articles' => 'Articles', |
||
36 | 'news' => 'News' |
||
37 | ]; |
||
38 | |||
39 | public function initialize() |
||
40 | { |
||
41 | $this->belongsTo('parent_id', 'Category\Model\Category', 'id', ['alias' => 'Parent']); |
||
42 | $this->hasMany("id", $this->translateModel, "foreign_id"); // translate |
||
43 | } |
||
44 | |||
45 | View Code Duplication | public function validation() |
|
46 | { |
||
47 | $validator = new Validation(); |
||
48 | $validator->add('slug', new UniquenessValidator( |
||
49 | [ |
||
50 | "model" => $this, |
||
51 | "message" => "Category with slug '" . $this->slug . "' is already exists. Take another title" |
||
52 | ] |
||
53 | )); |
||
54 | return $this->validate($validator); |
||
55 | } |
||
56 | |||
57 | public function beforeCreate() |
||
58 | { |
||
59 | $this->created_at = date("Y-m-d H:i:s"); |
||
60 | } |
||
61 | |||
62 | public function beforeUpdate() |
||
63 | { |
||
64 | $this->updated_at = date("Y-m-d H:i:s"); |
||
65 | } |
||
66 | |||
67 | public static function treeUpperLeafs($root) |
||
76 | |||
77 | View Code Duplication | public function children() |
|
92 | |||
93 | public function hasChildren() |
||
99 | |||
100 | /** |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function getId() |
||
104 | { |
||
105 | return $this->id; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param mixed $id |
||
110 | */ |
||
111 | public function setId($id) |
||
112 | { |
||
113 | $this->id = $id; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getRoot() |
||
120 | { |
||
121 | return $this->root; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param string $root |
||
126 | */ |
||
127 | public function setRoot($root) |
||
128 | { |
||
129 | $this->root = $root; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return mixed |
||
134 | */ |
||
135 | public function getParentId() |
||
136 | { |
||
137 | return $this->parent_id; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param mixed $parent_id |
||
142 | */ |
||
143 | public function setParentId($parent_id) |
||
144 | { |
||
145 | $this->parent_id = $parent_id; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function getDepth() |
||
152 | { |
||
153 | return $this->depth; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param mixed $depth |
||
158 | */ |
||
159 | public function setDepth($depth) |
||
160 | { |
||
161 | $this->depth = $depth; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return mixed |
||
166 | */ |
||
167 | public function getLeftKey() |
||
171 | |||
172 | /** |
||
173 | * @param mixed $left_key |
||
174 | */ |
||
175 | public function setLeftKey($left_key) |
||
179 | |||
180 | /** |
||
181 | * @return mixed |
||
182 | */ |
||
183 | public function getRightKey() |
||
187 | |||
188 | /** |
||
189 | * @param mixed $right_key |
||
190 | */ |
||
191 | public function setRightKey($right_key) |
||
195 | |||
196 | /** |
||
197 | * @return mixed |
||
198 | */ |
||
199 | public function getSlug() |
||
200 | { |
||
201 | return $this->slug; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param mixed $slug |
||
206 | */ |
||
207 | public function setSlug($slug) |
||
208 | { |
||
209 | $this->slug = $slug; |
||
211 | |||
212 | /** |
||
213 | * @return mixed |
||
214 | */ |
||
215 | public function getTitle() |
||
219 | |||
220 | /** |
||
221 | * @param mixed $title |
||
222 | */ |
||
223 | public function setTitle($title) |
||
227 | |||
228 | /** |
||
229 | * @return mixed |
||
230 | */ |
||
231 | public function getCreatedAt() |
||
235 | |||
236 | /** |
||
237 | * @param mixed $created_at |
||
238 | */ |
||
239 | public function setCreatedAt($created_at) |
||
243 | |||
244 | /** |
||
245 | * @return mixed |
||
246 | */ |
||
247 | public function getUpdatedAt() |
||
251 | |||
252 | /** |
||
253 | * @param mixed $updated_at |
||
254 | */ |
||
255 | public function setUpdatedAt($updated_at) |
||
259 | } |
||
260 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.