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 |
||
| 17 | class ErrorPage extends Page { |
||
| 18 | |||
| 19 | private static $db = array( |
||
| 20 | "ErrorCode" => "Int", |
||
| 21 | ); |
||
| 22 | |||
| 23 | private static $defaults = array( |
||
| 24 | "ShowInMenus" => 0, |
||
| 25 | "ShowInSearch" => 0 |
||
| 26 | ); |
||
| 27 | |||
| 28 | private static $allowed_children = array(); |
||
| 29 | |||
| 30 | private static $description = 'Custom content for different error cases (e.g. "Page not found")'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Allows control over writing directly to the configured `GeneratedAssetStore`. |
||
| 34 | * |
||
| 35 | * @config |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | private static $enable_static_file = true; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Prefix for storing error files in the {@see GeneratedAssetHandler} store. |
||
| 42 | * Defaults to empty (top level directory) |
||
| 43 | * |
||
| 44 | * @config |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private static $store_filepath = null; |
||
| 48 | /** |
||
| 49 | * @param $member |
||
| 50 | * |
||
| 51 | * @return boolean |
||
| 52 | */ |
||
| 53 | public function canAddChildren($member = null) { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get a {@link SS_HTTPResponse} to response to a HTTP error code if an |
||
| 59 | * {@link ErrorPage} for that code is present. First tries to serve it |
||
| 60 | * through the standard SilverStripe request method. Falls back to a static |
||
| 61 | * file generated when the user hit's save and publish in the CMS |
||
| 62 | * |
||
| 63 | * @param int $statusCode |
||
| 64 | * @return SS_HTTPResponse |
||
| 65 | */ |
||
| 66 | public static function response_for($statusCode) { |
||
| 67 | // first attempt to dynamically generate the error page |
||
| 68 | $errorPage = ErrorPage::get() |
||
| 69 | ->filter(array( |
||
| 70 | "ErrorCode" => $statusCode |
||
| 71 | ))->first(); |
||
| 72 | |||
| 73 | if($errorPage) { |
||
| 74 | Requirements::clear(); |
||
| 75 | Requirements::clear_combined_files(); |
||
| 76 | |||
| 77 | return ModelAsController::controller_for($errorPage) |
||
| 78 | ->handleRequest( |
||
| 79 | new SS_HTTPRequest('GET', ''), |
||
| 80 | DataModel::inst() |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | |||
| 84 | // then fall back on a cached version |
||
| 85 | $content = self::get_content_for_errorcode($statusCode); |
||
| 86 | if($content) { |
||
|
|
|||
| 87 | $response = new SS_HTTPResponse(); |
||
| 88 | $response->setStatusCode($statusCode); |
||
| 89 | $response->setBody($content); |
||
| 90 | return $response; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Ensures that there is always a 404 page by checking if there's an |
||
| 96 | * instance of ErrorPage with a 404 and 500 error code. If there is not, |
||
| 97 | * one is created when the DB is built. |
||
| 98 | */ |
||
| 99 | public function requireDefaultRecords() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns an array of arrays, each of which defines properties for a new |
||
| 148 | * ErrorPage record. |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | protected function getDefaultRecords() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return FieldList |
||
| 180 | */ |
||
| 181 | public function getCMSFields() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * When an error page is published, create a static HTML page with its |
||
| 225 | * content, so the page can be shown even when SilverStripe is not |
||
| 226 | * functioning correctly before publishing this page normally. |
||
| 227 | * |
||
| 228 | * @return bool True if published |
||
| 229 | */ |
||
| 230 | public function publishSingle() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Determine if static content is cached for this page |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | View Code Duplication | protected function hasStaticPage() { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Write out the published version of the page to the filesystem |
||
| 256 | * |
||
| 257 | * @return true if the page write was successful |
||
| 258 | */ |
||
| 259 | public function writeStaticPage() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function fieldLabels($includerelations = true) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Returns statically cached content for a given error code |
||
| 296 | * |
||
| 297 | * @param int $statusCode A HTTP Statuscode, typically 404 or 500 |
||
| 298 | * @return string|null |
||
| 299 | */ |
||
| 300 | View Code Duplication | public static function get_content_for_errorcode($statusCode) { |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Gets the filename identifier for the given error code. |
||
| 316 | * Used when handling responses under error conditions. |
||
| 317 | * |
||
| 318 | * @param int $statusCode A HTTP Statuscode, typically 404 or 500 |
||
| 319 | * @param ErrorPage $instance Optional instance to use for name generation |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | protected static function get_error_filename($statusCode, $instance = null) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get filename identifier for this record. |
||
| 334 | * Used for generating the filename for the current record. |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | protected function getErrorFilename() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return GeneratedAssetHandler |
||
| 344 | */ |
||
| 345 | protected static function get_asset_handler() { |
||
| 348 | } |
||
| 349 | |||
| 374 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: