From 33d04e872ca88dd208e36fe3bb2b561a28b16d05 Mon Sep 17 00:00:00 2001 From: darkelfe14728 Date: Thu, 12 Sep 2019 21:34:55 +0200 Subject: [PATCH] Fixing InvalidArgumentException in parsers --- Argument/ArgumentAbstract.class.php | 244 +++++++++++---------- Argument/ParseResult.class.php | 163 +++++++------- Argument/Parser/BooleanParser.class.php | 68 +++--- Argument/Parser/DecimalParser.class.php | 274 +++++++++++------------ Argument/Parser/EnumParser.class.php | 147 ++++++------- Argument/Parser/IValueParser.class.php | 58 +++-- Argument/Parser/IntegerParser.class.php | 280 ++++++++++++------------ Argument/Parser/RegexParser.class.php | 132 +++++------ Argument/Parser/StringParser.class.php | 44 ++-- 9 files changed, 710 insertions(+), 700 deletions(-) diff --git a/Argument/ArgumentAbstract.class.php b/Argument/ArgumentAbstract.class.php index 87e1fbd..ef9b6a7 100644 --- a/Argument/ArgumentAbstract.class.php +++ b/Argument/ArgumentAbstract.class.php @@ -1,121 +1,125 @@ -setName($name, true); - $this->setDescription($description); - } - - public function getVarName() { - return $this->_varName; - } - /** - * Définit le nom de la variable de retour de l'argument. - * - * @param string $varName Le nom - * - * @return $this - * - * @see $_name - */ - public function setVarName($varName) { - $this->_varName = $varName; - return $this; - } - - public function getName() { - return $this->_name; - } - /** - * Définit le nom de l'argument. - * - * @param string $name Le nom - * @param boolean $replaceVarName Remplacer également le nom de la variable de retour ? - * - * @return $this - * - * @see $_name - */ - public function setName($name, $replaceVarName = true) { - $this->_name = $name; - if($replaceVarName) - $this->setVarName($name); - return $this; - } - - public function getDescription() { - return $this->_description; - } - /** - * Définit la description de l'argument. - * - * @param string|null $description La description - * - * @return $this - * - * @see $_description - */ - public function setDescription($description) { - $this->_description = $description; - return $this; - } - - public function getDefault() { - return $this->_default; - } - /** - * Définit le valeur par défaut - * - * @param mixed|null $default La valeur par défaut. - * - * @return $this - * - * @see $_default - */ - public function setDefault($default = null) { - $this->_default = $default; - return $this; - } +setName($name, true); + $this->setDescription($description); + } + + /** + * @inheritDoc + */ + public function getVarName() { + return $this->_varName; + } + /** + * Définit le nom de la variable de retour de l'argument. + * + * @param string $varName Le nom + * + * @return $this + * + * @see $_name + */ + public function setVarName($varName) { + $this->_varName = $varName; + return $this; + } + + /** + * @inheritDoc + */ + public function getName() { + return $this->_name; + } + /** + * Définit le nom de l'argument. + * + * @param string $name Le nom + * @param boolean $replaceVarName Remplacer également le nom de la variable de retour ? + * + * @return $this + * + * @see $_name + */ + public function setName($name, $replaceVarName = true) { + $this->_name = $name; + if($replaceVarName) + $this->setVarName($name); + return $this; + } + + /** + * @inheritDoc + */ + public function getDescription() { + return $this->_description; + } + /** + * Définit la description de l'argument. + * + * @param string|null $description La description + * + * @return $this + * + * @see $_description + */ + public function setDescription($description) { + $this->_description = $description; + return $this; + } + + /** + * @inheritDoc + */ + public function getDefault() { + return $this->_default; + } + /** + * Définit le valeur par défaut + * + * @param mixed|null $default La valeur par défaut. + * + * @return $this + * + * @see $_default + */ + public function setDefault($default = null) { + $this->_default = $default; + return $this; + } } \ No newline at end of file diff --git a/Argument/ParseResult.class.php b/Argument/ParseResult.class.php index 1a30e13..456ea7d 100644 --- a/Argument/ParseResult.class.php +++ b/Argument/ParseResult.class.php @@ -1,85 +1,80 @@ -setValue($value); - $this->setConsume($consume); - } - - /** - * Le nombre d'argument consumé. - * - * @return int Le nombre d'argument consumé. - * - * @see $_consume - */ - public function getConsume() { - return $this->_consume; - } - /** - * Définit le nombre d'argument consumé. - * - * @param $consume int Le nombre d'argument consumé. - * - * @return $this - * - * @see $_consume - */ - public function setConsume($consume) { - $this->_consume = $consume; - return $this; - } - - /** - * La valeur. - * - * @return mixed La valeur. - * - * @see $_value - */ - public function getValue() { - return $this->_value; - } - /** - * Définit les valeurs définies. - * - * @param mixed $value La valeur. - * - * @return $this - * - * @see $_value - */ - public function setValue($value) { - $this->_value = $value; - return $this; - } +setValue($value); + $this->setConsume($consume); + } + + /** + * Le nombre d'argument consumé. + * + * @return int Le nombre d'argument consumé. + * + * @see $_consume + */ + public function getConsume() { + return $this->_consume; + } + /** + * Définit le nombre d'argument consumé. + * + * @param $consume int Le nombre d'argument consumé. + * + * @return $this + * + * @see $_consume + */ + public function setConsume($consume) { + $this->_consume = $consume; + return $this; + } + + /** + * La valeur. + * + * @return mixed La valeur. + * + * @see $_value + */ + public function getValue() { + return $this->_value; + } + /** + * Définit les valeurs définies. + * + * @param mixed $value La valeur. + * + * @return $this + * + * @see $_value + */ + public function setValue($value) { + $this->_value = $value; + return $this; + } } \ No newline at end of file diff --git a/Argument/Parser/BooleanParser.class.php b/Argument/Parser/BooleanParser.class.php index 5b7dac9..4b95bc4 100644 --- a/Argument/Parser/BooleanParser.class.php +++ b/Argument/Parser/BooleanParser.class.php @@ -1,32 +1,38 @@ -setValueMin($valueMin); - $this->setValueMax($valueMax); - } - - public function parseValue ($arg) { - if (!$this->_isDecimal($arg)) - throw new InvalidArgument($arg, 'La valeur n\'est pas un réel valide'); - - $int = (int)$arg; - - if ($this->hasValueMin()) { - if ($int < $this->getValueMin()) - throw new InvalidArgument($int, 'La valeur est inférieure au minimum : ' . $this->getValueMin()); - } - if ($this->hasValueMax()) { - if ($int > $this->getValueMax()) - throw new InvalidArgument($int, 'La valeur est supérieur au maximum : ' . $this->getValueMax()); - } - - return $int; - } - - protected function _isDecimal($val) { - if(!is_numeric($val)) - return false; - - return true; - } - - public function getValueDescription () { - return ($this->hasValueMin() ? $this->getValueMin().' <= ' : '').'decimal'.($this->hasValueMax() ? ' <= '.$this->getValueMax() : ''); - } - - /** - * La valeur minimumm autorisée - * - * @return double|null La valeur minimum. - * - * @see $_valueMin - */ - public function getValueMin() { - return $this->_valueMin; - } - /** - * Définit la valeur minimum autorisée. - * - * @param double|null $valueMin La valeur minimum. - * - * @return $this - * - * @throws InvalidArgument Si la valeur minimum n'est ni null, ni un entier - */ - public function setValueMin($valueMin = null) { - if(!is_null($valueMin) && !$this->_isDecimal($valueMin)) - throw new InvalidArgument($valueMin, 'La valeur n\'est pas un entier ou null'); - - $this->_valueMin = (int)$valueMin; - return $this; - } - /** - * Est-ce qu'il existe une limite minimum ? - * - * @return boolean True s'il existe une limite, sinon False - */ - public function hasValueMin() { - return !is_null($this->getValueMin()); - } - - /** - * La valeur maximum autorisée - * - * @return double|null La valeur maximum. - * - * @see $_valueMax - */ - public function getValueMax() { - return $this->_valueMax; - } - /** - * Définit la valeur maximum autorisée. - * - * @param double|null $valueMax La valeur maximum. - * - * @return $this - * - * @throws InvalidArgument Si la valeur maximum n'est ni null, ni un entier - */ - public function setValueMax($valueMax = null) { - if(!is_null($valueMax) && !$this->_isDecimal($valueMax)) - throw new InvalidArgument($valueMax, 'La valeur n\'est pas un entier ou null'); - - $this->_valueMax = (int)$valueMax; - return $this; - } - /** - * Est-ce qu'il existe une limite maximum ? - * - * @return boolean True s'il existe une limite, sinon False - */ - public function hasValueMax() { - return !is_null($this->getValueMax()); - } +setValueMin($valueMin); + $this->setValueMax($valueMax); + } + + /** + * @inheritDoc + */ + public function parseValue ($arg) { + if (!$this->_isDecimal($arg)) + throw new InvalidArgumentException('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); + } + if ($this->hasValueMax()) { + if ($int > $this->getValueMax()) + throw new InvalidArgumentException($int, 'La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $arg); + } + + return $int; + } + + /** + * @inheritDoc + */ + protected function _isDecimal($val) { + if(!is_numeric($val)) + return false; + + return true; + } + + /** + * @inheritDoc + */ + public function getValueDescription () { + return ($this->hasValueMin() ? $this->getValueMin().' <= ' : '').'decimal'.($this->hasValueMax() ? ' <= '.$this->getValueMax() : ''); + } + + /** + * La valeur minimumm autorisée + * + * @return double|null La valeur minimum. + * + * @see $_valueMin + */ + public function getValueMin() { + return $this->_valueMin; + } + /** + * Définit la valeur minimum autorisée. + * + * @param double|null $valueMin La valeur minimum. + * + * @return $this + */ + public function setValueMin($valueMin = null) { + if(!is_null($valueMin) && !$this->_isDecimal($valueMin)) + throw new InvalidArgumentException('La valeur n\'est pas un entier ou null' . $valueMin); + + $this->_valueMin = (int)$valueMin; + return $this; + } + /** + * Est-ce qu'il existe une limite minimum ? + * + * @return boolean True s'il existe une limite, sinon False + */ + public function hasValueMin() { + return !is_null($this->getValueMin()); + } + + /** + * La valeur maximum autorisée + * + * @return double|null La valeur maximum. + * + * @see $_valueMax + */ + public function getValueMax() { + return $this->_valueMax; + } + /** + * Définit la valeur maximum autorisée. + * + * @param double|null $valueMax La valeur maximum. + * + * @return $this + */ + public function setValueMax($valueMax = null) { + if(!is_null($valueMax) && !$this->_isDecimal($valueMax)) + throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : ' . $valueMax); + + $this->_valueMax = (int)$valueMax; + return $this; + } + /** + * Est-ce qu'il existe une limite maximum ? + * + * @return boolean True s'il existe une limite, sinon False + */ + public function hasValueMax() { + return !is_null($this->getValueMax()); + } } \ No newline at end of file diff --git a/Argument/Parser/EnumParser.class.php b/Argument/Parser/EnumParser.class.php index fc7db03..f4d402e 100644 --- a/Argument/Parser/EnumParser.class.php +++ b/Argument/Parser/EnumParser.class.php @@ -1,75 +1,74 @@ -setValues($values); - } - - public function parseValue ($arg) { - if(!in_array($arg, $this->_values)) - throw new InvalidArgument($arg, 'La valeur ne fait partie de liste des valeurs autorisées : '.implode(', ', $this->getValues())); - - return $arg; - } - - public function getValueDescription () { - return 'enum('.implode(',', $this->getValues()).')'; - } - - /** - * La liste des valeurs autoriséees. - * - * @return string[] La liste des valeurs - * - * @see $_values - */ - public function getValues() { - return $this->_values; - } - /** - * Définit la liste des valeurs autorisées. - * - * @param string[] $values La liste des valeurs - * - * @return $this - * - * @throws InvalidArgument Si la liste n'est pas un tableau ou si celui-ci est vide - * - * @see $_values - */ - public function setValues($values) { - if(!is_array($values)) - throw new InvalidArgument($values, 'La liste de valeurs n\'est pas un tableau'); - - if(count($values) == 0) - throw new InvalidArgument($values, 'La liste de valeurs doit avoir au moins un élément'); - - $this->_values = $values; - return $this; - } +setValues($values); + } + + /** + * @inheritDoc + */ + 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); + + return $arg; + } + + /** + * @inheritDoc + */ + public function getValueDescription () { + return 'enum('.implode(',', $this->getValues()).')'; + } + + /** + * La liste des valeurs autoriséees. + * + * @return string[] La liste des valeurs + * + * @see $_values + */ + public function getValues() { + return $this->_values; + } + /** + * Définit la liste des valeurs autorisées. + * + * @param string[] $values La liste des valeurs + * + * @return $this + * @see $_values + */ + public function setValues($values) { + if(!is_array($values)) + throw new InvalidArgumentException('La liste de valeurs n\'est pas un tableau'); + + if(count($values) == 0) + throw new InvalidArgumentException('La liste de valeurs doit avoir au moins un élément'); + + $this->_values = $values; + return $this; + } } \ No newline at end of file diff --git a/Argument/Parser/IValueParser.class.php b/Argument/Parser/IValueParser.class.php index 49427ac..e1f8834 100644 --- a/Argument/Parser/IValueParser.class.php +++ b/Argument/Parser/IValueParser.class.php @@ -1,32 +1,28 @@ -setValueMin($valueMin); - $this->setValueMax($valueMax); - } - - public function parseValue ($arg) { - if(!$this->_isInt($arg)) - throw new InvalidArgument($arg, 'La valeur n\'est pas un entier valide'); - - $int = (int)$arg; - - if($this->hasValueMin()) { - if($int < $this->getValueMin()) - throw new InvalidArgument($int, 'La valeur est inférieure au minimum : '.$this->getValueMin()); - } - if($this->hasValueMax()) { - if($int > $this->getValueMax()) - throw new InvalidArgument($int, 'La valeur est supérieur au maximum : '.$this->getValueMax()); - } - - return $int; - } - - protected function _isInt($val) { - if(!is_numeric($val)) - return false; - - if(floor($val) != $val) - return false; - - return true; - } - - public function getValueDescription () { - return ($this->hasValueMin() ? $this->getValueMin().' <= ' : '').'integer'.($this->hasValueMax() ? ' <= '.$this->getValueMax() : ''); - } - - /** - * La valeur minimumm autorisée - * - * @return int|null La valeur minimum. - * - * @see $_valueMin - */ - public function getValueMin() { - return $this->_valueMin; - } - /** - * Définit la valeur minimum autorisée. - * - * @param int|null $valueMin La valeur minimum. - * - * @return $this - * - * @throws InvalidArgument Si la valeur minimum n'est ni null, ni un entier - */ - public function setValueMin($valueMin = null) { - if(!is_null($valueMin) && !$this->_isInt($valueMin)) - throw new InvalidArgument($valueMin, 'La valeur n\'est pas un entier ou null'); - - $this->_valueMin = (int)$valueMin; - return $this; - } - /** - * Est-ce qu'il existe une limite minimum ? - * - * @return boolean True s'il existe une limite, sinon False - */ - public function hasValueMin() { - return !is_null($this->getValueMin()); - } - - /** - * La valeur maximum autorisée - * - * @return int|null La valeur maximum. - * - * @see $_valueMax - */ - public function getValueMax() { - return $this->_valueMax; - } - /** - * Définit la valeur maximum autorisée. - * - * @param int|null $valueMax La valeur maximum. - * - * @return $this - * - * @throws InvalidArgument Si la valeur maximum n'est ni null, ni un entier - */ - public function setValueMax($valueMax = null) { - if(!is_null($valueMax) && !$this->_isInt($valueMax)) - throw new InvalidArgument($valueMax, 'La valeur n\'est pas un entier ou null'); - - $this->_valueMax = (int)$valueMax; - return $this; - } - /** - * Est-ce qu'il existe une limite maximum ? - * - * @return boolean True s'il existe une limite, sinon False - */ - public function hasValueMax() { - return !is_null($this->getValueMax()); - } +setValueMin($valueMin); + $this->setValueMax($valueMax); + } + + /** + * @inheritDoc + */ + public function parseValue ($arg) { + if(!$this->_isInt($arg)) + throw new InvalidArgumentException('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); + } + if($this->hasValueMax()) { + if($int > $this->getValueMax()) + throw new InvalidArgumentException('La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $int); + } + + return $int; + } + + /** + * @inheritDoc + */ + protected function _isInt($val) { + if(!is_numeric($val)) + return false; + + if(floor($val) != $val) + return false; + + return true; + } + + /** + * @inheritDoc + */ + public function getValueDescription () { + return ($this->hasValueMin() ? $this->getValueMin().' <= ' : '').'integer'.($this->hasValueMax() ? ' <= '.$this->getValueMax() : ''); + } + + /** + * La valeur minimumm autorisée + * + * @return int|null La valeur minimum. + * + * @see $_valueMin + */ + public function getValueMin() { + return $this->_valueMin; + } + /** + * Définit la valeur minimum autorisée. + * + * @param int|null $valueMin La valeur minimum. + * + * @return $this + */ + public function setValueMin($valueMin = null) { + if(!is_null($valueMin) && !$this->_isInt($valueMin)) + throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : '.$valueMin); + + $this->_valueMin = (int)$valueMin; + return $this; + } + /** + * Est-ce qu'il existe une limite minimum ? + * + * @return boolean True s'il existe une limite, sinon False + */ + public function hasValueMin() { + return !is_null($this->getValueMin()); + } + + /** + * La valeur maximum autorisée + * + * @return int|null La valeur maximum. + * + * @see $_valueMax + */ + public function getValueMax() { + return $this->_valueMax; + } + /** + * Définit la valeur maximum autorisée. + * + * @param int|null $valueMax La valeur maximum. + * + * @return $this + */ + public function setValueMax($valueMax = null) { + if(!is_null($valueMax) && !$this->_isInt($valueMax)) + throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : '.$valueMax); + + $this->_valueMax = (int)$valueMax; + return $this; + } + /** + * Est-ce qu'il existe une limite maximum ? + * + * @return boolean True s'il existe une limite, sinon False + */ + public function hasValueMax() { + return !is_null($this->getValueMax()); + } } \ No newline at end of file diff --git a/Argument/Parser/RegexParser.class.php b/Argument/Parser/RegexParser.class.php index 3007a5b..3cd3a7e 100644 --- a/Argument/Parser/RegexParser.class.php +++ b/Argument/Parser/RegexParser.class.php @@ -1,65 +1,69 @@ -setRegex($regex); - } - - public function parseValue ($arg) { - if(!preg_match($this->regex, $arg, $matches)) - throw new InvalidArgument($arg, 'La valeur ne correspond pas au motif attendu'); - - return $matches; - } - - public function getValueDescription () { - return $this->getRegex(); - } - - /** - * Le motif à respecter - * - * @return string Le motif - * - * @see $_regex - */ - public function getRegex() { - return $this->_regex; - } - /** - * Modifie le motif à respecter - * - * @param string $regex Le nouveau motif - * - * @return $this - */ - public function setRegex($regex = null) { - $this->_regex = $regex; - return $this; - } +setRegex($regex); + } + + /** + * @inheritDoc + */ + public function parseValue ($arg) { + if(!preg_match($this->_regex, $arg, $matches)) + throw new InvalidArgumentException('La valeur ne correspond pas au motif attendu : ' . $arg); + + return $matches; + } + + /** + * @inheritDoc + */ + public function getValueDescription () { + return $this->getRegex(); + } + + /** + * Le motif à respecter + * + * @return string Le motif + * + * @see $_regex + */ + public function getRegex() { + return $this->_regex; + } + /** + * Modifie le motif à respecter + * + * @param string $regex Le nouveau motif + * + * @return $this + */ + public function setRegex($regex = null) { + $this->_regex = $regex; + return $this; + } } \ No newline at end of file diff --git a/Argument/Parser/StringParser.class.php b/Argument/Parser/StringParser.class.php index 0402761..62c16a7 100644 --- a/Argument/Parser/StringParser.class.php +++ b/Argument/Parser/StringParser.class.php @@ -1,20 +1,26 @@ -