Skip to content

Commit

Permalink
Disallow command names with more than 3 words.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 18, 2018
1 parent 0c8ce43 commit 62f328e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Console/CommandCollection.php
Expand Up @@ -64,6 +64,12 @@ public function add($name, $command)
"Cannot use '$class' for command '$name' it is not a subclass of Cake\Console\Shell or Cake\Console\Command."
);
}
if (!preg_match('/^[\p{L}\p{N}\-_:.]+(?:(?: [\p{L}\p{N}\-_:.]+){1,2})?$/ui', $name)) {
throw new InvalidArgumentException(
"The command name `{$name}` is invalid. " .
'Names must use only letters/numbers + punctuation characters and be 3 words or fewer.'
);
}

$this->commands[$name] = $command;

Expand Down
33 changes: 33 additions & 0 deletions tests/TestCase/Console/CommandCollectionTest.php
Expand Up @@ -136,6 +136,39 @@ public function testAddInvalidInstance()
$collection->add('routes', $shell);
}

/**
* Provider for invalid names.
*
* @return array
*/
public function invalidNameProvider()
{
return [
// Empty
[''],
// Leading spaces
[' spaced'],
// Trailing spaces
['spaced '],
// Too many words
['one two three four'],
];
}

/**
* test adding a command instance.
*
* @dataProvider invalidNameProvider
* @return void
*/
public function testAddCommandInvalidName($name)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid command name');
$collection = new CommandCollection();
$collection->add($name, DemoCommand::class);
}

/**
* Class names that are not shells should fail
*
Expand Down

0 comments on commit 62f328e

Please sign in to comment.