Skip to content

Commit

Permalink
Refactor code.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 24, 2014
1 parent 7ab75a9 commit 96bd2c6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/Console/Command/CompletionShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function options() {
*
* @return void
*/
public function subCommands() {
public function subcommands() {
if (!$this->args) {
return $this->_output();
}
Expand All @@ -86,7 +86,7 @@ public function subCommands() {

/**
* Guess autocomplete from the whole argument string
*
*
* @return void
*/
public function fuzzy() {
Expand Down Expand Up @@ -132,6 +132,8 @@ public function getOptionParser() {
]
]
]
])->addSubcommand('fuzzy', [
'help' => __d('cake_console', 'Guess autocomplete')
])->epilog([
__d('cake_console', 'This command is not intended to be called manually'),
]);
Expand Down
1 change: 1 addition & 0 deletions src/Console/ConsoleOptionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ public function subcommands() {
*/
public function parse($argv, $command = null) {
if (isset($this->_subcommands[$command]) && $this->_subcommands[$command]->parser()) {
array_shift($argv);
return $this->_subcommands[$command]->parser()->parse($argv);
}
$params = $args = [];
Expand Down
16 changes: 6 additions & 10 deletions src/Console/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ public function runCommand($command, $argv) {
try {
list($this->params, $this->args) = $this->OptionParser->parse($argv, $command);
} catch (Error\ConsoleException $e) {
$this->err('<error>Error: ' . $e->getMessage() . '</error>');
$this->out($this->OptionParser->help($command));
return false;
}
Expand All @@ -364,27 +365,22 @@ public function runCommand($command, $argv) {
}

$subcommands = $this->OptionParser->subcommands();

// Method when there are no subcommands.
$isMethod = $this->hasMethod($command);
if ($isMethod && count($subcommands) === 0) {
array_shift($this->args);
$this->startup();
return call_user_func_array([$this, $command], $this->args);
}

// Method when there is a subcommand
if ($isMethod && isset($subcommands[$command])) {
if (
$isMethod &&
(count($subcommands) === 0 || isset($subcommands[$command]))
) {
array_shift($this->args);
$this->startup();
return call_user_func_array([$this, $command], $this->args);
}

// Exposed task.
if ($this->hasTask($command) && isset($subcommands[$command])) {
array_shift($argv);
$this->startup();
$command = Inflector::camelize($command);
// TODO this is suspicious.
return $this->{$command}->runCommand('execute', $argv);
}

Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase/Console/Command/CompletionShellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function testOptions() {
* @return void
*/
public function testSubCommandsCorePlugin() {
$this->Shell->runCommand('subCommands', array('subCommands', 'CORE.bake'));
$this->Shell->runCommand('subcommands', array('subcommands', 'CORE.bake'));
$output = $this->out->output;

$expected = "behavior component controller fixture helper model plugin project shell test view widget zerg\n";
Expand All @@ -174,7 +174,7 @@ public function testSubCommandsCorePlugin() {
* @return void
*/
public function testSubCommandsAppPlugin() {
$this->Shell->runCommand('subCommands', array('subCommands', 'app.sample'));
$this->Shell->runCommand('subcommands', array('subcommands', 'app.sample'));
$output = $this->out->output;

$expected = '';
Expand All @@ -187,7 +187,7 @@ public function testSubCommandsAppPlugin() {
* @return void
*/
public function testSubCommandsPlugin() {
$this->Shell->runCommand('subCommands', array('subCommands', 'TestPluginTwo.welcome'));
$this->Shell->runCommand('subcommands', array('subcommands', 'TestPluginTwo.welcome'));
$output = $this->out->output;

$expected = "say_hello\n";
Expand All @@ -200,7 +200,7 @@ public function testSubCommandsPlugin() {
* @return void
*/
public function testSubCommandsNoArguments() {
$this->Shell->runCommand('subCommands', array());
$this->Shell->runCommand('subcommands', array());
$output = $this->out->output;

$expected = '';
Expand All @@ -213,7 +213,7 @@ public function testSubCommandsNoArguments() {
* @return void
*/
public function testSubCommandsNonExistingCommand() {
$this->Shell->runCommand('subCommands', array('subCommands', 'foo'));
$this->Shell->runCommand('subcommands', array('subcommands', 'foo'));
$output = $this->out->output;

$expected = '';
Expand All @@ -226,7 +226,7 @@ public function testSubCommandsNonExistingCommand() {
* @return void
*/
public function testSubCommands() {
$this->Shell->runCommand('subCommands', array('subCommands', 'bake'));
$this->Shell->runCommand('subcommands', array('subcommands', 'bake'));
$output = $this->out->output;

$expected = "behavior component controller fixture helper model plugin project shell test view widget zerg\n";
Expand Down

0 comments on commit 96bd2c6

Please sign in to comment.