From 272f18dcf5fe7112cdf11f5c0973eb00bc632944 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Sat, 11 May 2019 19:59:12 +0200 Subject: [PATCH] Apply "Symfony.Functions.ScopeOrder" Signed-off-by: Henrique Moody --- composer.json | 1 + library/Rules/AbstractAge.php | 14 ++--- library/Rules/AbstractFilterRule.php | 4 +- library/Rules/AbstractSearcher.php | 10 ++-- library/Rules/Domain.php | 34 ++++++------ library/Rules/In.php | 24 ++++----- library/Rules/KeyNested.php | 38 ++++++------- library/Rules/KeySet.php | 72 ++++++++++++------------- library/Rules/KeyValue.php | 80 ++++++++++++++-------------- library/Rules/Phone.php | 20 +++---- library/Rules/Size.php | 32 +++++------ library/Rules/Zend.php | 60 ++++++++++----------- phpcs.xml.dist | 1 + 13 files changed, 196 insertions(+), 194 deletions(-) diff --git a/composer.json b/composer.json index 97fefc89..644b005a 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", "egulias/email-validator": "^2.0", + "escapestudios/symfony2-coding-standard": "^3.8", "malukenho/docheader": "^0.1.4", "mikey179/vfsstream": "^1.6", "phpstan/phpstan": "^0.11", diff --git a/library/Rules/AbstractAge.php b/library/Rules/AbstractAge.php index 5735ab99..1a28a710 100644 --- a/library/Rules/AbstractAge.php +++ b/library/Rules/AbstractAge.php @@ -54,13 +54,6 @@ abstract class AbstractAge extends AbstractRule $this->baseDate = (int) date('Ymd') - $this->age * 10000; } - /** - * Should compare the current base date with the given one. - * - * The dates are represented as integers in the format "Ymd". - */ - abstract protected function compare(int $baseDate, int $givenDate): bool; - /** * {@inheritDoc} */ @@ -77,6 +70,13 @@ abstract class AbstractAge extends AbstractRule return $this->isValidWithFormat($this->format, (string) $input); } + /** + * Should compare the current base date with the given one. + * + * The dates are represented as integers in the format "Ymd". + */ + abstract protected function compare(int $baseDate, int $givenDate): bool; + private function isValidWithoutFormat(string $dateTime): bool { $timestamp = strtotime($dateTime); diff --git a/library/Rules/AbstractFilterRule.php b/library/Rules/AbstractFilterRule.php index eede0e0d..313b4d9f 100644 --- a/library/Rules/AbstractFilterRule.php +++ b/library/Rules/AbstractFilterRule.php @@ -37,8 +37,6 @@ abstract class AbstractFilterRule extends AbstractRule $this->additionalChars = implode($additionalChars); } - abstract protected function validateFilteredInput(string $input): bool; - /** * {@inheritDoc} */ @@ -58,6 +56,8 @@ abstract class AbstractFilterRule extends AbstractRule return $filteredInput === '' || $this->validateFilteredInput($filteredInput); } + abstract protected function validateFilteredInput(string $input): bool; + private function filter(string $input): string { return str_replace(str_split($this->additionalChars), '', $input); diff --git a/library/Rules/AbstractSearcher.php b/library/Rules/AbstractSearcher.php index 98cd6ad4..786788fc 100644 --- a/library/Rules/AbstractSearcher.php +++ b/library/Rules/AbstractSearcher.php @@ -25,11 +25,6 @@ abstract class AbstractSearcher extends AbstractRule { use CanValidateUndefined; - /** - * @return mixed[] - */ - abstract protected function getDataSource(): array; - /** * {@inheritDoc} */ @@ -42,4 +37,9 @@ abstract class AbstractSearcher extends AbstractRule return in_array($input, $dataSource, true); } + + /** + * @return mixed[] + */ + abstract protected function getDataSource(): array; } diff --git a/library/Rules/Domain.php b/library/Rules/Domain.php index 33312c59..a3fd7a3c 100644 --- a/library/Rules/Domain.php +++ b/library/Rules/Domain.php @@ -93,6 +93,23 @@ final class Domain extends AbstractRule return true; } + /** + * {@inheritDoc} + */ + public function check($input): void + { + try { + $this->assert($input); + } catch (NestedValidationException $exception) { + /** @var ValidationException $childException */ + foreach ($exception as $childException) { + throw $childException; + } + + throw $exception; + } + } + /** * @param ValidationException[] $exceptions * @param mixed $input @@ -111,23 +128,6 @@ final class Domain extends AbstractRule } } - /** - * {@inheritDoc} - */ - public function check($input): void - { - try { - $this->assert($input); - } catch (NestedValidationException $exception) { - /** @var ValidationException $childException */ - foreach ($exception as $childException) { - throw $childException; - } - - throw $exception; - } - } - private function createGenericRule(): Validatable { return new AllOf( diff --git a/library/Rules/In.php b/library/Rules/In.php index d58b5cf4..3747b46c 100644 --- a/library/Rules/In.php +++ b/library/Rules/In.php @@ -49,6 +49,18 @@ final class In extends AbstractRule $this->compareIdentical = $compareIdentical; } + /** + * {@inheritDoc} + */ + public function validate($input): bool + { + if ($this->compareIdentical) { + return $this->validateIdentical($input); + } + + return $this->validateEquals($input); + } + /** * @param mixed $input */ @@ -84,16 +96,4 @@ final class In extends AbstractRule return mb_strpos($this->haystack, $inputString, 0, mb_detect_encoding($inputString)) !== false; } - - /** - * {@inheritDoc} - */ - public function validate($input): bool - { - if ($this->compareIdentical) { - return $this->validateIdentical($input); - } - - return $this->validateEquals($input); - } } diff --git a/library/Rules/KeyNested.php b/library/Rules/KeyNested.php index cfa20316..95e49bcf 100644 --- a/library/Rules/KeyNested.php +++ b/library/Rules/KeyNested.php @@ -48,6 +48,25 @@ final class KeyNested extends AbstractRelated return true; } + /** + * {@inheritDoc} + */ + public function getReferenceValue($input) + { + if (is_scalar($input)) { + $message = sprintf('Cannot select the %s in the given data', $this->getReference()); + throw new ComponentException($message); + } + + $keys = $this->getReferencePieces(); + $value = $input; + while (!is_null($key = array_shift($keys))) { + $value = $this->getValue($value, $key); + } + + return $value; + } + /** * @return string[] */ @@ -127,23 +146,4 @@ final class KeyNested extends AbstractRelated $message = sprintf('Cannot select the property %s from the given data', $this->getReference()); throw new ComponentException($message); } - - /** - * {@inheritDoc} - */ - public function getReferenceValue($input) - { - if (is_scalar($input)) { - $message = sprintf('Cannot select the %s in the given data', $this->getReference()); - throw new ComponentException($message); - } - - $keys = $this->getReferencePieces(); - $value = $input; - while (!is_null($key = array_shift($keys))) { - $value = $this->getValue($value, $key); - } - - return $value; - } } diff --git a/library/Rules/KeySet.php b/library/Rules/KeySet.php index 3ec9d08b..43cd209a 100644 --- a/library/Rules/KeySet.php +++ b/library/Rules/KeySet.php @@ -52,6 +52,42 @@ final class KeySet extends AbstractWrapper parent::__construct(new AllOf(...$this->keyRules)); } + /** + * {@inheritDoc} + */ + public function assert($input): void + { + if (!$this->hasValidStructure($input)) { + throw $this->reportError($input); + } + + parent::assert($input); + } + + /** + * {@inheritDoc} + */ + public function check($input): void + { + if (!$this->hasValidStructure($input)) { + throw $this->reportError($input); + } + + parent::check($input); + } + + /** + * {@inheritDoc} + */ + public function validate($input): bool + { + if (!$this->hasValidStructure($input)) { + return false; + } + + return parent::validate($input); + } + /** * @throws ComponentException */ @@ -96,40 +132,4 @@ final class KeySet extends AbstractWrapper return count($input) == 0; } - - /** - * {@inheritDoc} - */ - public function assert($input): void - { - if (!$this->hasValidStructure($input)) { - throw $this->reportError($input); - } - - parent::assert($input); - } - - /** - * {@inheritDoc} - */ - public function check($input): void - { - if (!$this->hasValidStructure($input)) { - throw $this->reportError($input); - } - - parent::check($input); - } - - /** - * {@inheritDoc} - */ - public function validate($input): bool - { - if (!$this->hasValidStructure($input)) { - return false; - } - - return parent::validate($input); - } } diff --git a/library/Rules/KeyValue.php b/library/Rules/KeyValue.php index 27780261..3f6f01ac 100644 --- a/library/Rules/KeyValue.php +++ b/library/Rules/KeyValue.php @@ -51,46 +51,6 @@ final class KeyValue extends AbstractRule $this->baseKey = $baseKey; } - /** - * @param mixed $input - */ - private function getRule($input): Validatable - { - if (!isset($input[$this->comparedKey])) { - throw parent::reportError($this->comparedKey); - } - - if (!isset($input[$this->baseKey])) { - throw parent::reportError($this->baseKey); - } - - try { - $rule = Factory::getDefaultInstance()->rule($this->ruleName, [$input[$this->baseKey]]); - $rule->setName($this->comparedKey); - } catch (ComponentException $exception) { - throw parent::reportError($input, ['component' => true]); - } - - return $rule; - } - - private function overwriteExceptionParams(ValidationException $exception): ValidationException - { - $params = []; - foreach (array_keys($exception->getParams()) as $key) { - if (in_array($key, ['template', 'translator'])) { - continue; - } - - $params[$key] = $this->baseKey; - } - $params['name'] = $this->comparedKey; - - $exception->updateParams($params); - - return $exception; - } - /** * {@inheritDoc} */ @@ -144,4 +104,44 @@ final class KeyValue extends AbstractRule return $this->overwriteExceptionParams($exception); } } + + /** + * @param mixed $input + */ + private function getRule($input): Validatable + { + if (!isset($input[$this->comparedKey])) { + throw parent::reportError($this->comparedKey); + } + + if (!isset($input[$this->baseKey])) { + throw parent::reportError($this->baseKey); + } + + try { + $rule = Factory::getDefaultInstance()->rule($this->ruleName, [$input[$this->baseKey]]); + $rule->setName($this->comparedKey); + } catch (ComponentException $exception) { + throw parent::reportError($input, ['component' => true]); + } + + return $rule; + } + + private function overwriteExceptionParams(ValidationException $exception): ValidationException + { + $params = []; + foreach (array_keys($exception->getParams()) as $key) { + if (in_array($key, ['template', 'translator'])) { + continue; + } + + $params[$key] = $this->baseKey; + } + $params['name'] = $this->comparedKey; + + $exception->updateParams($params); + + return $exception; + } } diff --git a/library/Rules/Phone.php b/library/Rules/Phone.php index d8f351b3..cf04b9c2 100644 --- a/library/Rules/Phone.php +++ b/library/Rules/Phone.php @@ -30,16 +30,6 @@ use function sprintf; */ final class Phone extends AbstractRule { - private function getPregFormat(): string - { - return sprintf( - '/^\+?(%1$s)? ?(?(?=\()(\(%2$s\) ?%3$s)|([. -]?(%2$s[. -]*)?%3$s))$/', - '\d{0,3}', - '\d{1,3}', - '((\d{3,5})[. -]?(\d{4})|(\d{2}[. -]?){4})' - ); - } - /** * {@inheritDoc} */ @@ -51,4 +41,14 @@ final class Phone extends AbstractRule return preg_match($this->getPregFormat(), (string) $input) > 0; } + + private function getPregFormat(): string + { + return sprintf( + '/^\+?(%1$s)? ?(?(?=\()(\(%2$s\) ?%3$s)|([. -]?(%2$s[. -]*)?%3$s))$/', + '\d{0,3}', + '\d{1,3}', + '((\d{3,5})[. -]?(\d{4})|(\d{2}[. -]?){4})' + ); + } } diff --git a/library/Rules/Size.php b/library/Rules/Size.php index 9205a7a1..0298b9f0 100644 --- a/library/Rules/Size.php +++ b/library/Rules/Size.php @@ -61,6 +61,22 @@ final class Size extends AbstractRule $this->maxValue = $maxSize ? $this->toBytes($maxSize) : null; } + /** + * {@inheritDoc} + */ + public function validate($input): bool + { + if ($input instanceof SplFileInfo) { + return $this->isValidSize($input->getSize()); + } + + if (is_string($input)) { + return $this->isValidSize((int) filesize($input)); + } + + return false; + } + /** * @todo Move it to a trait * @@ -97,20 +113,4 @@ final class Size extends AbstractRule return $size <= $this->maxValue; } - - /** - * {@inheritDoc} - */ - public function validate($input): bool - { - if ($input instanceof SplFileInfo) { - return $this->isValidSize($input->getSize()); - } - - if (is_string($input)) { - return $this->isValidSize((int) filesize($input)); - } - - return false; - } } diff --git a/library/Rules/Zend.php b/library/Rules/Zend.php index f97b3e34..517cd4b1 100644 --- a/library/Rules/Zend.php +++ b/library/Rules/Zend.php @@ -53,36 +53,6 @@ final class Zend extends AbstractRule $this->zendValidator = $this->zendValidator($validator, $params); } - /** - * @param mixed $validator - * @param mixed[] $params - * - * @throws ComponentException - */ - private function zendValidator($validator, array $params = []): ValidatorInterface - { - if ($validator instanceof ValidatorInterface) { - return $validator; - } - - if (!is_string($validator)) { - throw new ComponentException('The given argument is not a valid Zend Validator'); - } - - $className = stripos($validator, 'Zend') === false ? 'Zend\\Validator\\'.$validator : '\\'.$validator; - - try { - $reflection = new ReflectionClass($className); - if (!$reflection->isInstantiable()) { - throw new ComponentException(sprintf('"%s" is not instantiable', $className)); - } - - return $this->zendValidator($reflection->newInstanceArgs($params)); - } catch (Throwable $exception) { - throw new ComponentException(sprintf('Could not create "%s"', $validator), 0, $exception); - } - } - /** * {@inheritDoc} */ @@ -134,4 +104,34 @@ final class Zend extends AbstractRule { return (clone $this->zendValidator)->isValid($input); } + + /** + * @param mixed $validator + * @param mixed[] $params + * + * @throws ComponentException + */ + private function zendValidator($validator, array $params = []): ValidatorInterface + { + if ($validator instanceof ValidatorInterface) { + return $validator; + } + + if (!is_string($validator)) { + throw new ComponentException('The given argument is not a valid Zend Validator'); + } + + $className = stripos($validator, 'Zend') === false ? 'Zend\\Validator\\'.$validator : '\\'.$validator; + + try { + $reflection = new ReflectionClass($className); + if (!$reflection->isInstantiable()) { + throw new ComponentException(sprintf('"%s" is not instantiable', $className)); + } + + return $this->zendValidator($reflection->newInstanceArgs($params)); + } catch (Throwable $exception) { + throw new ComponentException(sprintf('Could not create "%s"', $validator), 0, $exception); + } + } } diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 22dfa73a..80554ae8 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -160,4 +160,5 @@ 5 +