From 72dd88144ef0c4aa6fc044b5bc49402a82a5cf2b Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Thu, 27 Aug 2020 15:29:46 +0200 Subject: [PATCH] Update version of "respect/coding-standard" Signed-off-by: Henrique Moody --- .php_cs | 42 ----------------- composer.json | 2 +- library/Exceptions/ValidationException.php | 10 ++-- library/Factory.php | 54 +++++++++++----------- library/Rules/AbstractAge.php | 14 +++--- library/Rules/AbstractComparison.php | 16 +++---- library/Rules/AbstractFilterRule.php | 4 +- library/Rules/AbstractRule.php | 16 +++---- library/Rules/AbstractSearcher.php | 10 ++-- library/Rules/KeyNested.php | 3 +- library/Rules/Luhn.php | 4 +- library/Rules/NfeAccessKey.php | 4 +- library/Rules/Nif.php | 6 +-- library/Rules/PrimeNumber.php | 2 +- library/Rules/VideoUrl.php | 16 +++---- library/Validator.php | 16 +++---- tests/integration/rules/uploaded.phpt | 15 +++--- tests/unit/Rules/CallTest.php | 40 ++++++++-------- tests/unit/Rules/NoTest.php | 32 ++++++------- tests/unit/Rules/UploadedTest.php | 36 +++++++-------- tests/unit/Rules/YesTest.php | 32 ++++++------- 21 files changed, 165 insertions(+), 209 deletions(-) delete mode 100644 .php_cs diff --git a/.php_cs b/.php_cs deleted file mode 100644 index 1985665b..00000000 --- a/.php_cs +++ /dev/null @@ -1,42 +0,0 @@ -setRiskyAllowed(true) - ->setRules([ - '@Symfony' => true, - '@PHP71Migration:risky' => true, - 'phpdoc_align' => false, - 'phpdoc_summary' => false, - 'mb_str_functions' => true, - 'no_multiline_whitespace_before_semicolons' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'ordered_imports' => true, - 'phpdoc_order' => true, - 'array_syntax' => ['syntax' => 'short'], - 'no_short_echo_tag' => true, - 'php_unit_construct' => true, - 'php_unit_dedicate_assert' => true, - 'php_unit_expectation' => true, - 'php_unit_mock' => true, - 'php_unit_namespaced' => true, - 'php_unit_ordered_covers' => true, - 'php_unit_set_up_tear_down_visibility' => true, - 'php_unit_test_annotation' => [ - 'style' => 'annotation', - ], - 'php_unit_test_case_static_method_calls' => ['call_type' => 'self'], - 'php_unit_test_class_requires_covers' => true, - ]) - ->setCacheFile( - sprintf( - '%s/.php_cs.cache', - getenv('TRAVIS') ? getenv('HOME').'/.php-cs-fixer' : __DIR__ - ) - ) - ->setFinder( - PhpCsFixer\Finder::create() - ->in(['library', 'tests']) - ->name('*.php') - ->name('*.phpt') - ); diff --git a/composer.json b/composer.json index f30ef530..8656bcde 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "phpstan/phpstan-deprecation-rules": "^0.12", "phpstan/phpstan-phpunit": "^0.12", "phpunit/phpunit": "^9.3", - "respect/coding-standard": "^2.1", + "respect/coding-standard": "^3.0", "squizlabs/php_codesniffer": "^3.5", "symfony/validator": "^3.0||^4.0", "zendframework/zend-validator": "^2.1" diff --git a/library/Exceptions/ValidationException.php b/library/Exceptions/ValidationException.php index 31a6a959..6eab7dd6 100644 --- a/library/Exceptions/ValidationException.php +++ b/library/Exceptions/ValidationException.php @@ -136,11 +136,6 @@ class ValidationException extends InvalidArgumentException implements Exception return isset($this->defaultTemplates[$this->mode][$this->template]) === false; } - public function __toString(): string - { - return $this->getMessage(); - } - protected function chooseTemplate(): string { return (string) key($this->defaultTemplates[$this->mode]); @@ -154,4 +149,9 @@ class ValidationException extends InvalidArgumentException implements Exception $this->params ); } + + public function __toString(): string + { + return $this->getMessage(); + } } diff --git a/library/Factory.php b/library/Factory.php index b2e1742f..fdd17f1e 100644 --- a/library/Factory.php +++ b/library/Factory.php @@ -36,13 +36,6 @@ use function ucfirst; */ final class Factory { - /** - * Default instance of the Factory. - * - * @var Factory - */ - private static $defaultInstance; - /** * @var string[] */ @@ -63,11 +56,30 @@ final class Factory */ private $parameterStringifier; + /** + * Default instance of the Factory. + * + * @var Factory + */ + private static $defaultInstance; + public function __construct() { $this->parameterStringifier = new KeepOriginalStringName(); } + /** + * Returns the default instance of the Factory. + */ + public static function getDefaultInstance(): self + { + if (self::$defaultInstance === null) { + self::$defaultInstance = new self(); + } + + return self::$defaultInstance; + } + public function withRuleNamespace(string $rulesNamespace): self { $clone = clone $this; @@ -100,26 +112,6 @@ final class Factory return $clone; } - /** - * Define the default instance of the Factory. - */ - public static function setDefaultInstance(self $defaultInstance): void - { - self::$defaultInstance = $defaultInstance; - } - - /** - * Returns the default instance of the Factory. - */ - public static function getDefaultInstance(): self - { - if (self::$defaultInstance === null) { - self::$defaultInstance = new self(); - } - - return self::$defaultInstance; - } - /** * Creates a rule. * @@ -185,6 +177,14 @@ final class Factory return new ValidationException($input, $id, $params, $formatter); } + /** + * Define the default instance of the Factory. + */ + public static function setDefaultInstance(self $defaultInstance): void + { + self::$defaultInstance = $defaultInstance; + } + /** * Creates a reflection based on class name. * diff --git a/library/Rules/AbstractAge.php b/library/Rules/AbstractAge.php index a53d7f24..3360287c 100644 --- a/library/Rules/AbstractAge.php +++ b/library/Rules/AbstractAge.php @@ -45,6 +45,13 @@ abstract class AbstractAge extends AbstractRule */ private $baseDate; + /** + * 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; + /** * Initializes the rule. */ @@ -71,13 +78,6 @@ 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/AbstractComparison.php b/library/Rules/AbstractComparison.php index eb6f8ce8..6498f9fd 100644 --- a/library/Rules/AbstractComparison.php +++ b/library/Rules/AbstractComparison.php @@ -29,6 +29,14 @@ abstract class AbstractComparison extends AbstractRule */ private $compareTo; + /** + * Compare both values and return whether the comparison is valid or not. + * + * @param mixed $left + * @param mixed $right + */ + abstract protected function compare($left, $right): bool; + /** * Initializes the rule by setting the value to be compared to the input. * @@ -53,12 +61,4 @@ abstract class AbstractComparison extends AbstractRule return $this->compare($left, $right); } - - /** - * Compare both values and return whether the comparison is valid or not. - * - * @param mixed $left - * @param mixed $right - */ - abstract protected function compare($left, $right): bool; } diff --git a/library/Rules/AbstractFilterRule.php b/library/Rules/AbstractFilterRule.php index 6d2452b9..40374bea 100644 --- a/library/Rules/AbstractFilterRule.php +++ b/library/Rules/AbstractFilterRule.php @@ -29,6 +29,8 @@ abstract class AbstractFilterRule extends AbstractRule */ private $additionalChars; + abstract protected function validateFilteredInput(string $input): bool; + /** * Initializes the rule with a list of characters to be ignored by the validation. */ @@ -56,8 +58,6 @@ 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/AbstractRule.php b/library/Rules/AbstractRule.php index e88d371e..4d3bc825 100644 --- a/library/Rules/AbstractRule.php +++ b/library/Rules/AbstractRule.php @@ -35,14 +35,6 @@ abstract class AbstractRule implements Validatable */ protected $template; - /** - * @param mixed$input - */ - public function __invoke($input): bool - { - return $this->validate($input); - } - /** * {@inheritDoc} */ @@ -98,4 +90,12 @@ abstract class AbstractRule implements Validatable return $this; } + + /** + * @param mixed$input + */ + public function __invoke($input): bool + { + return $this->validate($input); + } } diff --git a/library/Rules/AbstractSearcher.php b/library/Rules/AbstractSearcher.php index e97902c6..bc456a08 100644 --- a/library/Rules/AbstractSearcher.php +++ b/library/Rules/AbstractSearcher.php @@ -26,6 +26,11 @@ abstract class AbstractSearcher extends AbstractRule { use CanValidateUndefined; + /** + * @return mixed[] + */ + abstract protected function getDataSource(): array; + /** * {@inheritDoc} */ @@ -38,9 +43,4 @@ abstract class AbstractSearcher extends AbstractRule return in_array($input, $dataSource, true); } - - /** - * @return mixed[] - */ - abstract protected function getDataSource(): array; } diff --git a/library/Rules/KeyNested.php b/library/Rules/KeyNested.php index c670b0d4..068af3d1 100644 --- a/library/Rules/KeyNested.php +++ b/library/Rules/KeyNested.php @@ -110,11 +110,10 @@ final class KeyNested extends AbstractRelated /** * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint * - * @param object $object * * @return mixed */ - private function getValueFromObject($object, string $property) + private function getValueFromObject(object $object, string $property) { if (empty($property) || !property_exists($object, $property)) { $message = sprintf('Cannot select the property %s from the given object', $this->getReference()); diff --git a/library/Rules/Luhn.php b/library/Rules/Luhn.php index 017570a5..73957492 100644 --- a/library/Rules/Luhn.php +++ b/library/Rules/Luhn.php @@ -48,7 +48,7 @@ final class Luhn extends AbstractRule $parity = $numDigits % 2; for ($i = 0; $i < $numDigits; ++$i) { $digit = $digits[$i]; - if ($parity == ($i % 2)) { + if ($parity == $i % 2) { $digit <<= 1; if (9 < $digit) { $digit = $digit - 9; @@ -57,6 +57,6 @@ final class Luhn extends AbstractRule $sum += $digit; } - return ($sum % 10) == 0; + return $sum % 10 == 0; } } diff --git a/library/Rules/NfeAccessKey.php b/library/Rules/NfeAccessKey.php index 91806a6e..40dcb5a2 100644 --- a/library/Rules/NfeAccessKey.php +++ b/library/Rules/NfeAccessKey.php @@ -44,7 +44,7 @@ final class NfeAccessKey extends AbstractRule $digits = array_map('intval', str_split($input)); $w = []; for ($i = 0, $z = 5, $m = 43; $i <= $m; ++$i) { - $z = $i < $m ? ($z - 1) == 1 ? 9 : ($z - 1) : 0; + $z = $i < $m ? $z - 1 == 1 ? 9 : $z - 1 : 0; $w[] = $z; } @@ -53,7 +53,7 @@ final class NfeAccessKey extends AbstractRule } $s -= 11 * floor($s / 11); - $v = $s == 0 || $s == 1 ? 0 : (11 - $s); + $v = $s == 0 || $s == 1 ? 0 : 11 - $s; return $v == $digits[43]; } diff --git a/library/Rules/Nif.php b/library/Rules/Nif.php index 62b1f683..5edcd86c 100644 --- a/library/Rules/Nif.php +++ b/library/Rules/Nif.php @@ -59,7 +59,7 @@ final class Nif extends AbstractRule private function validateDni(int $number, string $control): bool { - return mb_substr('TRWAGMYFPDXBNJZSQVHLCKE', ($number % 23), 1) === $control; + return mb_substr('TRWAGMYFPDXBNJZSQVHLCKE', $number % 23, 1) === $control; } private function validateNie(string $prefix, string $number, string $control): bool @@ -92,12 +92,12 @@ final class Nif extends AbstractRule $digits = str_split((string) $code); $lastDigit = (int) array_pop($digits); - $key = $lastDigit === 0 ? 0 : (10 - $lastDigit); + $key = $lastDigit === 0 ? 0 : 10 - $lastDigit; if (is_numeric($control)) { return (int) $key === (int) $control; } - return mb_substr('JABCDEFGHI', ($key % 10), 1) === $control; + return mb_substr('JABCDEFGHI', $key % 10, 1) === $control; } } diff --git a/library/Rules/PrimeNumber.php b/library/Rules/PrimeNumber.php index 6c5e8bb9..cac3e46a 100644 --- a/library/Rules/PrimeNumber.php +++ b/library/Rules/PrimeNumber.php @@ -42,7 +42,7 @@ final class PrimeNumber extends AbstractRule } for ($i = 3; $i <= ceil(sqrt((float) $input)); $i += 2) { - if (($input % $i) == 0) { + if ($input % $i == 0) { return false; } } diff --git a/library/Rules/VideoUrl.php b/library/Rules/VideoUrl.php index 286c7411..dc912cc2 100644 --- a/library/Rules/VideoUrl.php +++ b/library/Rules/VideoUrl.php @@ -31,19 +31,19 @@ use function sprintf; */ final class VideoUrl extends AbstractRule { + private const SERVICES = [ + // phpcs:disable Generic.Files.LineLength.TooLong + 'youtube' => '@^https?://(www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/]{11})@i', + 'vimeo' => '@^https?://(www\.)?(player\.)?(vimeo\.com/)((channels/[A-z]+/)|(groups/[A-z]+/videos/)|(video/))?([0-9]+)@i', + 'twitch' => '@^https?://(((www\.)?twitch\.tv/videos/[0-9]+)|clips\.twitch\.tv/[a-zA-Z]+)$@i', + // phpcs:enable Generic.Files.LineLength.TooLong + ]; + /** * @var string|null */ private $service; - // phpcs:disable Generic.Files.LineLength.TooLong - private const SERVICES = [ - 'youtube' => '@^https?://(www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/]{11})@i', - 'vimeo' => '@^https?://(www\.)?(player\.)?(vimeo\.com/)((channels/[A-z]+/)|(groups/[A-z]+/videos/)|(video/))?([0-9]+)@i', - 'twitch' => '@^https?://(((www\.)?twitch\.tv/videos/[0-9]+)|clips\.twitch\.tv/[a-zA-Z]+)$@i', - ]; - // phpcs:enable Generic.Files.LineLength.TooLong - /** * Create a new instance VideoUrl. * diff --git a/library/Validator.php b/library/Validator.php index 285958a0..5afcce1b 100644 --- a/library/Validator.php +++ b/library/Validator.php @@ -181,6 +181,14 @@ use function count; */ final class Validator extends AllOf { + /** + * Create instance validator. + */ + public static function create(): self + { + return new self(); + } + /** * {@inheritDoc} */ @@ -222,12 +230,4 @@ final class Validator extends AllOf return $this; } - - /** - * Create instance validator. - */ - public static function create(): self - { - return new self(); - } } diff --git a/tests/integration/rules/uploaded.phpt b/tests/integration/rules/uploaded.phpt index 5f544b65..0357d924 100644 --- a/tests/integration/rules/uploaded.phpt +++ b/tests/integration/rules/uploaded.phpt @@ -1,17 +1,10 @@ --CREDITS-- Paul Karikari ---SKIPIF-- +--FILE-- ---FILE-- -getFullMessage() . PHP_EOL; } ?> +--SKIPIF-- + --EXPECT-- "filename" must be an uploaded file "filename" must not be an uploaded file diff --git a/tests/unit/Rules/CallTest.php b/tests/unit/Rules/CallTest.php index 559e7c0f..fe8a9e9c 100644 --- a/tests/unit/Rules/CallTest.php +++ b/tests/unit/Rules/CallTest.php @@ -41,26 +41,6 @@ final class CallTest extends TestCase */ private $errorException; - /** - * {@inheritDoc} - */ - protected function setUp(): void - { - $this->errorException = new ErrorException('This is a PHP error'); - - set_error_handler(function (): void { - throw $this->errorException; - }); - } - - /** - * {@inheritDoc} - */ - protected function tearDown(): void - { - restore_error_handler(); - } - /** * @test */ @@ -312,4 +292,24 @@ final class CallTest extends TestCase trigger_error('Forcing PHP to trigger an error'); } + + /** + * {@inheritDoc} + */ + protected function setUp(): void + { + $this->errorException = new ErrorException('This is a PHP error'); + + set_error_handler(function (): void { + throw $this->errorException; + }); + } + + /** + * {@inheritDoc} + */ + protected function tearDown(): void + { + restore_error_handler(); + } } diff --git a/tests/unit/Rules/NoTest.php b/tests/unit/Rules/NoTest.php index 021eb483..b7b48336 100644 --- a/tests/unit/Rules/NoTest.php +++ b/tests/unit/Rules/NoTest.php @@ -35,22 +35,6 @@ final class NoTest extends RuleTestCase */ private $locale; - /** - * {@inheritDoc} - */ - protected function setUp(): void - { - $this->locale = (string) setlocale(LC_ALL, '0'); - } - - /** - * {@inheritDoc} - */ - protected function tearDown(): void - { - setlocale(LC_ALL, $this->locale); - } - /** * {@inheritDoc} */ @@ -139,4 +123,20 @@ final class NoTest extends RuleTestCase self::assertInvalidInput(new No(true), $input); } + + /** + * {@inheritDoc} + */ + protected function setUp(): void + { + $this->locale = (string) setlocale(LC_ALL, '0'); + } + + /** + * {@inheritDoc} + */ + protected function tearDown(): void + { + setlocale(LC_ALL, $this->locale); + } } diff --git a/tests/unit/Rules/UploadedTest.php b/tests/unit/Rules/UploadedTest.php index d8d5d799..34c66b1a 100644 --- a/tests/unit/Rules/UploadedTest.php +++ b/tests/unit/Rules/UploadedTest.php @@ -34,24 +34,6 @@ final class UploadedTest extends RuleTestCase { public const UPLOADED_FILENAME = 'uploaded.ext'; - /** - * {@inheritDoc} - */ - protected function setUp(): void - { - if (!extension_loaded('uopz')) { - throw new SkippedTestError('Extension "uopz" is required to test "Uploaded" rule'); - } - - uopz_set_return( - 'is_uploaded_file', - static function (string $filename): bool { - return $filename === UploadedTest::UPLOADED_FILENAME; - }, - true - ); - } - /** * {@inheritDoc} */ @@ -80,4 +62,22 @@ final class UploadedTest extends RuleTestCase [$rule, new stdClass()], ]; } + + /** + * {@inheritDoc} + */ + protected function setUp(): void + { + if (!extension_loaded('uopz')) { + throw new SkippedTestError('Extension "uopz" is required to test "Uploaded" rule'); + } + + uopz_set_return( + 'is_uploaded_file', + static function (string $filename): bool { + return $filename === UploadedTest::UPLOADED_FILENAME; + }, + true + ); + } } diff --git a/tests/unit/Rules/YesTest.php b/tests/unit/Rules/YesTest.php index 045bdef3..a4a325e8 100644 --- a/tests/unit/Rules/YesTest.php +++ b/tests/unit/Rules/YesTest.php @@ -39,22 +39,6 @@ final class YesTest extends RuleTestCase */ private $locale; - /** - * {@inheritDoc} - */ - protected function setUp(): void - { - $this->locale = (string) setlocale(LC_ALL, '0'); - } - - /** - * {@inheritDoc} - */ - protected function tearDown(): void - { - setlocale(LC_ALL, $this->locale); - } - /** * {@inheritDoc} */ @@ -146,4 +130,20 @@ final class YesTest extends RuleTestCase self::assertInvalidInput(new Yes(true), $input); } + + /** + * {@inheritDoc} + */ + protected function setUp(): void + { + $this->locale = (string) setlocale(LC_ALL, '0'); + } + + /** + * {@inheritDoc} + */ + protected function tearDown(): void + { + setlocale(LC_ALL, $this->locale); + } }