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 |
||
| 32 | * |
||
| 33 | * @subpackage control |
||
| 34 | * |
||
| 35 | * @todo Check for correct XHTML doctype in xhtml() |
||
| 36 | * @todo Allow for other HTML4 doctypes (e.g. Transitional) in html() |
||
| 37 | * @todo Make content replacement and doctype setting two separately configurable behaviours |
||
| 38 | * |
||
| 39 | * Some developers might know what they're doing and don't want ContentNegotiator messing with their |
||
| 40 | * HTML4 doctypes, but still find it useful to have self-closing tags removed. |
||
| 41 | */ |
||
| 42 | class ContentNegotiator extends Object { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @config |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private static $content_type = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @config |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private static $encoding = 'utf-8'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @config |
||
| 60 | * |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | private static $enabled = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @config |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private static $default_format = 'html'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set the character set encoding for this page. By default it's utf-8, but you could change it to, |
||
| 74 | * say, windows-1252, to improve interoperability with extended characters being imported from windows |
||
| 75 | * excel. |
||
| 76 | * |
||
| 77 | * @deprecated 4.0 Use the "ContentNegotiator.encoding" config setting instead |
||
| 78 | * |
||
| 79 | * @param string $encoding |
||
| 80 | */ |
||
| 81 | public static function set_encoding($encoding) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Return the character encoding set bhy ContentNegotiator::set_encoding(). It's recommended that all |
||
| 88 | * classes that need to specify the character set make use of this function. |
||
| 89 | * |
||
| 90 | * @deprecated 4.0 Use the "ContentNegotiator.encoding" config setting instead. |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public static function get_encoding() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Enable content negotiation for all templates, not just those with the xml header. |
||
| 101 | * |
||
| 102 | * @deprecated 4.0 Use the "ContentNegotiator.enabled" config setting instead |
||
| 103 | */ |
||
| 104 | public static function enable() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Disable content negotiation for all templates, not just those with the xml header. |
||
| 111 | * |
||
| 112 | * @deprecated 4.0 Use the "ContentNegotiator.enabled" config setting instead |
||
| 113 | */ |
||
| 114 | public static function disable() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns true if negotiation is enabled for the given response. By default, negotiation is only |
||
| 121 | * enabled for pages that have the xml header. |
||
| 122 | */ |
||
| 123 | public static function enabled_for($response) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param SS_HTTPResponse $response |
||
| 138 | */ |
||
| 139 | public static function process(HTTPResponse $response) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check user defined content type and use it, if it's empty use the strict application/xhtml+xml. |
||
| 184 | * Replaces a few common tags and entities with their XHTML representations (<br>, <img>, |
||
| 185 | * <input>, checked, selected). |
||
| 186 | * |
||
| 187 | * @param SS_HTTPResponse $response |
||
| 188 | * |
||
| 189 | * @todo Search for more xhtml replacement |
||
| 190 | */ |
||
| 191 | public function xhtml(HTTPResponse $response) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Performs the following replacements: |
||
| 221 | * - Check user defined content type and use it, if it's empty use the text/html. |
||
| 222 | * - If find a XML header replaces it and existing doctypes with HTML4.01 Strict. |
||
| 223 | * - Replaces self-closing tags like <img /> with unclosed solitary tags like <img>. |
||
| 224 | * - Replaces all occurrences of "application/xhtml+xml" with "text/html" in the template. |
||
| 225 | * - Removes "xmlns" attributes and any <?xml> Pragmas. |
||
| 226 | * |
||
| 227 | * @param SS_HTTPResponse $response |
||
| 228 | */ |
||
| 229 | public function html(HTTPResponse $response) { |
||
| 230 | $encoding = Config::inst()->get('SilverStripe\Control\ContentNegotiator', 'encoding'); |
||
| 231 | $contentType = Config::inst()->get('SilverStripe\Control\ContentNegotiator', 'content_type'); |
||
| 232 | View Code Duplication | if (empty($contentType)) { |
|
| 233 | $response->addHeader("Content-Type", "text/html; charset=" . $encoding); |
||
| 234 | } else { |
||
| 235 | $response->addHeader("Content-Type", $contentType . "; charset=" . $encoding); |
||
| 236 | } |
||
| 237 | $response->addHeader("Vary", "Accept"); |
||
| 238 | |||
| 239 | $content = $response->getBody(); |
||
| 240 | $hasXMLHeader = (substr($content,0,5) == '<' . '?xml' ); |
||
| 241 | |||
| 242 | // Fix base tag |
||
| 243 | $content = preg_replace('/<base href="([^"]*)" \/>/', |
||
| 244 | '<base href="$1"><!--[if lte IE 6]></base><![endif]-->', $content); |
||
| 245 | |||
| 246 | $content = preg_replace("#<\\?xml[^>]+\\?>\n?#", '', $content); |
||
| 247 | $content = str_replace(array('/>','xml:lang','application/xhtml+xml'),array('>','lang','text/html'), $content); |
||
| 248 | |||
| 249 | // Only replace the doctype in templates with the xml header |
||
| 250 | if($hasXMLHeader) { |
||
| 251 | $content = preg_replace('/<!DOCTYPE[^>]+>/', |
||
| 261 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.