Skip to content
This repository
Browse code

Cleaning up the RequestHandlerCompoent

removing redundant code and shifting a few things around so there
are less nested ifs and making things easier to follow.

Removing some variable setting, returning function calls instead.
  • Loading branch information...
commit e09bf024673b9785ed304422ebdfc5bef0fc6ab4 1 parent 25b4471
Carl Sutton authored September 14, 2012
114  lib/Cake/Controller/Component/RequestHandlerComponent.php
@@ -107,8 +107,7 @@ class RequestHandlerComponent extends Component {
107 107
  * @param array $settings Array of settings.
108 108
  */
109 109
 	public function __construct(ComponentCollection $collection, $settings = array()) {
110  
-		$default = array('checkHttpCache' => true);
111  
-		parent::__construct($collection, $settings + $default);
  110
+		parent::__construct($collection, $settings + array('checkHttpCache' => true));
112 111
 		$this->addInputType('xml', array(array($this, 'convertXml')));
113 112
 
114 113
 		$Controller = $collection->getController();
@@ -158,9 +157,11 @@ protected function _setExtension() {
158 157
 		$extensions = Router::extensions();
159 158
 		$preferred = array_shift($accept);
160 159
 		$preferredTypes = $this->response->mapType($preferred);
161  
-		$similarTypes = array_intersect($extensions, $preferredTypes);
162  
-		if (count($similarTypes) === 1 && !in_array('xhtml', $preferredTypes) && !in_array('html', $preferredTypes)) {
163  
-			$this->ext = array_shift($similarTypes);
  160
+		if (!in_array('xhtml', $preferredTypes) && !in_array('html', $preferredTypes)) {
  161
+			$similarTypes = array_intersect($extensions, $preferredTypes);
  162
+			if (count($similarTypes) === 1) {
  163
+				$this->ext = array_shift($similarTypes);
  164
+			}
164 165
 		}
165 166
 	}
166 167
 
@@ -269,8 +270,7 @@ public function beforeRedirect(Controller $controller, $url, $status = null, $ex
269 270
  * @return boolean false if the render process should be aborted
270 271
  **/
271 272
 	public function beforeRender(Controller $controller) {
272  
-		$shouldCheck = $this->settings['checkHttpCache'];
273  
-		if ($shouldCheck && $this->response->checkNotModified($this->request)) {
  273
+		if ($this->settings['checkHttpCache'] && $this->response->checkNotModified($this->request)) {
274 274
 			return false;
275 275
 		}
276 276
 	}
@@ -395,13 +395,11 @@ public function isDelete() {
395 395
  * Gets Prototype version if call is Ajax, otherwise empty string.
396 396
  * The Prototype library sets a special "Prototype version" HTTP header.
397 397
  *
398  
- * @return string Prototype version of component making Ajax call
  398
+ * @return string|boolean When Ajax the prototype version of component making the call otherwise false
399 399
  */
400 400
 	public function getAjaxVersion() {
401  
-		if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
402  
-			return env('HTTP_X_PROTOTYPE_VERSION');
403  
-		}
404  
-		return false;
  401
+		$httpX = env('HTTP_X_PROTOTYPE_VERSION');
  402
+		return ($httpX === null) ? false : $httpX;
405 403
 	}
406 404
 
407 405
 /**
@@ -467,9 +465,10 @@ public function getClientIP($safe = true) {
467 465
 	public function accepts($type = null) {
468 466
 		$accepted = $this->request->accepts();
469 467
 
470  
-		if ($type == null) {
  468
+		if (!$type) {
471 469
 			return $this->mapType($accepted);
472  
-		} elseif (is_array($type)) {
  470
+		}
  471
+		if (is_array($type)) {
473 472
 			foreach ($type as $t) {
474 473
 				$t = $this->mapAlias($t);
475 474
 				if (in_array($t, $accepted)) {
@@ -477,9 +476,9 @@ public function accepts($type = null) {
477 476
 				}
478 477
 			}
479 478
 			return false;
480  
-		} elseif (is_string($type)) {
481  
-			$type = $this->mapAlias($type);
482  
-			return in_array($type, $accepted);
  479
+		}
  480
+		if (is_string($type)) {
  481
+			return in_array($this->mapAlias($type), $accepted);
483 482
 		}
484 483
 		return false;
485 484
 	}
@@ -496,18 +495,20 @@ public function requestedWith($type = null) {
496 495
 		if (!$this->request->is('post') && !$this->request->is('put')) {
497 496
 			return null;
498 497
 		}
499  
-
500  
-		list($contentType) = explode(';', env('CONTENT_TYPE'));
501  
-		if ($type == null) {
502  
-			return $this->mapType($contentType);
503  
-		} elseif (is_array($type)) {
  498
+		if (is_array($type)) {
504 499
 			foreach ($type as $t) {
505 500
 				if ($this->requestedWith($t)) {
506 501
 					return $t;
507 502
 				}
508 503
 			}
509 504
 			return false;
510  
-		} elseif (is_string($type)) {
  505
+		}
  506
+
  507
+		list($contentType) = explode(';', env('CONTENT_TYPE'));
  508
+		if (!$type) {
  509
+			return $this->mapType($contentType);
  510
+		}
  511
+		if (is_string($type)) {
511 512
 			return ($type == $this->mapType($contentType));
512 513
 		}
513 514
 	}
@@ -535,10 +536,9 @@ public function prefers($type = null) {
535 536
 		if (empty($acceptRaw)) {
536 537
 			return $this->ext;
537 538
 		}
538  
-		$accepts = array_shift($acceptRaw);
539  
-		$accepts = $this->mapType($accepts);
  539
+		$accepts = $this->mapType(array_shift($acceptRaw));
540 540
 
541  
-		if ($type == null) {
  541
+		if (!$type) {
542 542
 			if (empty($this->ext) && !empty($accepts)) {
543 543
 				return $accepts[0];
544 544
 			}
@@ -611,8 +611,11 @@ public function renderAs(Controller $controller, $type, $options = array()) {
611 611
 		} elseif (empty($this->_renderType)) {
612 612
 			$controller->viewPath .= DS . $type;
613 613
 		} else {
614  
-			$remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath);
615  
-			$controller->viewPath = $remove;
  614
+			$controller->viewPath = preg_replace(
  615
+				"/([\/\\\\]{$this->_renderType})$/",
  616
+				DS . $type,
  617
+				$controller->viewPath
  618
+			);
616 619
 		}
617 620
 		$this->_renderType = $type;
618 621
 		$controller->layoutPath = $type;
@@ -622,12 +625,8 @@ public function renderAs(Controller $controller, $type, $options = array()) {
622 625
 		}
623 626
 
624 627
 		$helper = ucfirst($type);
625  
-		$isAdded = (
626  
-			in_array($helper, $controller->helpers) ||
627  
-			array_key_exists($helper, $controller->helpers)
628  
-		);
629 628
 
630  
-		if (!$isAdded) {
  629
+		if (!in_array($helper, $controller->helpers) && empty($controller->helpers[$helper])) {
631 630
 			App::uses('AppHelper', 'View/Helper');
632 631
 			App::uses($helper . 'Helper', 'View/Helper');
633 632
 			if (class_exists($helper . 'Helper')) {
@@ -653,39 +652,35 @@ public function respondAs($type, $options = array()) {
653 652
 		$defaults = array('index' => null, 'charset' => null, 'attachment' => false);
654 653
 		$options = $options + $defaults;
655 654
 
  655
+		$cType = $type;
656 656
 		if (strpos($type, '/') === false) {
657 657
 			$cType = $this->response->getMimeType($type);
658  
-			if ($cType === false) {
659  
-				return false;
660  
-			}
661  
-			if (is_array($cType) && isset($cType[$options['index']])) {
  658
+		}
  659
+		if (is_array($cType)) {
  660
+			if (isset($cType[$options['index']])) {
662 661
 				$cType = $cType[$options['index']];
663 662
 			}
664  
-			if (is_array($cType)) {
665  
-				if ($this->prefers($cType)) {
666  
-					$cType = $this->prefers($cType);
667  
-				} else {
668  
-					$cType = $cType[0];
669  
-				}
670  
-			}
671  
-		} else {
672  
-			$cType = $type;
673  
-		}
674 663
 
675  
-		if ($cType != null) {
676  
-			if (empty($this->request->params['requested'])) {
677  
-				$this->response->type($cType);
  664
+			if ($this->prefers($cType)) {
  665
+				$cType = $this->prefers($cType);
  666
+			} else {
  667
+				$cType = $cType[0];
678 668
 			}
  669
+		}
679 670
 
680  
-			if (!empty($options['charset'])) {
681  
-				$this->response->charset($options['charset']);
682  
-			}
683  
-			if (!empty($options['attachment'])) {
684  
-				$this->response->download($options['attachment']);
685  
-			}
686  
-			return true;
  671
+		if (!$type) {
  672
+			return false;
687 673
 		}
688  
-		return false;
  674
+		if (empty($this->request->params['requested'])) {
  675
+			$this->response->type($cType);
  676
+		}
  677
+		if (!empty($options['charset'])) {
  678
+			$this->response->charset($options['charset']);
  679
+		}
  680
+		if (!empty($options['attachment'])) {
  681
+			$this->response->download($options['attachment']);
  682
+		}
  683
+		return true;
689 684
 	}
690 685
 
691 686
 /**
@@ -758,7 +753,8 @@ public function addInputType($type, $handler) {
758 753
 	public function viewClassMap($type = null, $viewClass = null) {
759 754
 		if (!$viewClass && is_string($type) && isset($this->_viewClassMap[$type])) {
760 755
 			return $this->_viewClassMap[$type];
761  
-		} elseif (is_string($type)) {
  756
+		}
  757
+		if (is_string($type)) {
762 758
 			$this->_viewClassMap[$type] = $viewClass;
763 759
 		} elseif (is_array($type)) {
764 760
 			foreach ($type as $key => $value) {

0 notes on commit e09bf02

Please sign in to comment.
Something went wrong with that request. Please try again.