Fixing Exception management

master
Julien Rosset 6 years ago
parent 25974f3c47
commit f950043b9c

@ -44,7 +44,6 @@ interface IArgument {
* @param $args array La liste des arguments encore à traiter (par ordre de réception)
*
* @return ParseResult|null Le résultat du parsage. Null si rien parsé
*
* @throws IncorrectParse Echec du parsage de l'argument
*/
public function parse($args);

@ -4,12 +4,11 @@
*/
namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Argument\ArgumentAbstract;
use CommandLine\Argument\ParseResult;
use InvalidArgumentException;
/**
* Option de type "flag" contenant True ou False.
* Option de type "flag" contenant True ou False
*
* Si fourni, retournera la valeur inverse de {@see getDefault() getDefault}
*
@ -19,21 +18,22 @@ use CommandLine\Argument\ParseResult;
*/
class Flag extends OptionAbstract {
/**
* Crée un nouvel argument de type option.
* Crée un nouvel argument de type option
*
* @param string $name Le nom.
* @param boolean $default La valeur par défaut.
* @param string|null $description La description.
* @param string|null $tagLong Le tag long.
* @param string|null $tagShort Le tag court.
*
* @throws InvalidArgument Si l'un des paramètres n'est pas correct
* @param string $name Le nom
* @param boolean $default La valeur par défaut
* @param string|null $description La description
* @param string|null $tagLong Le tag long
* @param string|null $tagShort Le tag court
*/
public function __construct ($name, $default, $description, $tagLong = null, $tagShort = null) {
parent::__construct($name, $description, $tagLong, $tagShort);
$this->setDefault($default);
}
/**
* @inheritDoc
*/
public function parse ($args) {
if($this->_parseTag($args[0]))
return new ParseResult(true, 1);
@ -47,12 +47,10 @@ class Flag extends OptionAbstract {
* @param boolean $default La valeur par défaut.
*
* @return $this
*
* @throws InvalidArgument Si la valeur n'est pas un booléen.
*/
public function setDefault ($default = null) {
if(is_null($default) || !is_bool($default))
throw new InvalidArgument($default, 'La valeur par défaut DOIT être un booléen');
throw new InvalidArgumentException('La valeur par défaut DOIT être un booléen');
parent::setDefault($default);
return $this;

@ -4,8 +4,6 @@
*/
namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument;
/**
* Interface à implémenter si l'argument "option" auto-déclare d'autres arguments
*
@ -16,8 +14,6 @@ interface IArgumentOptionSecondary {
* La liste des arguments auto-déclarés.
*
* @return IArgumentOption[] La liste des arguments
*
* @throws InvalidArgument
*/
public function getOthersOptions();
}

@ -4,19 +4,12 @@
*/
namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Argument\ArgumentAbstract;
use InvalidArgumentException;
/**
* Classe abstraite pour les arguments de type "option" : --xxx / -x
*
* NOTE : les arguments de type "option" ne peuvent autoriser plusieurs valeurs ({@see $default $default} = 1).
*
* @property string|null $tagShort {@see $_tagShort $_tagShort }
* @property string|null $tagLong {@see $_tagLong $_tagLong }
* @property boolean $multiple {@see $_multiple $_multiple }
* @property boolean $isStoppingParse {@see $_isStoppingParse $_isStoppingParse}
*
* @package CommandLine\Argument\Option
*/
abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOption {
@ -140,14 +133,11 @@ abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOptio
* @param boolean $multiple Argument autorisé plusieurs fois ?
*
* @return $this
*
* @throws InvalidArgument Si la valeur n'est pas un booléen
*
* @see $_optional
*/
public function setMultiple($multiple) {
if (!is_bool($multiple))
throw new InvalidArgument($multiple, 'La valeur n\'est pas un booléen');
throw new InvalidArgumentException('La multiplicité n\'est pas un booléen');
$this->_multiple = $multiple;
return $this;
@ -162,14 +152,11 @@ abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOptio
* @param boolean $stoppingParse Met fin au parsage ?
*
* @return $this
*
* @throws InvalidArgument Si la valeur n'est pas un booléen
*
* @see $_optional
*/
public function setStoppingParse($stoppingParse) {
if (!is_bool($stoppingParse))
throw new InvalidArgument($stoppingParse, 'La valeur n\'est pas un booléen');
throw new InvalidArgumentException('La stoppabilité n\'est pas un booléen');
$this->_isStoppingParse = $stoppingParse;
return $this;

@ -4,16 +4,14 @@
*/
namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Exception\IncorrectParse;
use CommandLine\Argument\IArgumentValueDescription;
use CommandLine\Argument\Parser\IValueParser;
use CommandLine\Argument\ParseResult;
use CommandLine\Exception\IncorrectParse;
use RuntimeException;
/**
* Option contenant une valeur.
*
* @property IValueParser $valueParser Le parseur de valeur.
* Option contenant une valeur
*
* @package CommandLine\Argument\Option
*/
@ -24,36 +22,40 @@ class Value extends OptionAbstract implements IArgumentValueDescription {
protected $_valueParser;
/**
* Crée un nouvel argument de type option.
*
* @param string $name Le nom.
* @param string|null $description La description.
* @param IValueParser $valueParser Le parseur de valeur.
* @param string|null $tagLong Le tag long.
* @param string|null $tagShort Le tag court.
* Crée un nouvel argument de type option
*
* @throws \Fidit\v3\Exception\InvalidArgument Si l'un des paramètres n'est pas correct
* @param string $name Le nom
* @param string|null $description La description
* @param IValueParser $valueParser Le parseur de valeur
* @param string|null $tagLong Le tag long
* @param string|null $tagShort Le tag court
*/
public function __construct ($name, $description, IValueParser $valueParser, $tagLong = null, $tagShort = null) {
parent::__construct($name, $description, $tagLong, $tagShort);
$this->setValueParser($valueParser);
}
/**
* @inheritDoc
*/
public function parse ($args) {
try {
if(!$this->_parseTag($args[0]))
return null;
if(count($args) < 2 || is_null($args[1]))
throw new IncorrectParse($this,null, 'Seconde valeur de l\'argument manquante');
throw new IncorrectParse('La seconde valeur de l\'argument manquante');
return new ParseResult($this->_valueParser->parseValue($args[1]), 2);
}
catch (InvalidArgument $e) {
throw IncorrectParse::createFromInvalidArgument($this, $e);
catch (RuntimeException $e) {
throw new IncorrectParse('Échec du parsage de la valeur "' . $args[0] . '"', 0, $e);
}
}
/**
* @inheritDoc
*/
public function getValueDescription () {
return $this->_valueParser->getValueDescription();
}

@ -4,7 +4,7 @@
*/
namespace CommandLine\Argument\Parser;
use http\Exception\InvalidArgumentException;
use RangeException;
/**
* Parseur vers un booléen
@ -24,7 +24,7 @@ class BooleanParser implements IValueParser {
'true' , '1', 'oui', 'vrai',
'false', '0', 'non', 'faux',
)))
throw new InvalidArgumentException('La valeur n\'est pas un booléen valide : '.$arg);
throw new RangeException('La valeur n\'est pas un booléen valide : '.$arg);
return in_array($arg, array('true' , '1', 'oui', 'vrai'));
}

@ -5,6 +5,7 @@
namespace CommandLine\Argument\Parser;
use InvalidArgumentException;
use RangeException;
/**
* Parseur vers un réel
@ -37,17 +38,17 @@ class DecimalParser implements IValueParser {
*/
public function parseValue ($arg) {
if (!$this->_isDecimal($arg))
throw new InvalidArgumentException('La valeur n\'est pas un réel valide : ' . $arg);
throw new RangeException('La valeur n\'est pas un réel valide : ' . $arg);
$int = (int)$arg;
if ($this->hasValueMin()) {
if ($int < $this->getValueMin())
throw new InvalidArgumentException('La valeur est inférieure au minimum (' . $this->getValueMin().') : ' . $arg);
throw new RangeException('La valeur est inférieure au minimum (' . $this->getValueMin().') : ' . $arg);
}
if ($this->hasValueMax()) {
if ($int > $this->getValueMax())
throw new InvalidArgumentException($int, 'La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $arg);
throw new RangeException($int, 'La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $arg);
}
return $int;

@ -5,6 +5,7 @@
namespace CommandLine\Argument\Parser;
use InvalidArgumentException;
use RangeException;
/**
* Parseur vers une liste de valeurs possible (chaine de caractère).
@ -31,7 +32,7 @@ class EnumParser implements IValueParser {
*/
public function parseValue ($arg) {
if(!in_array($arg, $this->_values))
throw new InvalidArgumentException('La valeur ne fait partie de liste des valeurs autorisées (' . implode(', ', $this->getValues()) . ') : ' . $arg);
throw new RangeException('La valeur ne fait partie de liste des valeurs autorisées (' . implode(', ', $this->getValues()) . ') : ' . $arg);
return $arg;
}

@ -5,6 +5,7 @@
namespace CommandLine\Argument\Parser;
use InvalidArgumentException;
use RangeException;
/**
* Parseur vers un entier
@ -37,17 +38,17 @@ class IntegerParser implements IValueParser {
*/
public function parseValue ($arg) {
if(!$this->_isInt($arg))
throw new InvalidArgumentException('La valeur n\'est pas un entier valide : ' . $arg);
throw new RangeException('La valeur n\'est pas un entier valide : ' . $arg);
$int = (int)$arg;
if($this->hasValueMin()) {
if($int < $this->getValueMin())
throw new InvalidArgumentException('La valeur est inférieure au minimum (' . $this->getValueMin() . ') :' . $int);
throw new RangeException('La valeur est inférieure au minimum (' . $this->getValueMin() . ') :' . $int);
}
if($this->hasValueMax()) {
if($int > $this->getValueMax())
throw new InvalidArgumentException('La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $int);
throw new RangeException('La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $int);
}
return $int;

@ -4,7 +4,7 @@
*/
namespace CommandLine\Argument\Parser;
use InvalidArgumentException;
use RangeException;
/**
* Parseur vers une chaine de caractère correspondant à un motif
@ -33,7 +33,7 @@ class RegexParser implements IValueParser {
*/
public function parseValue ($arg) {
if(!preg_match($this->_regex, $arg, $matches))
throw new InvalidArgumentException('La valeur ne correspond pas au motif attendu : ' . $arg);
throw new RangeException('La valeur ne correspond pas au motif attendu : ' . $arg);
return $matches;
}

@ -10,8 +10,6 @@ use CommandLine\Argument\ParseResult;
/**
* Argument devant correspondre une valeur fixe
*
* @property string $value {@see $_value $_value}.
*
* @package CommandLine\Argument
*/
class FixedValue extends ValueAbstract implements IArgumentValueDescription {
@ -23,13 +21,10 @@ class FixedValue extends ValueAbstract implements IArgumentValueDescription {
/**
* Crée un nouvel argument de type valeur
*
* @param int $position La position
* @param string $varName Le nom de la variable
* @param string $value La valeur
* @param string|null $description La description
* @param boolean $optional Valeur optionnelle ?
*
* @throws \Fidit\v3\Exception\InvalidArgument Si l'un des paramètres n'est pas correct
*/
public function __construct ($varName, $value, $description, $optional = false) {
parent::__construct($value, $description, $optional);
@ -37,6 +32,9 @@ class FixedValue extends ValueAbstract implements IArgumentValueDescription {
$this->setValue($value);
}
/**
* @inheritDoc
*/
public function parse ($args) {
if($args[0] == $this->getValue())
return new ParseResult($this->getValue(), 1);
@ -44,6 +42,9 @@ class FixedValue extends ValueAbstract implements IArgumentValueDescription {
return null;
}
/**
* @inheritDoc
*/
public function getValueDescription () {
return '"'.$this->getValue().'"';
}
@ -64,7 +65,6 @@ class FixedValue extends ValueAbstract implements IArgumentValueDescription {
* @param string $value La valeur
*
* @return $this
*
* @see $_value
*/
public function setValue($value) {

@ -4,17 +4,15 @@
*/
namespace CommandLine\Argument\Value;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Exception\IncorrectParse;
use CommandLine\Argument\IArgumentValueDescription;
use CommandLine\Argument\Parser\IValueParser;
use CommandLine\Argument\ParseResult;
use CommandLine\Exception\IncorrectParse;
use RangeException;
/**
* Argument de type "valeur"
*
* @property IValueParser $valueParser Le parseur de valeur.
*
* @package CommandLine\Argument\Value
*/
class Value extends ValueAbstract implements IArgumentValueDescription {
@ -37,15 +35,21 @@ class Value extends ValueAbstract implements IArgumentValueDescription {
$this->setValueParser($valueParser);
}
/**
* @inheritDoc
*/
public function parse ($args) {
try {
return new ParseResult($this->_valueParser->parseValue($args[0]), 1);
}
catch(InvalidArgument $e) {
throw IncorrectParse::createFromInvalidArgument($this, $e);
catch (RangeException $e) {
throw new IncorrectParse('Échec du parsage de la valeur "' . $args[0] . '"', 0, $e);
}
}
/**
* @inheritDoc
*/
public function getValueDescription () {
return $this->_valueParser->getValueDescription();
}

@ -3,18 +3,15 @@
/**
* Déclaration de la classe CommandLine\Argument\Value\ValueArgument.
*/
namespace CommandLine\Argument\Value;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Argument\ArgumentAbstract;
use CommandLine\Argument\Value\IArgumentValue;
use InvalidArgumentException;
/**
* Classe abstraite pour les paramètres de type "valeur"
*
* @property int $occurMin {@see $_occurMin $_occurMin}
* @property int|null $occurMax {@see $_occurMax $_occurMax}
*
* @package CommandLine\Argument\Value
*/
abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue {
@ -39,7 +36,21 @@ abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue
$this->setOptional($optional);
}
/**
* Définit si l'argument est facultatif ou non.
*
* @param bool $optional Argument facultatif ?
*
* @return $this
*/
public function setOptional ($optional = false) {
$this->setOccurMin($optional ? 0 : 1);
return $this;
}
/**
* @inheritDoc
*/
public function getOccurMin () {
return $this->_occurMin;
}
@ -49,41 +60,29 @@ abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue
* @param int $occurMin Le nombre minimum
*
* @return $this
*
* @throws InvalidArgument Si la valeur n'est pas un entier positif.
*
* @see $_occurMin
*/
public function setOccurMin ($occurMin) {
if (!is_numeric($occurMin))
throw new InvalidArgument($occurMin, 'La valeur n\'est pas un entier');
if (!is_numeric($occurMin)) {
throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier');
}
if (floor($occurMin) != $occurMin)
throw new InvalidArgument($occurMin, 'La valeur n\'est pas un entier');
if (floor($occurMin) != $occurMin) {
throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier');
}
$int = (int)$occurMin;
if($int < 0)
throw new InvalidArgument($occurMin, 'La valeur n\'est pas un entier positif');
if ($int < 0) {
throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier positif');
}
$this->_occurMin = $int;
return $this;
}
/**
* Définit si l'argument est facultatif ou non.
*
* @param bool $optional Argument facultatif ?
*
* @return $this
* @inheritDoc
*/
public function setOptional($optional = false) {
try {
$this->setOccurMin($optional ? 0 : 1);
}
catch(InvalidArgument $e) {}
return $this;
}
public function getOccurMax () {
return $this->_occurMax;
}
@ -93,22 +92,22 @@ abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue
* @param int|null $occurMax Le nombre maximum
*
* @return $this
*
* @throws InvalidArgument Si la valeur n'est pas Null ou un entier strictement positif.
*
* @see $_occurMax
*/
public function setOccurMax ($occurMax) {
if (!is_null($occurMax)) {
if (!is_numeric($occurMax))
throw new InvalidArgument($occurMax, 'La valeur n\'est pas un entier');
if (!is_numeric($occurMax)) {
throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier');
}
if (floor($occurMax) != $occurMax)
throw new InvalidArgument($occurMax, 'La valeur n\'est pas un entier');
if (floor($occurMax) != $occurMax) {
throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier');
}
$occurMax = (int)$occurMax;
if ($occurMax <= 0)
throw new InvalidArgument($occurMax, 'La valeur n\'est pas un entier strictement positif');
if ($occurMax <= 0) {
throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier strictement positif');
}
}
$this->_occurMax = $occurMax;

@ -5,7 +5,7 @@
namespace CommandLine\Exception;
use CommandLine\Argument\IArgument;
use LogicException;
use RuntimeException;
/**
* Exception levée quand le parsage d'un argument échoue
@ -18,4 +18,4 @@ use LogicException;
*
* @see IArgument::parse()
*/
class IncorrectParse extends LogicException implements IException {}
class IncorrectParse extends RuntimeException implements IException {}
Loading…
Cancel
Save