Clean up code.

* It is not need to check the same thing twice
* One if condition instead of two ifs
* Ternary codition instead of checking if variable is null
* Returning early to avoid unnecessary conditional levels
* It is not needed to put an else as the if before already has returned
* Changing ternary conditional to PHP 5.2
* Removing some not needed conditions in the if, also changed to return false (instead of NULL) to follow the code block docs
* Changing to check before if the class_exists so we make sure that class was autoloaded before the condition

Squash of commits in https://github.com/cakephp/cakephp/pull/1034
This commit is contained in:
Vinícius Krolow 2012-12-18 02:38:47 -02:00 committed by mark_story
parent 554d5794ce
commit c724f0739a
3 changed files with 30 additions and 41 deletions

View file

@ -167,18 +167,10 @@ class Shell extends Object {
} }
$this->Tasks = new TaskCollection($this); $this->Tasks = new TaskCollection($this);
$this->stdout = $stdout; $this->stdout = $stdout ? $stdout : new ConsoleOutput('php://stdout');
$this->stderr = $stderr; $this->stderr = $stderr ? $stderr : new ConsoleOutput('php://stderr');
$this->stdin = $stdin; $this->stdin = $stdin ? $stdin : new ConsoleInput('php://stdin');
if (!$this->stdout) {
$this->stdout = new ConsoleOutput('php://stdout');
}
if (!$this->stderr) {
$this->stderr = new ConsoleOutput('php://stderr');
}
if (!$this->stdin) {
$this->stdin = new ConsoleInput('php://stdin');
}
$this->_useLogger(); $this->_useLogger();
$parent = get_parent_class($this); $parent = get_parent_class($this);
if ($this->tasks !== null && $this->tasks !== false) { if ($this->tasks !== null && $this->tasks !== false) {
@ -238,27 +230,25 @@ class Shell extends Object {
* @return boolean * @return boolean
*/ */
protected function _loadModels() { protected function _loadModels() {
if ($this->uses === null || $this->uses === false) { if (empty($this->uses)) {
return; return false;
} }
App::uses('ClassRegistry', 'Utility'); App::uses('ClassRegistry', 'Utility');
if ($this->uses !== true && !empty($this->uses)) { $uses = is_array($this->uses) ? $this->uses : array($this->uses);
$uses = is_array($this->uses) ? $this->uses : array($this->uses);
$modelClassName = $uses[0]; $modelClassName = $uses[0];
if (strpos($uses[0], '.') !== false) { if (strpos($uses[0], '.') !== false) {
list($plugin, $modelClassName) = explode('.', $uses[0]); list($plugin, $modelClassName) = explode('.', $uses[0]);
}
$this->modelClass = $modelClassName;
foreach ($uses as $modelClass) {
list($plugin, $modelClass) = pluginSplit($modelClass, true);
$this->{$modelClass} = ClassRegistry::init($plugin . $modelClass);
}
return true;
} }
return false; $this->modelClass = $modelClassName;
foreach ($uses as $modelClass) {
list($plugin, $modelClass) = pluginSplit($modelClass, true);
$this->{$modelClass} = ClassRegistry::init($plugin . $modelClass);
}
return true;
} }
/** /**
@ -682,10 +672,10 @@ class Shell extends Object {
$File->write($data); $File->write($data);
$this->out(__d('cake_console', '<success>Wrote</success> `%s`', $path)); $this->out(__d('cake_console', '<success>Wrote</success> `%s`', $path));
return true; return true;
} else {
$this->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2);
return false;
} }
$this->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2);
return false;
} }
/** /**

View file

@ -48,12 +48,10 @@ class ShellDispatcher {
*/ */
public function __construct($args = array(), $bootstrap = true) { public function __construct($args = array(), $bootstrap = true) {
set_time_limit(0); set_time_limit(0);
$this->parseParams($args);
if ($bootstrap) { if ($bootstrap) {
$this->_initConstants(); $this->_initConstants();
}
$this->parseParams($args);
if ($bootstrap) {
$this->_initEnvironment(); $this->_initEnvironment();
} }
} }

View file

@ -63,14 +63,15 @@ class TaskCollection extends ObjectCollection {
if (isset($this->_loaded[$name])) { if (isset($this->_loaded[$name])) {
return $this->_loaded[$name]; return $this->_loaded[$name];
} }
$taskClass = $name . 'Task'; $taskClass = $name . 'Task';
App::uses($taskClass, $plugin . 'Console/Command/Task'); App::uses($taskClass, $plugin . 'Console/Command/Task');
if (!class_exists($taskClass)) {
if (!class_exists($taskClass)) { $exists = class_exists($taskClass);
throw new MissingTaskException(array( if (!$exists) {
'class' => $taskClass throw new MissingTaskException(array(
)); 'class' => $taskClass
} ));
} }
$this->_loaded[$name] = new $taskClass( $this->_loaded[$name] = new $taskClass(