Skip to content
This repository
Browse code

Simplify the ClassRegistry by removing else, early returns

  • Loading branch information...
commit dc3995f939aff9e9dad2f7b8830730a1336fe8d4 1 parent 25b4471
Carl Sutton authored September 13, 2012

Showing 1 changed file with 20 additions and 23 deletions. Show diff stats Hide diff stats

  1. 43  lib/Cake/Utility/ClassRegistry.php
43  lib/Cake/Utility/ClassRegistry.php
@@ -94,8 +94,6 @@ public static function &getInstance() {
94 94
  */
95 95
 	public static function init($class, $strict = false) {
96 96
 		$_this = ClassRegistry::getInstance();
97  
-		$false = false;
98  
-		$true = true;
99 97
 
100 98
 		if (is_array($class)) {
101 99
 			$objects = $class;
@@ -105,11 +103,19 @@ public static function init($class, $strict = false) {
105 103
 		} else {
106 104
 			$objects = array(array('class' => $class));
107 105
 		}
108  
-		$defaults = isset($_this->_config['Model']) ? $_this->_config['Model'] : array();
  106
+		$defaults = array();
  107
+		if (isset($_this->_config['Model'])) {
  108
+			$defaults = $_this->_config['Model'];
  109
+		}
109 110
 		$count = count($objects);
110 111
 		$availableDs = array_keys(ConnectionManager::enumConnectionObjects());
111 112
 
112 113
 		foreach ($objects as $key => $settings) {
  114
+			if (is_numeric($settings)) {
  115
+				trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
  116
+				return false;
  117
+			}
  118
+
113 119
 			if (is_array($settings)) {
114 120
 				$pluginPath = null;
115 121
 				$settings = array_merge($defaults, $settings);
@@ -125,7 +131,8 @@ public static function init($class, $strict = false) {
125 131
 				}
126 132
 				$alias = $settings['alias'];
127 133
 
128  
-				if ($model = $_this->_duplicate($alias, $class)) {
  134
+				$model = $_this->_duplicate($alias, $class);
  135
+				if ($model) {
129 136
 					$_this->map($alias, $class);
130 137
 					return $model;
131 138
 				}
@@ -157,17 +164,16 @@ public static function init($class, $strict = false) {
157 164
 					} else {
158 165
 						$instance = $reflection->newInstance();
159 166
 					}
160  
-					if ($strict) {
161  
-						$instance = ($instance instanceof Model) ? $instance : null;
  167
+					if ($strict && !$instance instanceof Model) {
  168
+						$instance =  null;
162 169
 					}
163 170
 				}
164 171
 				if (!isset($instance)) {
  172
+					$appModel = 'AppModel';
165 173
 					if ($strict) {
166 174
 						return false;
167 175
 					} elseif ($plugin && class_exists($plugin . 'AppModel')) {
168 176
 						$appModel = $plugin . 'AppModel';
169  
-					} else {
170  
-						$appModel = 'AppModel';
171 177
 					}
172 178
 					if (!empty($appModel)) {
173 179
 						$settings['name'] = $class;
@@ -176,18 +182,15 @@ public static function init($class, $strict = false) {
176 182
 
177 183
 					if (!isset($instance)) {
178 184
 						trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
179  
-						return $false;
  185
+						return false;
180 186
 					}
181 187
 				}
182 188
 				$_this->map($alias, $class);
183  
-			} elseif (is_numeric($settings)) {
184  
-				trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
185  
-				return $false;
186 189
 			}
187 190
 		}
188 191
 
189 192
 		if ($count > 1) {
190  
-			return $true;
  193
+			return true;
191 194
 		}
192 195
 		return $instance;
193 196
 	}
@@ -232,12 +235,8 @@ public static function removeObject($key) {
232 235
 	public static function isKeySet($key) {
233 236
 		$_this = ClassRegistry::getInstance();
234 237
 		$key = Inflector::underscore($key);
235  
-		if (isset($_this->_objects[$key])) {
236  
-			return true;
237  
-		} elseif (isset($_this->_map[$key])) {
238  
-			return true;
239  
-		}
240  
-		return false;
  238
+
  239
+		return isset($_this->_objects[$key]) || isset($_this->_map[$key]);
241 240
 	}
242 241
 
243 242
 /**
@@ -246,8 +245,7 @@ public static function isKeySet($key) {
246 245
  * @return array Set of keys stored in registry
247 246
  */
248 247
 	public static function keys() {
249  
-		$_this = ClassRegistry::getInstance();
250  
-		return array_keys($_this->_objects);
  248
+		return array_keys(ClassRegistry::getInstance()->_objects);
251 249
 	}
252 250
 
253 251
 /**
@@ -338,8 +336,7 @@ public static function map($key, $name) {
338 336
  * @return array Keys of registry's map
339 337
  */
340 338
 	public static function mapKeys() {
341  
-		$_this = ClassRegistry::getInstance();
342  
-		return array_keys($_this->_map);
  339
+		return array_keys(ClassRegistry::getInstance()->_map);
343 340
 	}
344 341
 
345 342
 /**

0 notes on commit dc3995f

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