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_Dummy_Request 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_Dummy_Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Nexcessnet_Turpentine_Model_Dummy_Request extends |
||
|
|||
23 | Mage_Core_Controller_Request_Http { |
||
24 | |||
25 | public $GET = null; |
||
26 | public $POST = null; |
||
27 | public $SERVER = null; |
||
28 | public $ENV = null; |
||
29 | |||
30 | /** |
||
31 | * Constructor |
||
32 | * |
||
33 | * If a $uri is passed, the object will attempt to populate itself using |
||
34 | * that information. |
||
35 | * |
||
36 | * @param string|Zend_Uri $uri |
||
37 | * @return void |
||
38 | * @throws Zend_Controller_Request_Exception when invalid URI passed |
||
39 | */ |
||
40 | public function __construct($uri = null) { |
||
56 | |||
57 | /** |
||
58 | * Access values contained in the superglobals as public members |
||
59 | * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV |
||
60 | * |
||
61 | * @see http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx |
||
62 | * @param string $key |
||
63 | * @return mixed |
||
64 | */ |
||
65 | public function __get($key) { |
||
87 | |||
88 | /** |
||
89 | * Check to see if a property is set |
||
90 | * |
||
91 | * @param string $key |
||
92 | * @return boolean |
||
93 | */ |
||
94 | public function __isset($key) { |
||
112 | |||
113 | /** |
||
114 | * Set GET values |
||
115 | * |
||
116 | * @param string|array $spec |
||
117 | * @param null|mixed $value |
||
118 | * @return Zend_Controller_Request_Http |
||
119 | */ |
||
120 | public function setQuery($spec, $value = null) { |
||
134 | |||
135 | /** |
||
136 | * Retrieve a member of the $_GET superglobal |
||
137 | * |
||
138 | * If no $key is passed, returns the entire $_GET array. |
||
139 | * |
||
140 | * @todo How to retrieve from nested arrays |
||
141 | * @param string $key |
||
142 | * @param mixed $default Default value to use if key not found |
||
143 | * @return mixed Returns null if key does not exist |
||
144 | */ |
||
145 | public function getQuery($key = null, $default = null) { |
||
151 | |||
152 | /** |
||
153 | * Retrieve a member of the $_POST superglobal |
||
154 | * |
||
155 | * If no $key is passed, returns the entire $_POST array. |
||
156 | * |
||
157 | * @todo How to retrieve from nested arrays |
||
158 | * @param string $key |
||
159 | * @param mixed $default Default value to use if key not found |
||
160 | * @return mixed Returns null if key does not exist |
||
161 | */ |
||
162 | View Code Duplication | public function getPost($key = null, $default = null) { |
|
163 | if (null === $key) { |
||
164 | return $this->POST; |
||
165 | } |
||
166 | |||
167 | return (isset($this->POST[$key])) ? $this->POST[$key] : $default; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Retrieve a member of the $_SERVER superglobal |
||
172 | * |
||
173 | * If no $key is passed, returns the entire $_SERVER array. |
||
174 | * |
||
175 | * @param string $key |
||
176 | * @param mixed $default Default value to use if key not found |
||
177 | * @return mixed Returns null if key does not exist |
||
178 | */ |
||
179 | View Code Duplication | public function getServer($key = null, $default = null) { |
|
180 | if (null === $key) { |
||
181 | return $this->SERVER; |
||
182 | } |
||
183 | |||
184 | return (isset($this->SERVER[$key])) ? $this->SERVER[$key] : $default; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Retrieve a member of the $_ENV superglobal |
||
189 | * |
||
190 | * If no $key is passed, returns the entire $_ENV array. |
||
191 | * |
||
192 | * @param string $key |
||
193 | * @param mixed $default Default value to use if key not found |
||
194 | * @return mixed Returns null if key does not exist |
||
195 | */ |
||
196 | View Code Duplication | public function getEnv($key = null, $default = null) { |
|
197 | if (null === $key) { |
||
198 | return $this->ENV; |
||
199 | } |
||
200 | |||
201 | return (isset($this->ENV[$key])) ? $this->ENV[$key] : $default; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Return the value of the given HTTP header. Pass the header name as the |
||
206 | * plain, HTTP-specified header name. Ex.: Ask for 'Accept' to get the |
||
207 | * Accept header, 'Accept-Encoding' to get the Accept-Encoding header. |
||
208 | * |
||
209 | * @param string $header HTTP header name |
||
210 | * @return string|false HTTP header value, or false if not found |
||
211 | * @throws Zend_Controller_Request_Exception |
||
212 | */ |
||
213 | public function getHeader($header) { |
||
242 | |||
243 | /** |
||
244 | * Set the REQUEST_URI on which the instance operates |
||
245 | * |
||
246 | * If no request URI is passed, uses the value in $_SERVER['REQUEST_URI'], |
||
247 | * $_SERVER['HTTP_X_REWRITE_URL'], or $_SERVER['ORIG_PATH_INFO'] + $_SERVER['QUERY_STRING']. |
||
248 | * |
||
249 | * @param string $requestUri |
||
250 | * @return Zend_Controller_Request_Http |
||
251 | */ |
||
252 | public function setRequestUri($requestUri = null) { |
||
294 | |||
295 | /** |
||
296 | * Set the base URL of the request; i.e., the segment leading to the script name |
||
297 | * |
||
298 | * E.g.: |
||
299 | * - /admin |
||
300 | * - /myapp |
||
301 | * - /subdir/index.php |
||
302 | * |
||
303 | * Do not use the full URI when providing the base. The following are |
||
304 | * examples of what not to use: |
||
305 | * - http://example.com/admin (should be just /admin) |
||
306 | * - http://example.com/subdir/index.php (should be just /subdir/index.php) |
||
307 | * |
||
308 | * If no $baseUrl is provided, attempts to determine the base URL from the |
||
309 | * environment, using SCRIPT_FILENAME, SCRIPT_NAME, PHP_SELF, and |
||
310 | * ORIG_SCRIPT_NAME in its determination. |
||
311 | * |
||
312 | * @param mixed $baseUrl |
||
313 | * @return Zend_Controller_Request_Http |
||
314 | */ |
||
315 | public function setBaseUrl($baseUrl = null) { |
||
386 | |||
387 | /** |
||
388 | * Set the base path for the URL |
||
389 | * |
||
390 | * @param string|null $basePath |
||
391 | * @return Zend_Controller_Request_Http |
||
392 | */ |
||
393 | public function setBasePath($basePath = null) { |
||
394 | if ($basePath === null) { |
||
395 | $filename = (isset($this->SERVER['SCRIPT_FILENAME'])) |
||
396 | ? basename($this->SERVER['SCRIPT_FILENAME']) |
||
397 | : ''; |
||
398 | |||
399 | $baseUrl = $this->getBaseUrl(); |
||
400 | if (empty($baseUrl)) { |
||
401 | $this->_basePath = ''; |
||
402 | return $this; |
||
403 | } |
||
404 | |||
405 | if (basename($baseUrl) === $filename) { |
||
406 | $basePath = dirname($baseUrl); |
||
407 | } else { |
||
408 | $basePath = $baseUrl; |
||
409 | } |
||
410 | } |
||
411 | |||
412 | if (substr(PHP_OS, 0, 3) === 'WIN') { |
||
413 | $basePath = str_replace('\\', '/', $basePath); |
||
414 | } |
||
415 | |||
416 | $this->_basePath = rtrim($basePath, '/'); |
||
417 | return $this; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Retrieve a parameter |
||
422 | * |
||
423 | * Retrieves a parameter from the instance. Priority is in the order of |
||
424 | * userland parameters (see {@link setParam()}), $_GET, $_POST. If a |
||
425 | * parameter matching the $key is not found, null is returned. |
||
426 | * |
||
427 | * If the $key is an alias, the actual key aliased will be used. |
||
428 | * |
||
429 | * @param mixed $key |
||
430 | * @param mixed $default Default value to use if key not found |
||
431 | * @return mixed |
||
432 | */ |
||
433 | public function getParam($key, $default = null) { |
||
434 | $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key; |
||
435 | |||
436 | $paramSources = $this->getParamSources(); |
||
437 | if (isset($this->_params[$keyName])) { |
||
438 | return $this->_params[$keyName]; |
||
439 | } elseif (in_array('_GET', $paramSources) && (isset($this->GET[$keyName]))) { |
||
440 | return $this->GET[$keyName]; |
||
441 | } elseif (in_array('_POST', $paramSources) && (isset($this->POST[$keyName]))) { |
||
442 | return $this->POST[$keyName]; |
||
443 | } |
||
444 | |||
445 | return $default; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Retrieve an array of parameters |
||
450 | * |
||
451 | * Retrieves a merged array of parameters, with precedence of userland |
||
452 | * params (see {@link setParam()}), $_GET, $_POST (i.e., values in the |
||
453 | * userland params will take precedence over all others). |
||
454 | * |
||
455 | * @return array |
||
456 | */ |
||
457 | public function getParams() { |
||
458 | $return = $this->_params; |
||
459 | $paramSources = $this->getParamSources(); |
||
460 | View Code Duplication | if (in_array('_GET', $paramSources) |
|
461 | && isset($this->GET) |
||
462 | && is_array($this->GET) |
||
463 | ) { |
||
464 | $return += $this->GET; |
||
465 | } |
||
466 | View Code Duplication | if (in_array('_POST', $paramSources) |
|
467 | && isset($this->POST) |
||
468 | && is_array($this->POST) |
||
469 | ) { |
||
470 | $return += $this->POST; |
||
471 | } |
||
472 | return $return; |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Retrieve HTTP HOST |
||
477 | * |
||
478 | * @param bool $trimPort |
||
479 | * @return string |
||
480 | */ |
||
481 | public function getHttpHost($trimPort = true) { |
||
491 | |||
492 | /** |
||
493 | * Set a member of the $_POST superglobal |
||
494 | * |
||
495 | * @param string|array $key |
||
496 | * @param mixed $value |
||
497 | * |
||
498 | * @return Mage_Core_Controller_Request_Http |
||
499 | */ |
||
500 | public function setPost($key, $value = null) { |
||
508 | |||
509 | /** |
||
510 | * Init the fake superglobal vars |
||
511 | * |
||
512 | * @return null |
||
513 | */ |
||
514 | protected function _initFakeSuperGlobals() { |
||
515 | $this->GET = array(); |
||
516 | $this->POST = $_POST; |
||
517 | $this->SERVER = $_SERVER; |
||
518 | $this->ENV = $_ENV; |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Fix up the fake superglobal vars |
||
523 | * |
||
524 | * @param string $uri |
||
525 | * @return null |
||
526 | */ |
||
527 | protected function _fixupFakeSuperGlobals($uri) { |
||
554 | |||
555 | /** |
||
556 | * Check this request against the cms, standard, and default routers to fill |
||
557 | * the module/controller/action/route fields. |
||
558 | * |
||
559 | * TODO: This whole thing is a gigantic hack. Would be nice to have a |
||
560 | * better solution. |
||
561 | * |
||
562 | * @return null |
||
563 | */ |
||
564 | public function fakeRouterDispatch() { |
||
575 | |||
576 | /** |
||
577 | * |
||
578 | * |
||
579 | * @return boolean |
||
580 | */ |
||
581 | protected function _defaultRouterMatch() { |
||
582 | $noRoute = explode('/', Mage::app()->getStore()->getConfig('web/default/no_route')); |
||
583 | $moduleName = isset($noRoute[0]) ? $noRoute[0] : 'core'; |
||
584 | $controllerName = isset($noRoute[1]) ? $noRoute[1] : 'index'; |
||
585 | $actionName = isset($noRoute[2]) ? $noRoute[2] : 'index'; |
||
586 | |||
587 | if (Mage::app()->getStore()->isAdmin()) { |
||
588 | $adminFrontName = (string) Mage::getConfig()->getNode('admin/routers/adminhtml/args/frontName'); |
||
589 | if ($adminFrontName != $moduleName) { |
||
590 | $moduleName = 'core'; |
||
591 | $controllerName = 'index'; |
||
592 | $actionName = 'noRoute'; |
||
593 | Mage::app()->setCurrentStore(Mage::app()->getDefaultStoreView()); |
||
594 | } |
||
595 | } |
||
596 | |||
597 | $this->setModuleName($moduleName) |
||
598 | ->setControllerName($controllerName) |
||
599 | ->setActionName($actionName); |
||
600 | |||
601 | return true; |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * |
||
606 | * |
||
607 | * @return bool |
||
608 | */ |
||
609 | protected function _standardRouterMatch() { |
||
763 | |||
764 | /** |
||
765 | * |
||
766 | * |
||
767 | * @return bool |
||
768 | */ |
||
769 | protected function _cmsRouterMatch() { |
||
813 | } |
||
814 |
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.