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->stdout = $stdout;
$this->stderr = $stderr;
$this->stdin = $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->stdout = $stdout ? $stdout : new ConsoleOutput('php://stdout');
$this->stderr = $stderr ? $stderr : new ConsoleOutput('php://stderr');
$this->stdin = $stdin ? $stdin : new ConsoleInput('php://stdin');
$this->_useLogger();
$parent = get_parent_class($this);
if ($this->tasks !== null && $this->tasks !== false) {
@ -238,12 +230,11 @@ class Shell extends Object {
* @return boolean
*/
protected function _loadModels() {
if ($this->uses === null || $this->uses === false) {
return;
if (empty($this->uses)) {
return false;
}
App::uses('ClassRegistry', 'Utility');
if ($this->uses !== true && !empty($this->uses)) {
$uses = is_array($this->uses) ? $this->uses : array($this->uses);
$modelClassName = $uses[0];
@ -256,10 +247,9 @@ class Shell extends Object {
list($plugin, $modelClass) = pluginSplit($modelClass, true);
$this->{$modelClass} = ClassRegistry::init($plugin . $modelClass);
}
return true;
}
return false;
}
/**
* Loads tasks defined in public $tasks
@ -682,11 +672,11 @@ class Shell extends Object {
$File->write($data);
$this->out(__d('cake_console', '<success>Wrote</success> `%s`', $path));
return true;
} else {
}
$this->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2);
return false;
}
}
/**
* Action to create a Unit Test

View file

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

View file

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