From c724f0739acf6a601a81e0d0560b9fe11f70f71b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Krolow?= Date: Tue, 18 Dec 2012 02:38:47 -0200 Subject: [PATCH] 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 --- lib/Cake/Console/Shell.php | 52 +++++++++++----------------- lib/Cake/Console/ShellDispatcher.php | 6 ++-- lib/Cake/Console/TaskCollection.php | 13 +++---- 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/lib/Cake/Console/Shell.php b/lib/Cake/Console/Shell.php index 065a79e14..9e215a07e 100644 --- a/lib/Cake/Console/Shell.php +++ b/lib/Cake/Console/Shell.php @@ -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,27 +230,25 @@ 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); + $uses = is_array($this->uses) ? $this->uses : array($this->uses); - $modelClassName = $uses[0]; - if (strpos($uses[0], '.') !== false) { - 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; + $modelClassName = $uses[0]; + if (strpos($uses[0], '.') !== false) { + list($plugin, $modelClassName) = explode('.', $uses[0]); } - 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); $this->out(__d('cake_console', 'Wrote `%s`', $path)); return true; - } else { - $this->err(__d('cake_console', 'Could not write to `%s`.', $path), 2); - return false; } + + $this->err(__d('cake_console', 'Could not write to `%s`.', $path), 2); + return false; } /** diff --git a/lib/Cake/Console/ShellDispatcher.php b/lib/Cake/Console/ShellDispatcher.php index 499d8c5d9..22d797da2 100644 --- a/lib/Cake/Console/ShellDispatcher.php +++ b/lib/Cake/Console/ShellDispatcher.php @@ -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(); } } diff --git a/lib/Cake/Console/TaskCollection.php b/lib/Cake/Console/TaskCollection.php index 35f21eecf..95d5ed91b 100644 --- a/lib/Cake/Console/TaskCollection.php +++ b/lib/Cake/Console/TaskCollection.php @@ -63,14 +63,15 @@ 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)) { - throw new MissingTaskException(array( - 'class' => $taskClass - )); - } + + $exists = class_exists($taskClass); + if (!$exists) { + throw new MissingTaskException(array( + 'class' => $taskClass + )); } $this->_loaded[$name] = new $taskClass(