Skip to content

Commit

Permalink
Tidy up the exceptions to extend/follow core/spl exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan3dc committed Oct 6, 2018
1 parent 1c8eead commit b994a7f
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 68 deletions.
8 changes: 5 additions & 3 deletions src/Argument/Argument.php
Expand Up @@ -2,7 +2,7 @@

namespace League\CLImate\Argument;

use League\CLImate\Exceptions\InvalidArgumentCastTypeException;
use League\CLImate\Exceptions\UnexpectedValueException;
use function is_array;

class Argument
Expand Down Expand Up @@ -281,13 +281,15 @@ public function castTo()
*
* Valid data types are "string", "int", "float", and "bool".
*
* @throws \Exception if $castTo is not a valid data type.
* @param string $castTo
*
* @return void
* @throws UnexpectedValueException if $castTo is not a valid data type.
*/
protected function setCastTo($castTo)
{
if (!in_array($castTo, ['string', 'int', 'float', 'bool'])) {
throw new InvalidArgumentCastTypeException(
throw new UnexpectedValueException(
"An argument may only be cast to the data type "
. "'string', 'int', 'float', or 'bool'."
);
Expand Down
11 changes: 6 additions & 5 deletions src/Argument/Manager.php
Expand Up @@ -3,7 +3,7 @@
namespace League\CLImate\Argument;

use League\CLImate\CLImate;
use League\CLImate\Exceptions\InvalidTypeException;
use League\CLImate\Exceptions\InvalidArgumentException;

class Manager
{
Expand Down Expand Up @@ -52,9 +52,11 @@ public function __construct()
/**
* Add an argument.
*
* @throws \Exception if $argument isn't an array or Argument object.
* @param Argument|string|array $argument
* @param $options
*
* @return void
* @throws InvalidArgumentException if $argument isn't an array or Argument object.
*/
public function add($argument, array $options = [])
{
Expand All @@ -67,8 +69,8 @@ public function add($argument, array $options = [])
$argument = Argument::createFromArray($argument, $options);
}

if (!($argument instanceof Argument)) {
throw new InvalidTypeException('Please provide an argument name or object.');
if (!$argument instanceof Argument) {
throw new InvalidArgumentException('Please provide an argument name or object.');
}

$this->arguments[$argument->name()] = $argument;
Expand Down Expand Up @@ -228,7 +230,6 @@ public function usage(CLImate $climate, array $argv = null)
/**
* Parse command line arguments into CLImate arguments.
*
* @throws \Exception if required arguments aren't defined.
* @param array $argv
*/
public function parse(array $argv = null)
Expand Down
8 changes: 5 additions & 3 deletions src/Argument/Parser.php
Expand Up @@ -2,7 +2,7 @@

namespace League\CLImate\Argument;

use League\CLImate\Exceptions\MissingRequiredArgumentException;
use League\CLImate\Exceptions\InvalidArgumentException;

class Parser
{
Expand Down Expand Up @@ -44,8 +44,10 @@ public function setFilter($filter, $arguments)
/**
* Parse command line arguments into CLImate arguments.
*
* @throws \Exception if required arguments aren't defined.
* @param array $argv
*
* @return void
* @throws InvalidArgumentException if required arguments aren't defined.
*/
public function parse(array $argv = null)
{
Expand All @@ -64,7 +66,7 @@ public function parse(array $argv = null)
$missingArguments = $this->filter->missing();

if (count($missingArguments) > 0) {
throw new MissingRequiredArgumentException(
throw new InvalidArgumentException(
'The following arguments are required: '
. $this->summary->short($missingArguments) . '.'
);
Expand Down
7 changes: 0 additions & 7 deletions src/Exceptions/InvalidArgumentCastTypeException.php

This file was deleted.

7 changes: 7 additions & 0 deletions src/Exceptions/InvalidArgumentException.php
@@ -0,0 +1,7 @@
<?php

namespace League\CLImate\Exceptions;

class InvalidArgumentException extends \InvalidArgumentException implements Exception
{
}
7 changes: 0 additions & 7 deletions src/Exceptions/InvalidTypeException.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Exceptions/MissingClassException.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Exceptions/MissingRequiredArgumentException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Exceptions/RuntimeException.php
Expand Up @@ -2,6 +2,6 @@

namespace League\CLImate\Exceptions;

class RuntimeException extends \Exception implements Exception
class RuntimeException extends \RuntimeException implements Exception
{
}
7 changes: 0 additions & 7 deletions src/Exceptions/SPLUnexpectedValueException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Exceptions/UnexpectedValueException.php
Expand Up @@ -2,6 +2,6 @@

namespace League\CLImate\Exceptions;

class UnexpectedValueException extends \Exception implements Exception
class UnexpectedValueException extends \UnexpectedValueException implements Exception
{
}
4 changes: 3 additions & 1 deletion src/TerminalObject/Dynamic/Progress.php
Expand Up @@ -93,7 +93,9 @@ public function total($total)
*
* @param integer $current
* @param mixed $label
* @throws \Exception
*
* @return void
* @throws UnexpectedValueException
*/
public function current($current, $label = null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/TerminalObject/Dynamic/Spinner.php
Expand Up @@ -2,7 +2,7 @@

namespace League\CLImate\TerminalObject\Dynamic;

use League\CLImate\Exceptions\SPLUnexpectedValueException;
use League\CLImate\Exceptions\UnexpectedValueException;
use function array_merge;
use function count;
use function microtime;
Expand Down Expand Up @@ -94,7 +94,7 @@ public function timeLimit($timeLimit)
public function characters(...$characters)
{
if (count($characters) < 1) {
throw new SPLUnexpectedValueException("You must specify the characters to use");
throw new UnexpectedValueException("You must specify the characters to use");
}

$this->characters = $characters;
Expand Down
4 changes: 2 additions & 2 deletions src/TerminalObject/Helper/Art.php
Expand Up @@ -2,7 +2,7 @@

namespace League\CLImate\TerminalObject\Helper;

use League\CLImate\Exceptions\SPLUnexpectedValueException;
use League\CLImate\Exceptions\UnexpectedValueException;

trait Art
{
Expand Down Expand Up @@ -95,7 +95,7 @@ protected function artFile($art)
}

if (count($files) === 0) {
throw new SPLUnexpectedValueException("Unable to find an art file with the name '{$art}'");
throw new UnexpectedValueException("Unable to find an art file with the name '{$art}'");
}

return reset($files);
Expand Down
14 changes: 7 additions & 7 deletions src/TerminalObject/Router/ExtensionCollection.php
Expand Up @@ -2,8 +2,8 @@

namespace League\CLImate\TerminalObject\Router;

use League\CLImate\Exceptions\InvalidTypeException;
use League\CLImate\Exceptions\MissingClassException;
use League\CLImate\Exceptions\InvalidArgumentException;
use League\CLImate\Exceptions\UnexpectedValueException;
use League\CLImate\Util\Helper;

class ExtensionCollection
Expand Down Expand Up @@ -39,7 +39,7 @@ public function collection()
* @param string $original_key
* @param string|object|array $original_class
*
* @return type
* @return void
*/
protected function createCollection($original_key, $original_class)
{
Expand Down Expand Up @@ -82,19 +82,19 @@ protected function validateExtension($class)
/**
* @param string|object $class
*
* @throws \Exception if extension class does not exist
* @throws UnexpectedValueException if extension class does not exist
*/
protected function validateClassExists($class)
{
if (is_string($class) && !class_exists($class)) {
throw new MissingClassException('Class does not exist: ' . $class);
throw new UnexpectedValueException('Class does not exist: ' . $class);
}
}

/**
* @param string|object $class
*
* @throws \Exception if extension class does not implement either Dynamic or Basic interface
* @throws InvalidArgumentException if extension class does not implement either Dynamic or Basic interface
*/
protected function validateClassImplementation($class)
{
Expand All @@ -104,7 +104,7 @@ protected function validateClassImplementation($class)
|| is_a($class, $this->dynamic_interface, $str_class));

if (!$valid_implementation) {
throw new InvalidTypeException('Class must implement either '
throw new InvalidArgumentException('Class must implement either '
. $this->basic_interface . ' or ' . $this->dynamic_interface);
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/Util/Output.php
Expand Up @@ -2,7 +2,8 @@

namespace League\CLImate\Util;

use League\CLImate\Exceptions\InvalidTypeException;
use League\CLImate\Exceptions\InvalidArgumentException;
use League\CLImate\Exceptions\UnexpectedValueException;
use League\CLImate\Util\Writer\WriterInterface;

class Output
Expand Down Expand Up @@ -133,15 +134,15 @@ public function persist($persist = true)
/**
* Get a specific writer
*
* @throws \Exception if writer key doesn't exist
* @param string $writer
*
* @return WriterInterface|array
* @throws UnexpectedValueException if writer key doesn't exist
*/
public function get($writer)
{
if (!array_key_exists($writer, $this->writers)) {
throw new InvalidTypeException('Unknown writer [' . $writer . ']');
throw new UnexpectedValueException('Unknown writer [' . $writer . ']');
}

if (count($this->writers[$writer]) == 1) {
Expand Down Expand Up @@ -243,19 +244,19 @@ protected function resolveStringWriter($writer)

/**
* @param mixed $writer
* @throws \Exception For non-valid writer
* @throws InvalidArgumentException For non-valid writer
*/
protected function handleUnknownWriter($writer)
{
// If we've gotten this far and don't know what it is,
// let's at least try and give a helpful error message
if (is_object($writer)) {
throw new InvalidTypeException('Class [' . get_class($writer) . '] must implement '
throw new InvalidArgumentException('Class [' . get_class($writer) . '] must implement '
. 'League\CLImate\Util\Writer\WriterInterface.');
}

// No idea, just tell them we can't resolve it
throw new InvalidTypeException('Unable to resolve writer [' . $writer . ']');
throw new InvalidArgumentException('Unable to resolve writer [' . $writer . ']');
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Util/Reader/Stdin.php
Expand Up @@ -57,7 +57,7 @@ public function hidden()
* Lazily re-opens STDIN after hitting an EOF
*
* @return resource
* @throws \Exception
* @throws RuntimeException
*/
protected function getStdIn()
{
Expand All @@ -77,7 +77,8 @@ protected function getStdIn()
/**
* Attempt to set the stdin property
*
* @throws \Exception
* @return void
* @throws RuntimeException
*/
protected function setStdIn()
{
Expand Down

0 comments on commit b994a7f

Please sign in to comment.