From b2f9ed062d1c0af45c5430bd7823b9ff4057e74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antal=20=C3=81ron?= Date: Thu, 24 Oct 2024 07:20:56 +0200 Subject: [PATCH] Update CS --- .github/workflows/test.yaml | 6 +- .gitignore | 3 +- composer.json | 5 +- src/VatNumber.php | 7 +- src/VatNumberValidator.php | 255 +++++++++--------- .../Tests/AbstractConstraintValidatorTest.php | 32 +++ tests/Tests/VatNumberValidatorTest.php | 22 +- tests/bootstrap.php | 6 +- 8 files changed, 179 insertions(+), 157 deletions(-) create mode 100644 tests/Tests/AbstractConstraintValidatorTest.php diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 612bfbb..b627ff4 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -31,7 +31,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-version: ['5.6', '7.0', '7.4', '8.1', '8.2'] + php-version: ['7.4', '8.1', '8.2'] dependency: [lowest, highest] steps: - name: Checkout code @@ -39,11 +39,11 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.1' + php-version: ${{ matrix.php-version }} extensions: intl, mbstring - name: Get composer cache directory id: composer-cache - run: echo "dir=$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache composer dependencies uses: actions/cache@v4 with: diff --git a/.gitignore b/.gitignore index f65fd52..8902c23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ vendor/ composer.lock -.php_cs.cache -.php_cs tools/ +.phpunit.result.cache diff --git a/composer.json b/composer.json index 9c484d3..08d52dd 100644 --- a/composer.json +++ b/composer.json @@ -10,13 +10,14 @@ } ], "require": { - "symfony/validator": "^2.8|^3.0|^4.0|^5.0" + "php": ">= 7.4", + "symfony/validator": "^2.8|^3.0|^4.0|^5.0|^6.0|^7.0" }, "require-dev": { "phpunit/phpunit": "*" }, "conflict": { - "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0" + "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0|>=10" }, "autoload": { "psr-4": { diff --git a/src/VatNumber.php b/src/VatNumber.php index 97a3799..2523186 100644 --- a/src/VatNumber.php +++ b/src/VatNumber.php @@ -22,19 +22,16 @@ */ class VatNumber extends Constraint { - const MESSAGE = 'Not a VAT number.'; + public const MESSAGE = 'Not a VAT number.'; public $message = self::MESSAGE; public $extraVat; - /** - * {@inheritdoc} - */ public function __construct($options = null) { parent::__construct($options); - if (null !== $this->extraVat && !is_callable($this->extraVat)) { + if (null !== $this->extraVat && !\is_callable($this->extraVat)) { throw new ConstraintDefinitionException('The option "extraVat" must be callable'); } } diff --git a/src/VatNumberValidator.php b/src/VatNumberValidator.php index b8bef2f..6b6f7ab 100644 --- a/src/VatNumberValidator.php +++ b/src/VatNumberValidator.php @@ -72,30 +72,26 @@ class VatNumberValidator extends ConstraintValidator '/^(SI)([1-9]\d{7})$/', // + Slovenia '/^(SK)([1-9]\d[2346-9]\d{7})$/', // + Slovakia ]; - - /** - * - * @var string Original VAT number - */ - protected $originalVAT = ""; /** - * {@inheritdoc} + * @var string Original VAT number */ + protected $originalVAT = ''; + public function validate($value, Constraint $constraint) { if (null === $value || '' === $value) { return; } - - //stored original VAT + + // stored original VAT $this->originalVAT = $value; // Uppercase, remove spaces etc. from the VAT number to help validation $value = preg_replace('/(\s|-|\.)+/', '', strtoupper($value)); // Check user callable first - if (null !== $constraint->extraVat && call_user_func($constraint->extraVat, $value)) { + if (null !== $constraint->extraVat && \call_user_func($constraint->extraVat, $value)) { return; } @@ -122,7 +118,8 @@ public function validate($value, Constraint $constraint) // If we reached this point, it is invalid $this->context->buildViolation($constraint->message) - ->addViolation(); + ->addViolation() + ; } /** @@ -139,7 +136,7 @@ private function ATcheck($number) $temp = 0; // Extract the next digit and multiply by the appropriate multiplier - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $temp = (int) $number[$i] * $multipliers[$i]; if (9 < $temp) { $total += floor($temp / 10) + $temp % 10; @@ -156,7 +153,7 @@ private function ATcheck($number) // Compare it with the last character of the VAT number. If it's the // same, then it's valid - return $total === (int) $number[7]; + return (int) $number[7] === $total; } /** @@ -169,7 +166,7 @@ private function ATcheck($number) private function BEcheck($number) { // Nine digit numbers have a 0 inserted at the front. - if (9 === strlen($number)) { + if (9 === \strlen($number)) { $number = '0'.$number; } @@ -191,25 +188,25 @@ private function BEcheck($number) private function BGcheck($number) { // Check the check digit of 9 digit Bulgarian VAT numbers. - if (9 === strlen($number)) { + if (9 === \strlen($number)) { $total = 0; // First try to calculate the check digit using the first multipliers $temp = 0; - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $temp += (int) $number[$i] * ($i + 1); } // See if we have a check digit yet $total = $temp % 11; if (10 !== $total) { - return $total === (int) substr($number, 8); + return (int) substr($number, 8) === $total; } // We got a modulus of 10 before so we have to keep going. Calculate // the new check digit using the different multipliers $temp = 0; - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $temp += (int) $number[$i] * ($i + 3); } @@ -220,25 +217,25 @@ private function BGcheck($number) $total = 0; } - return $total === (int) substr($number, 8); + return (int) substr($number, 8) === $total; } if (0 !== preg_match('/^\d\d[0-5]\d[0-3]\d\d{4}$/', $number)) { // Check month $month = (int) substr($number, 2, 2); - if (($month > 0 && $month < 13) || - ($month > 20 && $month < 33) || - ($month > 40 && $month < 53) + if ((0 < $month && 13 > $month) + || (20 < $month && 33 > $month) + || (40 < $month && 53 > $month) ) { // Extract the next digit and multiply by the counter. $multipliers = [2, 4, 8, 5, 10, 9, 7, 3, 6]; $total = 0; - for ($i = 0; $i < 9; ++$i) { + for ($i = 0; 9 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } // Establish check digit. - $total = $total % 11; + $total %= 11; if (10 === $total) { $total = 0; } @@ -256,7 +253,7 @@ private function BGcheck($number) // Extract the next digit and multiply by the counter. $multipliers = [21, 19, 17, 13, 11, 9, 7, 3, 1]; $total = 0; - for ($i = 0; $i < 9; ++$i) { + for ($i = 0; 9 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -271,7 +268,7 @@ private function BGcheck($number) // Extract the next digit and multiply by the counter. $multipliers = [4, 3, 2, 7, 6, 5, 4, 3, 2]; $total = 0; - for ($i = 0; $i < 9; ++$i) { + for ($i = 0; 9 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -285,7 +282,7 @@ private function BGcheck($number) // Check to see if the check digit given is correct, If not, we have // an error with the VAT number - return $total === (int) $number[9]; + return (int) $number[9] === $total; } /** @@ -299,7 +296,7 @@ private function CHEcheck($number) { $multipliers = [5, 4, 3, 2, 7, 6, 5, 4]; $total = 0; - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -313,7 +310,7 @@ private function CHEcheck($number) // Check to see if the check digit given is correct, If not, we have // an error with the VAT number - return $total === (int) $number[8]; + return (int) $number[8] === $total; } /** @@ -332,7 +329,7 @@ private function CYcheck($number) // Extract the next digit and multiply by the counter. $total = 0; - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $temp = (int) $number[$i]; if (0 === $i % 2) { if (0 === $temp) { @@ -353,8 +350,8 @@ private function CYcheck($number) } // Establish check digit using modulus 26, and translate to char. equivalent. - $total = $total % 26; - $total = chr($total + 65); + $total %= 26; + $total = \chr($total + 65); // Check to see if the check digit given is correct return $number[8] === $total; @@ -383,7 +380,7 @@ private function CZcheck($number) // Legal entities if (0 !== preg_match($czExpr[0], $number)) { // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -405,7 +402,7 @@ private function CZcheck($number) // Individuals type 2 (Special Cases) - 9 digits including check digit elseif (0 !== preg_match($czExpr[2], $number)) { // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $total += (int) $number[$i + 1] * $multipliers[$i]; } @@ -449,7 +446,7 @@ private function DEcheck($number) $product = 10; $sum = 0; $checkDigit = 0; - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { // Extract the next digit and implement peculiar algorithm!. $sum = ((int) $number[$i] + $product) % 10; @@ -485,12 +482,12 @@ private function DKcheck($number) $multipliers = [2, 7, 6, 5, 4, 3, 2, 1]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } // Establish check digit. - $total = $total % 11; + $total %= 11; // The remainder should be 0 for it to be valid. return 0 === $total; @@ -509,7 +506,7 @@ private function EEcheck($number) $multipliers = [3, 7, 1, 3, 7, 1, 3, 7]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -535,18 +532,18 @@ private function ELcheck($number) $total = 0; $multipliers = [256, 128, 64, 32, 16, 8, 4, 2]; - //eight character numbers should be prefixed with an 0. - if (8 === strlen($number)) { + // eight character numbers should be prefixed with an 0. + if (8 === \strlen($number)) { $number = '0'.$number; } // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } // Establish check digit. - $total = $total % 11; + $total %= 11; if (9 < $total) { $total = 0; } @@ -578,7 +575,7 @@ private function EScheck($number) // National juridical entities if (0 !== preg_match($esExpr[0], $number)) { // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $temp = (int) $number[$i + 1] * $multipliers[$i]; if (9 < $temp) { $total += floor($temp / 10) + $temp % 10; @@ -599,7 +596,7 @@ private function EScheck($number) // Juridical entities other than national ones elseif (0 !== preg_match($esExpr[1], $number)) { // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $temp = (int) $number[$i + 1] * $multipliers[$i]; if (9 < $temp) { $total += floor($temp / 10) + $temp % 10; @@ -610,7 +607,7 @@ private function EScheck($number) // Now calculate the check digit itself. $total = 10 - $total % 10; - $total = chr($total + 64); + $total = \chr($total + 64); // Compare it with the last character of the VAT number. If it's the same, then it's valid. return $total === $number[8]; @@ -626,13 +623,13 @@ private function EScheck($number) $charString = 'TRWAGMYFPDXBNJZSQVHLCKE'; - return $tempNumber[8] === $charString[(int) substr($tempNumber, 0, 8) % 23]; + return $charString[(int) substr($tempNumber, 0, 8) % 23] === $tempNumber[8]; } // Personal number (NIF) (starting with K, L, M, or X) elseif (0 !== preg_match($esExpr[3], $number)) { $charString = 'TRWAGMYFPDXBNJZSQVHLCKE'; - return $tempNumber[8] === $charString[(int) substr($tempNumber, 1, 7) % 23]; + return $charString[(int) substr($tempNumber, 1, 7) % 23] === $tempNumber[8]; } return false; @@ -667,7 +664,7 @@ private function FIcheck($number) $multipliers = [7, 9, 10, 5, 8, 4, 2]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -691,7 +688,7 @@ private function FIcheck($number) private function FRcheck($number) { // Valid for 32-bit systems - if (4 === PHP_INT_SIZE) { + if (4 === \PHP_INT_SIZE) { return true; } @@ -742,7 +739,7 @@ private function GBcheck($number) $no = (int) substr($number, 0, 7); // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -751,30 +748,30 @@ private function GBcheck($number) // Establish check digits by subtracting 97 from total until negative. $cd = $total; - while ($cd > 0) { - $cd = $cd - 97; + while (0 < $cd) { + $cd -= 97; } // Get the absolute value and compare it with the last two characters of the VAT number. If the // same, then it is a valid traditional check digit. However, even then the number must fit within // certain specified ranges. $cd = abs($cd); - if ((int) substr($number, 7, 2) === $cd && - $no < 9990001 && - ($no < 100000 || $no > 999999) && - ($no < 9490001 || $no > 9700000) + if ((int) substr($number, 7, 2) === $cd + && 9990001 > $no + && (100000 > $no || 999999 < $no) + && (9490001 > $no || 9700000 < $no) ) { return true; } // Now try the new method by subtracting 55 from the check digit if we can - else add 42 - if ($cd >= 55) { - $cd = $cd - 55; + if (55 <= $cd) { + $cd -= 55; } else { - $cd = $cd + 42; + $cd += 42; } - return (int) substr($number, 7, 2) === $cd && $no > 1000000; + return (int) substr($number, 7, 2) === $cd && 1000000 < $no; } /** @@ -791,7 +788,7 @@ private function HRcheck($number) $sum = 0; $checkDigit = 0; - for ($i = 0; $i < 10; ++$i) { + for ($i = 0; 10 > $i; ++$i) { // Extract the next digit and implement the algorithm $sum = ((int) $number[$i] + $product) % 10; if (0 === $sum) { @@ -818,7 +815,7 @@ private function HUcheck($number) $multipliers = [9, 7, 3, 1, 9, 7, 3]; // Extract the next digit and multiply by the counter - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -830,7 +827,7 @@ private function HUcheck($number) // Compare it with the last character of the VAT number. If it's the // same, then it's valid - return $total === (int) $number[7]; + return (int) $number[7] === $total; } /** @@ -851,7 +848,7 @@ private function IEcheck($number) } // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -866,11 +863,11 @@ private function IEcheck($number) } // Establish check digit using modulus 23, and translate to char. equivalent. - $total = $total % 23; + $total %= 23; if (0 === $total) { $total = 'W'; } else { - $total = chr($total + 64); + $total = \chr($total + 64); } // Compare it with the eighth character of the VAT number. If it's the same, then it's valid. @@ -895,14 +892,14 @@ private function ITcheck($number) } $temp = (int) substr($number, 7, 3); - if (($temp < 1) || ($temp > 201) && $temp !== 999 && $temp !== 888) { + if ((1 > $temp) || (201 < $temp) && 999 !== $temp && 888 !== $temp) { return false; } // Extract the next digit and multiply by the appropriate - for ($i = 0; $i < 10; ++$i) { + for ($i = 0; 10 > $i; ++$i) { $temp = (int) $number[$i] * $multipliers[$i]; - if ($temp > 9) { + if (9 < $temp) { $total += floor($temp / 10) + $temp % 10; } else { $total += $temp; @@ -911,7 +908,7 @@ private function ITcheck($number) // Establish check digit. $total = 10 - $total % 10; - if ($total > 9) { + if (9 < $total) { $total = 0; } @@ -929,7 +926,7 @@ private function ITcheck($number) private function LTcheck($number) { // 9 character VAT numbers are for legal persons - if (9 === strlen($number)) { + if (9 === \strlen($number)) { // 8th character must be one if ('1' !== $number[7]) { return false; @@ -937,7 +934,7 @@ private function LTcheck($number) // Extract the next digit and multiply by the counter+1. $total = 0; - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $total += (int) $number[$i] * ($i + 1); } @@ -945,13 +942,13 @@ private function LTcheck($number) if (10 === $total % 11) { $multipliers = [3, 4, 5, 6, 7, 8, 9, 1]; $total = 0; - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } } // Establish check digit. - $total = $total % 11; + $total %= 11; if (10 === $total) { $total = 0; } @@ -969,7 +966,7 @@ private function LTcheck($number) // Extract the next digit and multiply by the counter+1. $total = 0; $multipliers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]; - for ($i = 0; $i < 11; ++$i) { + for ($i = 0; 11 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -977,13 +974,13 @@ private function LTcheck($number) if (10 === $total % 11) { $multipliers = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4]; $total = 0; - for ($i = 0; $i < 11; ++$i) { + for ($i = 0; 11 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } } // Establish check digit. - $total = $total % 11; + $total %= 11; if (10 === $total) { $total = 0; } @@ -1024,7 +1021,7 @@ private function LVcheck($number) $multipliers = [9, 1, 4, 8, 3, 10, 2, 5, 7, 6]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 10; ++$i) { + for ($i = 0; 10 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -1057,7 +1054,7 @@ private function MTcheck($number) $multipliers = [3, 4, 6, 7, 8, 9]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 6; ++$i) { + for ($i = 0; 6 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -1081,12 +1078,12 @@ private function NLcheck($number) $multipliers = [9, 8, 7, 6, 5, 4, 3, 2]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } // Establish check digits by getting modulus 11. - $total = $total % 11; + $total %= 11; if (9 < $total) { $total = 0; } @@ -1095,10 +1092,10 @@ private function NLcheck($number) if ((int) $number[8] === $total) { return true; } - - //Traditional dutch VAT does not match, try to check whether it is a valid sole proprietor vat number - - // take the original vat string + + // Traditional dutch VAT does not match, try to check whether it is a valid sole proprietor vat number + + // take the original vat string $vat = $this->originalVAT; // Each character in the string is looked at one at a time @@ -1107,46 +1104,44 @@ private function NLcheck($number) // The result of the conversion goes here $numericvat = null; - //part length splitting long numbers in modulo operations + // part length splitting long numbers in modulo operations $partLength = 7; - - //constant divisor for calculation + + // constant divisor for calculation $divisor = 97; - - for ($i = 0; $i < strlen($vat); $i++) { - // Pick up the next character from the vat string as Unicode - $nextchar = ord(substr($vat, $i, $i + 1)); + for ($i = 0; \strlen($vat) > $i; ++$i) { + // Pick up the next character from the vat string as Unicode + $nextchar = \ord(substr($vat, $i, $i + 1)); // If it a '+' or a '*' convert to numeric 36 and 37 respectively - if ($nextchar > 41 && $nextchar < 44) { - $nextchar = $nextchar - 6; + if (41 < $nextchar && 44 > $nextchar) { + $nextchar -= 6; - // Convert 0 to 9 to 0 to 9 characters - } else if ($nextchar > 47 && $nextchar < 58) { - $nextchar = $nextchar - 48; + // Convert 0 to 9 to 0 to 9 characters + } elseif (47 < $nextchar && 58 > $nextchar) { + $nextchar -= 48; - // Convert A-Z to 10 to 35 - } else if ($nextchar > 64 && $nextchar < 91) { - $nextchar = $nextchar - 55; + // Convert A-Z to 10 to 35 + } elseif (64 < $nextchar && 91 > $nextchar) { + $nextchar -= 55; } // Add to convert test string - $numericvat = $numericvat . $nextchar; + $numericvat .= $nextchar; } - while (strlen($numericvat) > $partLength) { + while (\strlen($numericvat) > $partLength) { $part = substr($numericvat, 0, $partLength); - $numericvat = ($part % $divisor) . substr($numericvat, $partLength); + $numericvat = ($part % $divisor).substr($numericvat, $partLength); } - if (1 == ($numericvat % $divisor)) { + if (1 === ($numericvat % $divisor)) { return true; } - - + // Neither - it must be invalid return false; } - + /** * Checks the check digits of a Norwegian VAT number. * @@ -1162,7 +1157,7 @@ private function NOcheck($number) $multipliers = [3, 2, 7, 6, 5, 4, 3, 2]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -1189,12 +1184,12 @@ private function PLcheck($number) $multipliers = [6, 5, 7, 2, 3, 4, 5, 6, 7]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 9; ++$i) { + for ($i = 0; 9 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } // Establish check digits subtracting modulus 11 from 11. - $total = $total % 11; + $total %= 11; if (9 < $total) { $total = 0; } @@ -1216,7 +1211,7 @@ private function PTcheck($number) $multipliers = [9, 8, 7, 6, 5, 4, 3, 2]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -1242,10 +1237,10 @@ private function ROcheck($number) $multipliers = [7, 5, 3, 2, 1, 7, 5, 3, 2]; // Extract the next digit and multiply by the counter. - $VATlen = strlen($number); + $VATlen = \strlen($number); $total = 0; - $multipliers = array_slice($multipliers, 1 - $VATlen); - for ($i = 0; $i < $VATlen - 1; ++$i) { + $multipliers = \array_slice($multipliers, 1 - $VATlen); + for ($i = 0; $VATlen - 1 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -1271,7 +1266,7 @@ private function RScheck($number) $product = 10; $sum = 0; - for ($i = 0; $i < 8; ++$i) { + for ($i = 0; 8 > $i; ++$i) { // Extract the next digit and implement the algorithm $sum = ((int) $number[$i] + $product) % 10; if (0 === $sum) { @@ -1296,45 +1291,45 @@ private function RScheck($number) private function RUcheck($number) { // 10 digit INN numbers - if (10 === strlen($number)) { + if (10 === \strlen($number)) { $total = 0; $multipliers = [2, 4, 10, 3, 5, 9, 4, 6, 8, 0]; - for ($i = 0; $i < 10; ++$i) { + for ($i = 0; 10 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } - $total = $total % 11; + $total %= 11; if (9 < $total) { - $total = $total % 10; + $total %= 10; } // Compare it with the last character of the VAT number. If it is the same, then it's valid return (int) $number[9] === $total; } // 12 digit INN numbers - elseif (12 === strlen($number)) { + elseif (12 === \strlen($number)) { $total1 = 0; $multipliers1 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0]; $total2 = 0; $multipliers2 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0]; - for ($i = 0; $i < 11; ++$i) { + for ($i = 0; 11 > $i; ++$i) { $total1 += (int) $number[$i] * $multipliers1[$i]; } - $total1 = $total1 % 11; + $total1 %= 11; if (9 < $total1) { - $total1 = $total1 % 10; + $total1 %= 10; } - for ($i = 0; $i < 11; ++$i) { + for ($i = 0; 11 > $i; ++$i) { $total2 += (int) $number[$i] * $multipliers2[$i]; } - $total2 = $total2 % 11; + $total2 %= 11; if (9 < $total2) { - $total2 = $total2 % 10; + $total2 %= 10; } // Compare the first check with the 11th character and the second @@ -1357,14 +1352,14 @@ private function SEcheck($number) { // Calculate R where R = R1 + R3 + R5 + R7 + R9, and Ri = INT(Ci/5) + (Ci*2) modulo 10 $R = 0; - for ($i = 0; $i < 9; $i = $i + 2) { + for ($i = 0; 9 > $i; $i += 2) { $digit = (int) $number[$i]; $R += floor($digit / 5) + ((2 * $digit) % 10); } // Calculate S where S = C2 + C4 + C6 + C8 $S = 0; - for ($i = 1; $i < 9; $i = $i + 2) { + for ($i = 1; 9 > $i; $i += 2) { $S += (int) $number[$i]; } @@ -1388,7 +1383,7 @@ private function SIcheck($number) $multipliers = [8, 7, 6, 5, 4, 3, 2]; // Extract the next digit and multiply by the counter. - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -1413,7 +1408,7 @@ private function SIcheck($number) private function SKcheck($number) { // Valid for 32-bit systems - if (4 === PHP_INT_SIZE) { + if (4 === \PHP_INT_SIZE) { return true; } diff --git a/tests/Tests/AbstractConstraintValidatorTest.php b/tests/Tests/AbstractConstraintValidatorTest.php new file mode 100644 index 0000000..549275e --- /dev/null +++ b/tests/Tests/AbstractConstraintValidatorTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Antalaron\Component\VatNumberValidator\Tests; + +use Antalaron\Component\VatNumberValidator\Tests\AbstractConstraintValidatorMiddlewareTest as SymfonyAbstractConstraintValidatorTest; +use Antalaron\Component\VatNumberValidator\VatNumberValidator; +use Symfony\Component\Validator\ConstraintValidatorInterface; + +if (\PHP_VERSION_ID >= 80200) { + class AbstractConstraintValidatorTest extends SymfonyAbstractConstraintValidatorTest + { + protected function createValidator(): ConstraintValidatorInterface + { + return new VatNumberValidator(); + } + } +} else { + class AbstractConstraintValidatorTest extends SymfonyAbstractConstraintValidatorTest + { + protected function createValidator() + { + return new VatNumberValidator(); + } + } +} diff --git a/tests/Tests/VatNumberValidatorTest.php b/tests/Tests/VatNumberValidatorTest.php index ebf6d25..82ff1f6 100644 --- a/tests/Tests/VatNumberValidatorTest.php +++ b/tests/Tests/VatNumberValidatorTest.php @@ -10,15 +10,9 @@ namespace Antalaron\Component\VatNumberValidator\Tests; use Antalaron\Component\VatNumberValidator\VatNumber; -use Antalaron\Component\VatNumberValidator\VatNumberValidator; class VatNumberValidatorTest extends AbstractConstraintValidatorTest { - protected function createValidator() - { - return new VatNumberValidator(); - } - public function testNullIsValid() { $this->validator->validate(null, new VatNumber()); @@ -33,11 +27,12 @@ public function testVatNumbers($vatNumber, $valid, $shouldWorkOn32bit = true) { $this->validator->validate($vatNumber, new VatNumber()); - if ($valid || (4 === PHP_INT_SIZE && !$shouldWorkOn32bit)) { + if ($valid || (4 === \PHP_INT_SIZE && !$shouldWorkOn32bit)) { $this->assertNoViolation(); } else { $this->buildViolation(VatNumber::MESSAGE) - ->assertRaised(); + ->assertRaised() + ; } } @@ -2129,7 +2124,8 @@ public function testWithoutExtraVat() { $this->validator->validate('11316385-2-18', new VatNumber()); $this->buildViolation(VatNumber::MESSAGE) - ->assertRaised(); + ->assertRaised() + ; } public function testExtraVat() @@ -2142,7 +2138,7 @@ public function testExtraVat() $total = 0; $multipliers = [9, 7, 3, 1, 9, 7, 3]; - for ($i = 0; $i < 7; ++$i) { + for ($i = 0; 7 > $i; ++$i) { $total += (int) $number[$i] * $multipliers[$i]; } @@ -2151,7 +2147,7 @@ public function testExtraVat() $total = 0; } - return $total === (int) $number[7]; + return (int) $number[7] === $total; }])); $this->assertNoViolation(); @@ -2162,6 +2158,8 @@ public function testExtraVat() */ public function testInvalidExtraVat() { - $this->validator->validate('11316385-2-18', new VatNumber(['extraVat' => new \stdClass()])); + $this->validator->validate('11316385-2-18', new VatNumber(['extraVat' => function ($number) { + return 'invalid'; + }])); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index cb80701..487f5a5 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,7 +8,7 @@ */ if (!@include __DIR__.'/../vendor/autoload.php') { - die('You must set up the project dependencies, run the following commands: + exit('You must set up the project dependencies, run the following commands: wget http://getcomposer.org/composer.phar php composer.phar install --dev '); @@ -19,7 +19,7 @@ class_alias('PHPUnit\Framework\Assert', 'PHPUnit\Framework\Assert'); } if (class_exists('Symfony\Component\Validator\Test\ConstraintValidatorTestCase')) { - class_alias('Symfony\Component\Validator\Test\ConstraintValidatorTestCase', 'Antalaron\Component\VatNumberValidator\Tests\AbstractConstraintValidatorTest'); + class_alias('Symfony\Component\Validator\Test\ConstraintValidatorTestCase', 'Antalaron\Component\VatNumberValidator\Tests\AbstractConstraintValidatorMiddlewareTest'); } else { - class_alias('Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest', 'Antalaron\Component\VatNumberValidator\Tests\AbstractConstraintValidatorTest'); + class_alias('Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest', 'Antalaron\Component\VatNumberValidator\Tests\AbstractConstraintValidatorMiddlewareTest'); }