Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 #1034
  • Loading branch information...
commit c724f0739acf6a601a81e0d0560b9fe11f70f71b 1 parent 554d579
@krolow krolow authored markstory committed
View
50 lib/Cake/Console/Shell.php
@@ -167,18 +167,10 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {
}
$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 @@ protected function _welcome() {
* @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;
+ $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;
+ foreach ($uses as $modelClass) {
+ list($plugin, $modelClass) = pluginSplit($modelClass, true);
+ $this->{$modelClass} = ClassRegistry::init($plugin . $modelClass);
}
- return false;
+
+ return true;
}
/**
@@ -682,10 +672,10 @@ public function createFile($path, $contents) {
$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;
}
+
+ $this->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2);
+ return false;
}
/**
View
6 lib/Cake/Console/ShellDispatcher.php
@@ -48,12 +48,10 @@ class ShellDispatcher {
*/
public function __construct($args = array(), $bootstrap = true) {
set_time_limit(0);
-
- if ($bootstrap) {
- $this->_initConstants();
- }
$this->parseParams($args);
+
if ($bootstrap) {
+ $this->_initConstants();
$this->_initEnvironment();
}
}
View
13 lib/Cake/Console/TaskCollection.php
@@ -63,14 +63,15 @@ public function load($task, $settings = array()) {
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(
Please sign in to comment.
Something went wrong with that request. Please try again.