Simplify the ClassRegistry by removing else, early returns

This commit is contained in:
dogmatic69 2012-09-13 07:02:55 +01:00
parent 25b447149d
commit dc3995f939

View file

@ -94,8 +94,6 @@ class ClassRegistry {
*/ */
public static function init($class, $strict = false) { public static function init($class, $strict = false) {
$_this = ClassRegistry::getInstance(); $_this = ClassRegistry::getInstance();
$false = false;
$true = true;
if (is_array($class)) { if (is_array($class)) {
$objects = $class; $objects = $class;
@ -105,11 +103,19 @@ class ClassRegistry {
} else { } else {
$objects = array(array('class' => $class)); $objects = array(array('class' => $class));
} }
$defaults = isset($_this->_config['Model']) ? $_this->_config['Model'] : array(); $defaults = array();
if (isset($_this->_config['Model'])) {
$defaults = $_this->_config['Model'];
}
$count = count($objects); $count = count($objects);
$availableDs = array_keys(ConnectionManager::enumConnectionObjects()); $availableDs = array_keys(ConnectionManager::enumConnectionObjects());
foreach ($objects as $key => $settings) { foreach ($objects as $key => $settings) {
if (is_numeric($settings)) {
trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
return false;
}
if (is_array($settings)) { if (is_array($settings)) {
$pluginPath = null; $pluginPath = null;
$settings = array_merge($defaults, $settings); $settings = array_merge($defaults, $settings);
@ -125,7 +131,8 @@ class ClassRegistry {
} }
$alias = $settings['alias']; $alias = $settings['alias'];
if ($model = $_this->_duplicate($alias, $class)) { $model = $_this->_duplicate($alias, $class);
if ($model) {
$_this->map($alias, $class); $_this->map($alias, $class);
return $model; return $model;
} }
@ -157,17 +164,16 @@ class ClassRegistry {
} else { } else {
$instance = $reflection->newInstance(); $instance = $reflection->newInstance();
} }
if ($strict) { if ($strict && !$instance instanceof Model) {
$instance = ($instance instanceof Model) ? $instance : null; $instance = null;
} }
} }
if (!isset($instance)) { if (!isset($instance)) {
$appModel = 'AppModel';
if ($strict) { if ($strict) {
return false; return false;
} elseif ($plugin && class_exists($plugin . 'AppModel')) { } elseif ($plugin && class_exists($plugin . 'AppModel')) {
$appModel = $plugin . 'AppModel'; $appModel = $plugin . 'AppModel';
} else {
$appModel = 'AppModel';
} }
if (!empty($appModel)) { if (!empty($appModel)) {
$settings['name'] = $class; $settings['name'] = $class;
@ -176,18 +182,15 @@ class ClassRegistry {
if (!isset($instance)) { if (!isset($instance)) {
trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING); trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
return $false; return false;
} }
} }
$_this->map($alias, $class); $_this->map($alias, $class);
} elseif (is_numeric($settings)) {
trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
return $false;
} }
} }
if ($count > 1) { if ($count > 1) {
return $true; return true;
} }
return $instance; return $instance;
} }
@ -232,12 +235,8 @@ class ClassRegistry {
public static function isKeySet($key) { public static function isKeySet($key) {
$_this = ClassRegistry::getInstance(); $_this = ClassRegistry::getInstance();
$key = Inflector::underscore($key); $key = Inflector::underscore($key);
if (isset($_this->_objects[$key])) {
return true; return isset($_this->_objects[$key]) || isset($_this->_map[$key]);
} elseif (isset($_this->_map[$key])) {
return true;
}
return false;
} }
/** /**
@ -246,8 +245,7 @@ class ClassRegistry {
* @return array Set of keys stored in registry * @return array Set of keys stored in registry
*/ */
public static function keys() { public static function keys() {
$_this = ClassRegistry::getInstance(); return array_keys(ClassRegistry::getInstance()->_objects);
return array_keys($_this->_objects);
} }
/** /**
@ -338,8 +336,7 @@ class ClassRegistry {
* @return array Keys of registry's map * @return array Keys of registry's map
*/ */
public static function mapKeys() { public static function mapKeys() {
$_this = ClassRegistry::getInstance(); return array_keys(ClassRegistry::getInstance()->_map);
return array_keys($_this->_map);
} }
/** /**