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 |
||
10 | class Page extends Model |
||
11 | { |
||
12 | |||
13 | public function getSource() |
||
17 | |||
18 | protected $translateModel = 'Page\Model\Translate\PageTranslate'; // translate |
||
19 | |||
20 | public $id; |
||
21 | public $slug; |
||
22 | public $title; // translate |
||
23 | public $text; // translate |
||
24 | public $meta_title; // translate |
||
25 | public $meta_description; // translate |
||
26 | public $meta_keywords; // translate |
||
27 | public $created_at; |
||
28 | public $updated_at; |
||
29 | |||
30 | public function initialize() |
||
34 | |||
35 | public function beforeCreate() |
||
39 | |||
40 | public function beforeUpdate() |
||
44 | |||
45 | public function updateFields($data) |
||
46 | { |
||
47 | if (!$this->getSlug()) { |
||
48 | $this->setSlug(Transliterator::slugify($data['title'])); |
||
49 | } |
||
50 | if (!$this->getMetaTitle()) { |
||
51 | $this->setMetaTitle($data['title']); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | public function validation() |
||
66 | |||
67 | View Code Duplication | public static function findCachedBySlug($slug) |
|
74 | |||
75 | /** |
||
76 | * @param mixed $created_at |
||
77 | */ |
||
78 | public function setCreatedAt($created_at) |
||
83 | |||
84 | /** |
||
85 | * @return mixed |
||
86 | */ |
||
87 | public function getCreatedAt() |
||
91 | |||
92 | /** |
||
93 | * @param mixed $id |
||
94 | */ |
||
95 | public function setId($id) |
||
100 | |||
101 | /** |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function getId() |
||
108 | |||
109 | /** |
||
110 | * @param mixed $meta_description |
||
111 | */ |
||
112 | public function setMetaDescription($meta_description) |
||
117 | |||
118 | /** |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function getMetaDescription() |
||
125 | |||
126 | /** |
||
127 | * @param mixed $meta_keywords |
||
128 | */ |
||
129 | public function setMetaKeywords($meta_keywords) |
||
134 | |||
135 | /** |
||
136 | * @return mixed |
||
137 | */ |
||
138 | public function getMetaKeywords() |
||
142 | |||
143 | /** |
||
144 | * @param mixed $meta_title |
||
145 | */ |
||
146 | public function setMetaTitle($meta_title) |
||
151 | |||
152 | /** |
||
153 | * @return mixed |
||
154 | */ |
||
155 | public function getMetaTitle() |
||
159 | |||
160 | /** |
||
161 | * @param mixed $slug |
||
162 | */ |
||
163 | public function setSlug($slug) |
||
168 | |||
169 | /** |
||
170 | * @return mixed |
||
171 | */ |
||
172 | public function getSlug() |
||
176 | |||
177 | /** |
||
178 | * @param mixed $text |
||
179 | */ |
||
180 | public function setText($text) |
||
185 | |||
186 | /** |
||
187 | * @return mixed |
||
188 | */ |
||
189 | public function getText() |
||
193 | |||
194 | /** |
||
195 | * @param mixed $title |
||
196 | */ |
||
197 | public function setTitle($title) |
||
202 | |||
203 | /** |
||
204 | * @return mixed |
||
205 | */ |
||
206 | public function getTitle() |
||
210 | |||
211 | /** |
||
212 | * @param mixed $updated_at |
||
213 | */ |
||
214 | public function setUpdatedAt($updated_at) |
||
219 | |||
220 | /** |
||
221 | * @return mixed |
||
222 | */ |
||
223 | public function getUpdatedAt() |
||
227 | |||
228 | } |
||
229 |