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:
Complex classes like Nexcessnet_Turpentine_Helper_Esi often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Nexcessnet_Turpentine_Helper_Esi, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Nexcessnet_Turpentine_Helper_Esi extends Mage_Core_Helper_Abstract { |
||
|
|
|||
| 23 | const ESI_DATA_PARAM = 'data'; |
||
| 24 | const ESI_TTL_PARAM = 'ttl'; |
||
| 25 | const ESI_CACHE_TYPE_PARAM = 'access'; |
||
| 26 | const ESI_SCOPE_PARAM = 'scope'; |
||
| 27 | const ESI_METHOD_PARAM = 'method'; |
||
| 28 | const ESI_HMAC_PARAM = 'hmac'; |
||
| 29 | const MAGE_CACHE_NAME = 'turpentine_esi_blocks'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Cache for layout XML |
||
| 33 | * |
||
| 34 | * @var Mage_Core_Model_Layout_Element|SimpleXMLElement |
||
| 35 | */ |
||
| 36 | protected $_layoutXml = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Get whether ESI includes are enabled or not |
||
| 40 | * |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | public function getEsiEnabled() { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Get if ESI should be used for this request |
||
| 49 | * |
||
| 50 | * @return bool |
||
| 51 | */ |
||
| 52 | public function shouldResponseUseEsi() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Check if ESI includes are enabled and throw an exception if not |
||
| 59 | * |
||
| 60 | * @return null |
||
| 61 | */ |
||
| 62 | public function ensureEsiEnabled() { |
||
| 63 | if ( ! $this->shouldResponseUseEsi()) { |
||
| 64 | Mage::throwException('ESI includes are not enabled'); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the name of the URL param that holds the ESI block hash |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function getEsiDataParam() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get the URL param name for the ESI block cache type |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public function getEsiCacheTypeParam() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get the URL param name for the ESI block scope |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function getEsiScopeParam() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the URL param name for the ESI block TTL |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function getEsiTtlParam() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get the URL param name for the ESI inclusion method |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getEsiMethodParam() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get the URL param name for the ESI HMAC |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getEsiHmacParam() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get referrer param |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getEsiReferrerParam() { |
||
| 128 | return Mage_Core_Controller_Varien_Action::PARAM_NAME_BASE64_URL; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get whether ESI debugging is enabled or not |
||
| 133 | * |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | public function getEsiDebugEnabled() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get whether block name logging is enabled or not |
||
| 143 | * |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | public function getEsiBlockLogEnabled() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Check if the flash messages are enabled and we're not in the admin section |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | public function shouldFixFlashMessages() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get URL for redirects and dummy requests |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getDummyUrl() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get mock request |
||
| 172 | * |
||
| 173 | * Used to pretend that the request was for the base URL instead of |
||
| 174 | * turpentine/esi/getBlock while rendering ESI blocks. Not perfect, but may |
||
| 175 | * be good enough |
||
| 176 | * |
||
| 177 | * @return Mage_Core_Controller_Request_Http |
||
| 178 | */ |
||
| 179 | public function getDummyRequest($url = null) { |
||
| 180 | if ($url === null) { |
||
| 181 | $url = $this->getDummyUrl(); |
||
| 182 | } |
||
| 183 | $request = Mage::getModel('turpentine/dummy_request', $url); |
||
| 184 | $request->fakeRouterDispatch(); |
||
| 185 | return $request; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the cache type Magento uses |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getMageCacheName() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get the list of cache clear events to include with every ESI block |
||
| 199 | * |
||
| 200 | * @return string[] |
||
| 201 | */ |
||
| 202 | public function getDefaultCacheClearEvents() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the list of events that should cause the ESI cache to be cleared |
||
| 212 | * |
||
| 213 | * @return string[] |
||
| 214 | */ |
||
| 215 | public function getCacheClearEvents() { |
||
| 216 | Varien_Profiler::start('turpentine::helper::esi::getCacheClearEvents'); |
||
| 217 | $cacheKey = $this->getCacheClearEventsCacheKey(); |
||
| 218 | $events = @unserialize(Mage::app()->loadCache($cacheKey)); |
||
| 219 | if (is_null($events) || $events === false) { |
||
| 220 | $events = $this->_loadEsiCacheClearEvents(); |
||
| 221 | Mage::app()->saveCache(serialize($events), $cacheKey, |
||
| 222 | array('LAYOUT_GENERAL_CACHE_TAG')); |
||
| 223 | } |
||
| 224 | Varien_Profiler::stop('turpentine::helper::esi::getCacheClearEvents'); |
||
| 225 | return array_merge($this->getDefaultCacheClearEvents(), $events); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the default private ESI block TTL |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getDefaultEsiTtl() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get the CORS origin field from the unsecure base URL |
||
| 239 | * |
||
| 240 | * If this isn't added to AJAX responses they won't load properly |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getCorsOrigin($url = null) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the layout's XML structure |
||
| 260 | * |
||
| 261 | * This is cached because it's expensive to load for each ESI'd block |
||
| 262 | * |
||
| 263 | * @return Mage_Core_Model_Layout_Element|SimpleXMLElement |
||
| 264 | */ |
||
| 265 | public function getLayoutXml() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the cache key for the cache clear events |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | View Code Duplication | public function getCacheClearEventsCacheKey() { |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Get the cache key for the file layouts xml |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function getFileLayoutUpdatesXmlCacheKey() { |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Generate an ESI tag to be replaced by the content from the given URL |
||
| 322 | * |
||
| 323 | * Generated tag looks like: |
||
| 324 | * <esi:include src="$url" /> |
||
| 325 | * |
||
| 326 | * @param string $url url to pull content from |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function buildEsiIncludeFragment($url) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Generate an ESI tag with content that is removed when ESI processed, and |
||
| 335 | * visible when not |
||
| 336 | * |
||
| 337 | * Generated tag looks like: |
||
| 338 | * <esi:remove>$content</esi> |
||
| 339 | * |
||
| 340 | * @param string $content content to be removed |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function buildEsiRemoveFragment($content) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get URL for grabbing form key via ESI |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | public function getFormKeyEsiUrl() { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Grab a block node by name from the layout XML. |
||
| 367 | * |
||
| 368 | * Multiple blocks with the same name may exist in the layout, because some themes |
||
| 369 | * use 'unsetChild' to remove a block and create it with the same name somewhere |
||
| 370 | * else. For example Ultimo does this. |
||
| 371 | * |
||
| 372 | * @param Mage_Core_Model_Layout $layout |
||
| 373 | * @param string $blockName value of name= attribute in layout XML |
||
| 374 | * @return Mage_Core_Model_Layout_Element |
||
| 375 | */ |
||
| 376 | public function getEsiLayoutBlockNode($layout, $blockName) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Load the ESI cache clear events from the layout |
||
| 393 | * |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | protected function _loadEsiCacheClearEvents() { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Load the layout's XML structure, bypassing any caching |
||
| 415 | * |
||
| 416 | * @return Mage_Core_Model_Layout_Element |
||
| 417 | */ |
||
| 418 | protected function _loadLayoutXml() { |
||
| 431 | } |
||
| 432 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.