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 |
||
| 11 | class Blocker |
||
| 12 | { |
||
| 13 | const SEPERATOR = ':'; |
||
| 14 | |||
| 15 | public static $explanation = "Access to this website has been blocked because your referral is set to %s. <a href='%s'>Read why</a>"; |
||
| 16 | |||
| 17 | private static $blocklist = './../../domains/blocked'; |
||
| 18 | private static $reason = 'Not blocking, no reason given'; |
||
| 19 | |||
| 20 | ////////////////////////////////////////// |
||
| 21 | // PUBLIC API // |
||
| 22 | ////////////////////////////////////////// |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Block a page if referer is found on list of blocked domains. |
||
| 26 | * |
||
| 27 | * @param string $action If empty, send 403 response; if URL, redirect here; if non-empty string, print message |
||
| 28 | */ |
||
| 29 | public static function protect($action = '') |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param bool $verbose Deprecated. Please use the explain() method instead. |
||
| 51 | * |
||
| 52 | * @return bool|string |
||
| 53 | */ |
||
| 54 | public static function blocked($verbose = false) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public static function explain() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Send a 403 Forbidden header. |
||
| 74 | */ |
||
| 75 | public static function forbidden() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | public static function getBlocklist() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public static function getBlocklistFilename() |
||
| 96 | |||
| 97 | ////////////////////////////////////////// |
||
| 98 | // PRIVATE FUNCTIONS // |
||
| 99 | ////////////////////////////////////////// |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Responsible for sending action output. |
||
| 103 | * |
||
| 104 | * @param string $action |
||
| 105 | */ |
||
| 106 | private static function doBlock($action = '') |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Execute desired action. |
||
| 122 | * |
||
| 123 | * @param string $action |
||
| 124 | */ |
||
| 125 | private static function blockAction($action = '') |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Clear output buffer. |
||
| 140 | */ |
||
| 141 | private static function cls() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Redirect to a url by sending the appropriate header. |
||
| 150 | * |
||
| 151 | * @param string $url |
||
| 152 | */ |
||
| 153 | private static function redirect($url) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * The public use of this function is undocumented. |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public static function isRefererOnBlocklist() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * The public use of this function is undocumented. |
||
| 177 | * |
||
| 178 | * @param string $url |
||
| 179 | * @param string $entity |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public static function isUrlOnBlocklist($url, $entity = 'url') |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Returns HTTP Referer if it is available and not empty, null otherwise. |
||
| 227 | * |
||
| 228 | * @return string|null |
||
| 229 | */ |
||
| 230 | private static function getHttpReferer() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | private static function getBlocklistContents() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | private static function getConcatenateBlocklist() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $blocklistContent |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | private static function parseBlocklist($blocklistContent) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $blocklistContent |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | private static function concatenateBlocklist($blocklistContent) |
||
| 274 | } |
||
| 275 |
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
SomeClassto useselfinstead: