Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3502ca2

Browse files
committed
minor #9244 [Console] make parent constructor test more reliable (Tobion)
This PR was merged into the master branch. Discussion ---------- [Console] make parent constructor test more reliable | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Fixes | #9186 , f2b60e9 | Tests pass? | yes | license? | MIT It also fixes the test since f2b60e9 and improves phpdoc The second commit improves regex performance to validate name (using possesive quantifier). I did some basic performance tests http://3v4l.org/PuvuL The new regex only takes 1/3 of the time compared to the old one! Commits ------- 5798029 [Console] improve regex performance to validate name 22b09ce [Console] make parent constructor test more reliable
2 parents 7455650 + 5798029 commit 3502ca2

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@
5656
*/
5757
class Application
5858
{
59-
private $commands;
59+
private $commands = array();
6060
private $wantHelps = false;
6161
private $runningCommand;
6262
private $name;
6363
private $version;
64-
private $catchExceptions;
65-
private $autoExit;
64+
private $catchExceptions = true;
65+
private $autoExit = true;
6666
private $definition;
6767
private $helperSet;
6868
private $dispatcher;
@@ -80,9 +80,6 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
8080
{
8181
$this->name = $name;
8282
$this->version = $version;
83-
$this->catchExceptions = true;
84-
$this->autoExit = true;
85-
$this->commands = array();
8683
$this->helperSet = $this->getDefaultHelperSet();
8784
$this->definition = $this->getDefaultInputDefinition();
8885

@@ -407,8 +404,8 @@ public function add(Command $command)
407404
return;
408405
}
409406

410-
if (null === $command->getAliases()) {
411-
throw new \LogicException(sprintf('You must call the parent constructor in "%s::__construct()"', get_class($command)));
407+
if (null === $command->getDefinition()) {
408+
throw new \LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
412409
}
413410

414411
$this->commands[$command->getName()] = $command;

src/Symfony/Component/Console/Command/Command.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ class Command
3333
{
3434
private $application;
3535
private $name;
36-
private $aliases;
36+
private $aliases = array();
3737
private $definition;
3838
private $help;
3939
private $description;
40-
private $ignoreValidationErrors;
41-
private $applicationDefinitionMerged;
42-
private $applicationDefinitionMergedWithArgs;
40+
private $ignoreValidationErrors = false;
41+
private $applicationDefinitionMerged = false;
42+
private $applicationDefinitionMergedWithArgs = false;
4343
private $code;
4444
private $synopsis;
4545
private $helperSet;
4646

4747
/**
4848
* Constructor.
4949
*
50-
* @param string $name The name of the command
50+
* @param string|null $name The name of the command; passing null means it must be set in configure()
5151
*
5252
* @throws \LogicException When the command name is empty
5353
*
@@ -56,10 +56,6 @@ class Command
5656
public function __construct($name = null)
5757
{
5858
$this->definition = new InputDefinition();
59-
$this->ignoreValidationErrors = false;
60-
$this->applicationDefinitionMerged = false;
61-
$this->applicationDefinitionMergedWithArgs = false;
62-
$this->aliases = array();
6359

6460
if (null !== $name) {
6561
$this->setName($name);
@@ -402,7 +398,7 @@ public function addOption($name, $shortcut = null, $mode = null, $description =
402398
*
403399
* @return Command The current instance
404400
*
405-
* @throws \InvalidArgumentException When command name given is empty
401+
* @throws \InvalidArgumentException When the name is invalid
406402
*
407403
* @api
408404
*/
@@ -512,6 +508,8 @@ public function getProcessedHelp()
512508
*
513509
* @return Command The current instance
514510
*
511+
* @throws \InvalidArgumentException When an alias is invalid
512+
*
515513
* @api
516514
*/
517515
public function setAliases($aliases)
@@ -606,9 +604,18 @@ public function asXml($asDom = false)
606604
return $output->fetch();
607605
}
608606

607+
/**
608+
* Validates a command name.
609+
*
610+
* It must be non-empty and parts can optionally be separated by ":".
611+
*
612+
* @param string $name
613+
*
614+
* @throws \InvalidArgumentException When the name is invalid
615+
*/
609616
private function validateName($name)
610617
{
611-
if (!preg_match('/^[^\:]+(\:[^\:]+)*$/', $name)) {
618+
if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) {
612619
throw new \InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
613620
}
614621
}

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ public function testAdd()
127127
}
128128

129129
/**
130-
* @expectedException LogicException
131-
* @expectedExceptionMessage You must call the parent constructor in "Foo5Command::__construct()"
130+
* @expectedException \LogicException
131+
* @expectedExceptionMessage Command class "Foo5Command" is not correctly initialized. You probably forgot to call the parent constructor.
132132
*/
133133
public function testAddCommandWithEmptyConstructor()
134134
{
135135
$application = new Application();
136-
$application->add($foo = new \Foo5Command());
136+
$application->add(new \Foo5Command());
137137
}
138138

139139
public function testHasGet()

0 commit comments

Comments
 (0)