Skip to content

Commit 63f5af6

Browse files
committed
[Console] Use domain specific exceptions
Replace exception thrown by specific exception implementing the same interface.
1 parent 185e506 commit 63f5af6

38 files changed

+281
-145
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
4040
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
4141
use Symfony\Component\Console\Exception\CommandNotDefinedException;
42+
use Symfony\Component\Console\Exception\LogicException;
4243
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4344

4445
/**
@@ -393,7 +394,7 @@ public function add(Command $command)
393394
}
394395

395396
if (null === $command->getDefinition()) {
396-
throw new \LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
397+
throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
397398
}
398399

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

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Symfony\Component\Console\Output\OutputInterface;
2222
use Symfony\Component\Console\Application;
2323
use Symfony\Component\Console\Helper\HelperSet;
24+
use Symfony\Component\Console\Exception\InvalidArgumentException;
25+
use Symfony\Component\Console\Exception\LogicException;
2426

2527
/**
2628
* Base class for all commands.
@@ -51,7 +53,7 @@ class Command
5153
*
5254
* @param string|null $name The name of the command; passing null means it must be set in configure()
5355
*
54-
* @throws \LogicException When the command name is empty
56+
* @throws LogicException When the command name is empty
5557
*
5658
* @api
5759
*/
@@ -66,7 +68,7 @@ public function __construct($name = null)
6668
$this->configure();
6769

6870
if (!$this->name) {
69-
throw new \LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
71+
throw new LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
7072
}
7173
}
7274

@@ -162,13 +164,13 @@ protected function configure()
162164
*
163165
* @return null|int null or 0 if everything went fine, or an error code
164166
*
165-
* @throws \LogicException When this abstract method is not implemented
167+
* @throws LogicException When this abstract method is not implemented
166168
*
167169
* @see setCode()
168170
*/
169171
protected function execute(InputInterface $input, OutputInterface $output)
170172
{
171-
throw new \LogicException('You must override the execute() method in the concrete command class.');
173+
throw new LogicException('You must override the execute() method in the concrete command class.');
172174
}
173175

174176
/**
@@ -272,7 +274,7 @@ public function run(InputInterface $input, OutputInterface $output)
272274
*
273275
* @return Command The current instance
274276
*
275-
* @throws \InvalidArgumentException
277+
* @throws InvalidArgumentException
276278
*
277279
* @see execute()
278280
*
@@ -281,7 +283,7 @@ public function run(InputInterface $input, OutputInterface $output)
281283
public function setCode($code)
282284
{
283285
if (!is_callable($code)) {
284-
throw new \InvalidArgumentException('Invalid callable provided to Command::setCode.');
286+
throw new InvalidArgumentException('Invalid callable provided to Command::setCode.');
285287
}
286288

287289
if (PHP_VERSION_ID >= 50400 && $code instanceof \Closure) {
@@ -423,7 +425,7 @@ public function addOption($name, $shortcut = null, $mode = null, $description =
423425
*
424426
* @return Command The current instance
425427
*
426-
* @throws \InvalidArgumentException When the name is invalid
428+
* @throws InvalidArgumentException When the name is invalid
427429
*
428430
* @api
429431
*/
@@ -552,14 +554,14 @@ public function getProcessedHelp()
552554
*
553555
* @return Command The current instance
554556
*
555-
* @throws \InvalidArgumentException When an alias is invalid
557+
* @throws InvalidArgumentException When an alias is invalid
556558
*
557559
* @api
558560
*/
559561
public function setAliases($aliases)
560562
{
561563
if (!is_array($aliases) && !$aliases instanceof \Traversable) {
562-
throw new \InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
564+
throw new InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
563565
}
564566

565567
foreach ($aliases as $alias) {
@@ -634,7 +636,7 @@ public function getUsages()
634636
*
635637
* @return mixed The helper value
636638
*
637-
* @throws \InvalidArgumentException if the helper is not defined
639+
* @throws InvalidArgumentException if the helper is not defined
638640
*
639641
* @api
640642
*/
@@ -693,12 +695,12 @@ public function asXml($asDom = false)
693695
*
694696
* @param string $name
695697
*
696-
* @throws \InvalidArgumentException When the name is invalid
698+
* @throws InvalidArgumentException When the name is invalid
697699
*/
698700
private function validateName($name)
699701
{
700702
if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) {
701-
throw new \InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
703+
throw new InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
702704
}
703705
}
704706
}

src/Symfony/Component/Console/Descriptor/Descriptor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Input\InputDefinition;
1818
use Symfony\Component\Console\Input\InputOption;
1919
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Exception\InvalidArgumentException;
2021

2122
/**
2223
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
@@ -54,7 +55,7 @@ public function describe(OutputInterface $output, $object, array $options = arra
5455
$this->describeApplication($object, $options);
5556
break;
5657
default:
57-
throw new \InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
58+
throw new InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
5859
}
5960
}
6061

src/Symfony/Component/Console/Exception/CommandNotDefinedException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
/**
1515
* Represents an incorrect command name typed in the console.
16+
*
17+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
1618
*/
17-
class CommandNotDefinedException extends \InvalidArgumentException
19+
class CommandNotDefinedException extends \InvalidArgumentException implements ExceptionInterface
1820
{
1921
private $alternatives;
2022

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
/**
15+
* ExceptionInterface.
16+
*
17+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
18+
*
19+
* @api
20+
*/
21+
interface ExceptionInterface
22+
{
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
/**
15+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
16+
*/
17+
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
18+
{
19+
}

src/Symfony/Component/Console/Exception/InvalidOptionException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
/**
1515
* Represents an incorrect option name typed in the console.
16+
*
17+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
1618
*/
17-
class InvalidOptionException extends \InvalidArgumentException
19+
class InvalidOptionException extends \InvalidArgumentException implements ExceptionInterface
1820
{
1921
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
/**
15+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
16+
*/
17+
class LogicException extends \LogicException implements ExceptionInterface
18+
{
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
/**
15+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
16+
*/
17+
class RuntimeException extends \RuntimeException implements ExceptionInterface
18+
{
19+
}

src/Symfony/Component/Console/Formatter/OutputFormatter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Console\Formatter;
1313

14+
use Symfony\Component\Console\Exception\InvalidArgumentException;
15+
1416
/**
1517
* Formatter class for console output.
1618
*
@@ -118,14 +120,14 @@ public function hasStyle($name)
118120
*
119121
* @return OutputFormatterStyleInterface
120122
*
121-
* @throws \InvalidArgumentException When style isn't defined
123+
* @throws InvalidArgumentException When style isn't defined
122124
*
123125
* @api
124126
*/
125127
public function getStyle($name)
126128
{
127129
if (!$this->hasStyle($name)) {
128-
throw new \InvalidArgumentException(sprintf('Undefined style: %s', $name));
130+
throw new InvalidArgumentException(sprintf('Undefined style: %s', $name));
129131
}
130132

131133
return $this->styles[strtolower($name)];

0 commit comments

Comments
 (0)