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 |
||
12 | class Blocker |
||
13 | { |
||
14 | const SEPERATOR = ':'; |
||
15 | |||
16 | public static $explanation = "Access to this website has been blocked because your referral is set to %s. <a href='%s'>Read why</a>"; |
||
17 | |||
18 | private static $blocklist = './../../domains/blocked'; |
||
19 | private static $reason = 'Not blocking, no reason given'; |
||
20 | |||
21 | ////////////////////////////////////////// |
||
22 | // PUBLIC API // |
||
23 | ////////////////////////////////////////// |
||
24 | |||
25 | /** |
||
26 | * Block a page if referer is found on list of blocked domains |
||
27 | * |
||
28 | * @param string $action If empty, send 403 response; if URL, redirect here; if non-empty string, print message |
||
29 | */ |
||
30 | public static function protect($action = '') |
||
43 | |||
44 | /** |
||
45 | * @param bool $verbose Deprecated. Please use the explain() method instead. |
||
46 | * @return bool|string |
||
47 | */ |
||
48 | public static function blocked($verbose = false) |
||
49 | { |
||
50 | $blocked = self::isRefererOnBlocklist(); |
||
51 | if ($verbose === true) { |
||
52 | return self::$reason; |
||
53 | } |
||
54 | return $blocked; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return string |
||
59 | */ |
||
60 | public static function explain() |
||
64 | |||
65 | /** |
||
66 | * Send a 403 Forbidden header |
||
67 | */ |
||
68 | public static function forbidden() |
||
69 | { |
||
70 | $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); |
||
71 | header($protocol . ' 403 Forbidden'); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return array |
||
76 | */ |
||
77 | public static function getBlocklist() |
||
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | public static function getBlocklistFilename() |
||
89 | |||
90 | ////////////////////////////////////////// |
||
91 | // PRIVATE FUNCTIONS // |
||
92 | ////////////////////////////////////////// |
||
93 | |||
94 | /** |
||
95 | * Responsible for sending action output |
||
96 | * |
||
97 | * @param string $action |
||
98 | */ |
||
99 | private static function doBlock($action = '') |
||
110 | |||
111 | /** |
||
112 | * Execute desired action |
||
113 | * |
||
114 | * @param string $action |
||
115 | */ |
||
116 | private static function blockAction($action = '') |
||
126 | |||
127 | |||
128 | /** |
||
129 | * Clear output buffer |
||
130 | */ |
||
131 | private static function cls() |
||
135 | |||
136 | /** |
||
137 | * Redirect to a url by sending the appropriate header. |
||
138 | * @param string $url |
||
139 | */ |
||
140 | private static function redirect($url) |
||
144 | |||
145 | |||
146 | /** |
||
147 | * The public use of this function is undocumented. |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | public static function isRefererOnBlocklist() |
||
162 | |||
163 | |||
164 | /** |
||
165 | * The public use of this function is undocumented. |
||
166 | * |
||
167 | * @param string $url |
||
168 | * @param string $entity |
||
169 | * @return bool |
||
170 | */ |
||
171 | public static function isUrlOnBlocklist($url, $entity = 'url') |
||
211 | |||
212 | /** |
||
213 | * Returns HTTP Referer if it is available and not empty, null otherwise |
||
214 | * |
||
215 | * @return string|null |
||
216 | */ |
||
217 | private static function getHttpReferer() |
||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | private static function getBlocklistContents() |
||
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | private static function getConcatenateBlocklist() |
||
241 | |||
242 | /** |
||
243 | * @param string $blocklistContent |
||
244 | * @return array |
||
245 | */ |
||
246 | private static function parseBlocklist($blocklistContent) |
||
250 | |||
251 | /** |
||
252 | * @param string $blocklistContent |
||
253 | * @return string |
||
254 | */ |
||
255 | private static function concatenateBlocklist($blocklistContent) |
||
259 | } |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: