Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify the ClassRegistry by removing else, early returns

  • Loading branch information...
commit dc3995f939aff9e9dad2f7b8830730a1336fe8d4 1 parent 25b4471
@dogmatic69 dogmatic69 authored
Showing with 20 additions and 23 deletions.
  1. +20 −23 lib/Cake/Utility/ClassRegistry.php
View
43 lib/Cake/Utility/ClassRegistry.php
@@ -94,8 +94,6 @@ public static function &getInstance() {
*/
public static function init($class, $strict = false) {
$_this = ClassRegistry::getInstance();
- $false = false;
- $true = true;
if (is_array($class)) {
$objects = $class;
@@ -105,11 +103,19 @@ public static function init($class, $strict = false) {
} else {
$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);
$availableDs = array_keys(ConnectionManager::enumConnectionObjects());
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)) {
$pluginPath = null;
$settings = array_merge($defaults, $settings);
@@ -125,7 +131,8 @@ public static function init($class, $strict = false) {
}
$alias = $settings['alias'];
- if ($model = $_this->_duplicate($alias, $class)) {
+ $model = $_this->_duplicate($alias, $class);
+ if ($model) {
$_this->map($alias, $class);
return $model;
}
@@ -157,17 +164,16 @@ public static function init($class, $strict = false) {
} else {
$instance = $reflection->newInstance();
}
- if ($strict) {
- $instance = ($instance instanceof Model) ? $instance : null;
+ if ($strict && !$instance instanceof Model) {
+ $instance = null;
}
}
if (!isset($instance)) {
+ $appModel = 'AppModel';
if ($strict) {
return false;
} elseif ($plugin && class_exists($plugin . 'AppModel')) {
$appModel = $plugin . 'AppModel';
- } else {
- $appModel = 'AppModel';
}
if (!empty($appModel)) {
$settings['name'] = $class;
@@ -176,18 +182,15 @@ public static function init($class, $strict = false) {
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);
- return $false;
+ return false;
}
}
$_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) {
- return $true;
+ return true;
}
return $instance;
}
@@ -232,12 +235,8 @@ public static function removeObject($key) {
public static function isKeySet($key) {
$_this = ClassRegistry::getInstance();
$key = Inflector::underscore($key);
- if (isset($_this->_objects[$key])) {
- return true;
- } elseif (isset($_this->_map[$key])) {
- return true;
- }
- return false;
+
+ return isset($_this->_objects[$key]) || isset($_this->_map[$key]);
}
/**
@@ -246,8 +245,7 @@ public static function isKeySet($key) {
* @return array Set of keys stored in registry
*/
public static function keys() {
- $_this = ClassRegistry::getInstance();
- return array_keys($_this->_objects);
+ return array_keys(ClassRegistry::getInstance()->_objects);
}
/**
@@ -338,8 +336,7 @@ public static function map($key, $name) {
* @return array Keys of registry's map
*/
public static function mapKeys() {
- $_this = ClassRegistry::getInstance();
- return array_keys($_this->_map);
+ return array_keys(ClassRegistry::getInstance()->_map);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.