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_Model_Observer_Ban 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_Model_Observer_Ban, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Nexcessnet_Turpentine_Model_Observer_Ban extends Varien_Event_Observer { |
||
27 | |||
28 | /** |
||
29 | * Cache the varnish admin object |
||
30 | * @var Nexcessnet_Turpentine_Model_Varnish_Admin |
||
31 | */ |
||
32 | protected $_varnishAdmin = null; |
||
33 | /** |
||
34 | * Flag to prevent doing the ESI cache clear more than once per request |
||
35 | * @var boolean |
||
36 | */ |
||
37 | protected $_esiClearFlag = array(); |
||
38 | |||
39 | /** |
||
40 | * Clear the ESI block cache for a specific client |
||
41 | * |
||
42 | * Events: |
||
43 | * the events are applied dynamically according to what events are set |
||
44 | * for the various blocks' esi policies |
||
45 | * |
||
46 | * @param Varien_Object $eventObject |
||
47 | * @return null |
||
48 | */ |
||
49 | public function banClientEsiCache($eventObject) { |
||
74 | |||
75 | /** |
||
76 | * Ban a specific product page from the cache |
||
77 | * |
||
78 | * Events: |
||
79 | * catalog_product_save_commit_after |
||
80 | * |
||
81 | * @param Varien_Object $eventObject |
||
82 | * @return null |
||
83 | */ |
||
84 | public function banProductPageCache($eventObject) { |
||
102 | |||
103 | /** |
||
104 | * Ban a product page from the cache if it's stock status changed |
||
105 | * |
||
106 | * Events: |
||
107 | * cataloginventory_stock_item_save_after |
||
108 | * |
||
109 | * @param Varien_Object $eventObject |
||
110 | * @return null |
||
111 | */ |
||
112 | public function banProductPageCacheCheckStock($eventObject) { |
||
138 | |||
139 | /** |
||
140 | * Ban a category page, and any subpages on save |
||
141 | * |
||
142 | * Events: |
||
143 | * catalog_category_save_commit_after |
||
144 | * |
||
145 | * @param Varien_Object $eventObject |
||
146 | * @return null |
||
147 | */ |
||
148 | View Code Duplication | public function banCategoryCache($eventObject) { |
|
160 | |||
161 | /** |
||
162 | * Clear the media (CSS/JS) cache, corresponds to the buttons on the cache |
||
163 | * page in admin |
||
164 | * |
||
165 | * Events: |
||
166 | * clean_media_cache_after |
||
167 | * |
||
168 | * @param Varien_Object $eventObject |
||
169 | * @return null |
||
170 | */ |
||
171 | View Code Duplication | public function banMediaCache($eventObject) { |
|
178 | |||
179 | /** |
||
180 | * Flush catalog images cache, corresponds to same button in admin cache |
||
181 | * management page |
||
182 | * |
||
183 | * Events: |
||
184 | * clean_catalog_images_cache_after |
||
185 | * |
||
186 | * @param Varien_Object $eventObject |
||
187 | * @return null |
||
188 | */ |
||
189 | View Code Duplication | public function banCatalogImagesCache($eventObject) { |
|
197 | |||
198 | /** |
||
199 | * Ban a specific CMS page from cache after edit |
||
200 | * |
||
201 | * Events: |
||
202 | * cms_page_save_commit_after |
||
203 | * |
||
204 | * @param Varien_Object $eventObject |
||
205 | * @return null |
||
206 | */ |
||
207 | View Code Duplication | public function banCmsPageCache($eventObject) { |
|
219 | |||
220 | /** |
||
221 | * Ban a specific CMS page revision from cache after edit (enterprise edition only) |
||
222 | * Events: |
||
223 | * enterprise_cms_revision_save_commit_after |
||
224 | * |
||
225 | * @param Varien_Object $eventObject |
||
226 | * @return null |
||
227 | */ |
||
228 | public function banCmsPageRevisionCache($eventObject) { |
||
247 | |||
248 | /** |
||
249 | * Do a full cache flush, corresponds to "Flush Magento Cache" and |
||
250 | * "Flush Cache Storage" buttons in admin > cache management |
||
251 | * |
||
252 | * Events: |
||
253 | * adminhtml_cache_flush_system |
||
254 | * adminhtml_cache_flush_all |
||
255 | * |
||
256 | * @param Varien_Object $eventObject |
||
257 | * @return null |
||
258 | */ |
||
259 | View Code Duplication | public function banAllCache($eventObject) { |
|
266 | |||
267 | /** |
||
268 | * Do a flush on the ESI blocks |
||
269 | * |
||
270 | * Events: |
||
271 | * adminhtml_cache_refresh_type |
||
272 | * |
||
273 | * @param Varien_Object $eventObject |
||
274 | * @return null |
||
275 | */ |
||
276 | public function banCacheType($eventObject) { |
||
293 | |||
294 | /** |
||
295 | * Ban a product's reviews page |
||
296 | * |
||
297 | * @param Varien_Object $eventObject |
||
298 | * @return bool |
||
299 | */ |
||
300 | public function banProductReview($eventObject) { |
||
335 | |||
336 | /** |
||
337 | * Check a result from varnish admin action, log if result has errors |
||
338 | * |
||
339 | * @param array $result stored as $socketName => $result |
||
340 | * @return bool |
||
341 | */ |
||
342 | protected function _checkResult($result) { |
||
354 | |||
355 | /** |
||
356 | * Get the varnish admin socket |
||
357 | * |
||
358 | * @return Nexcessnet_Turpentine_Model_Varnish_Admin |
||
359 | */ |
||
360 | protected function _getVarnishAdmin() { |
||
366 | } |
||
367 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.