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 Page extends AliasModel |
||
14 | { |
||
15 | /** |
||
16 | * The content of the page |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $content; |
||
20 | |||
21 | /** |
||
22 | * The creation date of the page |
||
23 | * @var TimeDate |
||
24 | */ |
||
25 | protected $created; |
||
26 | |||
27 | /** |
||
28 | * The date the page was last updated |
||
29 | * @var TimeDate |
||
30 | */ |
||
31 | protected $updated; |
||
32 | |||
33 | /** |
||
34 | * The ID of the author of the page |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $author; |
||
38 | |||
39 | /** |
||
40 | * Whether the page is the home page |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $home; |
||
44 | |||
45 | /** |
||
46 | * The status of the page |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $status; |
||
50 | |||
51 | /** |
||
52 | * The name of the database table used for queries |
||
53 | */ |
||
54 | const TABLE = "pages"; |
||
55 | |||
56 | const CREATE_PERMISSION = Permission::CREATE_PAGE; |
||
57 | const EDIT_PERMISSION = Permission::EDIT_PAGE; |
||
58 | const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_PAGE; |
||
59 | const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_PAGE; |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 1 | View Code Duplication | protected function assignResult($page) |
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | protected function assignLazyResult($page) |
||
82 | |||
83 | /** |
||
84 | * Get the raw content of the page |
||
85 | * @return string |
||
86 | */ |
||
87 | public function getContent() |
||
88 | { |
||
89 | $this->lazyLoad(); |
||
90 | |||
91 | return $this->content; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get the page's submission time |
||
96 | * @return TimeDate |
||
97 | */ |
||
98 | public function getCreated() |
||
104 | |||
105 | /** |
||
106 | * Get the time when the page was last updated |
||
107 | * @return TimeDate |
||
108 | */ |
||
109 | public function getUpdated() |
||
110 | { |
||
111 | $this->lazyLoad(); |
||
112 | |||
113 | return $this->updated->copy(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Get the user who created the page |
||
118 | * @return Player The page's author |
||
119 | */ |
||
120 | public function getAuthor() |
||
121 | { |
||
122 | return Player::get($this->author); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get the status of the page |
||
127 | * @return string |
||
128 | */ |
||
129 | 1 | public function getStatus() |
|
133 | |||
134 | /** |
||
135 | * Find out whether this is the homepage |
||
136 | * @return bool |
||
137 | */ |
||
138 | 1 | public function isHomePage() |
|
142 | |||
143 | /** |
||
144 | * Set the content of the page |
||
145 | * |
||
146 | * @param string $content |
||
147 | * @return self |
||
148 | */ |
||
149 | public function setContent($content) |
||
150 | { |
||
151 | return $this->updateProperty($this->content, "content", $content, 's'); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Set the status of the page |
||
156 | * |
||
157 | * @param string $status One of "live", "revision" or "disabled" |
||
158 | * @return self |
||
159 | */ |
||
160 | public function setStatus($status) |
||
164 | |||
165 | /** |
||
166 | * Update the last edit timestamp |
||
167 | * @return self |
||
168 | */ |
||
169 | public function updateEditTimestamp() |
||
173 | |||
174 | /** |
||
175 | * Create a new Page |
||
176 | * |
||
177 | * @param string $title The title of the page |
||
178 | * @param string $content The content of page |
||
179 | * @param int $authorID The ID of the author |
||
180 | * @param string $status Page status: 'live','disabled',or 'deleted' |
||
181 | * |
||
182 | * @return Page An object representing the page that was just created |
||
183 | */ |
||
184 | 1 | public static function addPage($title, $content, $authorID, $status = "live") |
|
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | 1 | public static function getRouteName($action = 'show') |
|
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | 1 | protected static function getDisallowedAliases() |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 1 | public static function getActiveStatuses() |
|
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | 1 | public static function getEagerColumns() |
|
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | public static function getLazyColumns() |
||
239 | |||
240 | /** |
||
241 | * Get a query builder for pages |
||
242 | * @return QueryBuilder |
||
243 | */ |
||
244 | 1 | View Code Duplication | public static function getQueryBuilder() |
254 | |||
255 | /** |
||
256 | * Get the home page |
||
257 | * @deprecated |
||
258 | * @return Page |
||
259 | */ |
||
260 | public static function getHomePage() |
||
264 | } |
||
265 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.