Skip to content

Commit

Permalink
Clean up code.
Browse files Browse the repository at this point in the history
* 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
krolow authored and markstory committed Dec 21, 2012
1 parent 554d579 commit c724f07
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 40 deletions.
50 changes: 20 additions & 30 deletions lib/Cake/Console/Shell.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
6 changes: 2 additions & 4 deletions lib/Cake/Console/ShellDispatcher.php
Expand Up @@ -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();
}
}
Expand Down
13 changes: 7 additions & 6 deletions lib/Cake/Console/TaskCollection.php
Expand Up @@ -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(
Expand Down

0 comments on commit c724f07

Please sign in to comment.