From bf5cdfc1cdd9ddf15a7ad3d59f9bc54fd460a88d Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 25 Jul 2020 22:10:27 +0200 Subject: [PATCH] Enable Psalm, Infection and Insights reports. --- .github/FUNDING.yml | 1 + .github/workflows/continuous-integration.yml | 24 ++++++- README.md | 1 + composer.json | 10 +-- grumphp.yml.dist | 6 +- psalm.xml | 15 +++++ spec/loophp/Tin/TINSpec.php | 5 ++ src/CountryHandler/Belgium.php | 21 +++---- src/CountryHandler/Bulgaria.php | 11 +--- src/CountryHandler/CountryHandler.php | 63 +++---------------- .../CountryHandlerInterface.php | 7 --- src/CountryHandler/Cyprus.php | 5 -- src/CountryHandler/CzechRepublic.php | 7 +-- src/CountryHandler/Denmark.php | 18 +++--- src/CountryHandler/Estonia.php | 8 +-- src/CountryHandler/Finland.php | 10 +-- src/CountryHandler/France.php | 6 +- src/CountryHandler/Germany.php | 19 ++++-- src/CountryHandler/Hungary.php | 2 +- src/CountryHandler/Ireland.php | 4 +- src/CountryHandler/Italy.php | 11 ++-- src/CountryHandler/Latvia.php | 11 +--- src/CountryHandler/Lithuania.php | 6 +- src/CountryHandler/Luxembourg.php | 6 +- src/CountryHandler/Poland.php | 6 +- src/CountryHandler/Romania.php | 13 ++-- src/CountryHandler/Slovakia.php | 2 +- src/CountryHandler/Slovenia.php | 2 +- src/CountryHandler/Spain.php | 10 ++- src/CountryHandler/Sweden.php | 30 +++++---- src/CountryHandler/UnitedKingdom.php | 6 +- src/TIN.php | 40 ++++-------- 32 files changed, 159 insertions(+), 227 deletions(-) create mode 100644 psalm.xml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index b4c89d9..fd9654b 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1,2 @@ github: drupol +custom: ["https://www.paypal.me/drupol"] diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index e09ab4a..164f4de 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -17,6 +17,11 @@ jobs: php-versions: ['7.1', '7.2', '7.3', '7.4'] steps: + - name: Set git to use LF + run: | + git config --global core.autocrlf false + git config --global core.eol lf + - name: Checkout uses: actions/checkout@master with: @@ -27,12 +32,13 @@ jobs: with: php-version: ${{ matrix.php-versions }} extensions: mbstring,xdebug,pcov + - name: Get Composer Cache Directory id: composer-cache run: echo "::set-output name=dir::$(composer config cache-files-dir)" - name: Cache dependencies - uses: actions/cache@v1 + uses: actions/cache@v2 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} @@ -42,7 +48,7 @@ jobs: run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader - name: Run Grumphp - run: vendor/bin/grumphp run + run: vendor/bin/grumphp run --no-ansi -n env: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} @@ -50,8 +56,20 @@ jobs: run: vendor/bin/psalm --shepherd --stats continue-on-error: true - - name: Scrutinizer + - name: Send Scrutinizer data run: | wget https://scrutinizer-ci.com/ocular.phar php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml continue-on-error: true + + - name: Infection score report + run: | + vendor/bin/infection run -j 2 + continue-on-error: true + + - name: PHP Insights report + run: | + rm -rf composer.lock vendor + composer require nunomaduro/phpinsights --dev + vendor/bin/phpinsights analyse src/ -n + continue-on-error: true diff --git a/README.md b/README.md index b3b5eab..22ac226 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Total Downloads](https://img.shields.io/packagist/dt/loophp/tin.svg?style=flat-square)](https://packagist.org/packages/loophp/tin) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/loophp/tin/Continuous%20Integration?style=flat-square)](https://github.com/loophp/tin/actions) [![Scrutinizer code quality](https://img.shields.io/scrutinizer/quality/g/loophp/tin/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/loophp/tin/?branch=master) + [![Type Coverage](https://shepherd.dev/github/loophp/tin/coverage.svg)](https://shepherd.dev/github/loophp/tin) [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/loophp/tin/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/loophp/tin/?branch=master) [![Mutation testing badge](https://badge.stryker-mutator.io/github.com/loophp/tin/master)](https://stryker-mutator.github.io) [![License](https://img.shields.io/packagist/l/loophp/tin.svg?style=flat-square)](https://packagist.org/packages/loophp/tin) diff --git a/composer.json b/composer.json index 25c0181..1704a34 100644 --- a/composer.json +++ b/composer.json @@ -15,14 +15,16 @@ } ], "require": { - "php": "^7.1" + "php": "^7.1", + "symfony/polyfill-mbstring": "^1.18" }, "require-dev": { - "drupol/php-conventions": "^1.6", + "drupol/php-conventions": "^1.7.2 || ^1.8.6", "friends-of-phpspec/phpspec-code-coverage": "^4.3", "infection/infection": "^0.13.6 || ^0.15.3", - "phpspec/phpspec": "^5.1.2 || ^6.1.1", - "phpstan/phpstan-strict-rules": "^0.12.2" + "phpspec/phpspec": "^5.1.2 || ^6.2.1", + "phpstan/phpstan-strict-rules": "^0.12.2", + "vimeo/psalm": "^3.12" }, "autoload": { "psr-4": { diff --git a/grumphp.yml.dist b/grumphp.yml.dist index bc6c50d..91b29fe 100644 --- a/grumphp.yml.dist +++ b/grumphp.yml.dist @@ -2,18 +2,14 @@ imports: - { resource: vendor/drupol/php-conventions/config/php71/grumphp.yml } parameters: - process_timeout: 3600 extra_tasks: + psalm: ~ phpspec: verbose: true - metadata: - priority: 3000 infection: threads: 1 test_framework: phpspec configuration: infection.json.dist min_msi: 50 min_covered_msi: 50 - metadata: - priority: 2000 diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..9d2a538 --- /dev/null +++ b/psalm.xml @@ -0,0 +1,15 @@ + + + + + + + + + diff --git a/spec/loophp/Tin/TINSpec.php b/spec/loophp/Tin/TINSpec.php index 824a83d..ac89f1d 100644 --- a/spec/loophp/Tin/TINSpec.php +++ b/spec/loophp/Tin/TINSpec.php @@ -65,4 +65,9 @@ public function it_is_initializable() { $this->shouldHaveType(TIN::class); } + + public function let() + { + $this->beConstructedThrough('fromSlug', ['foo123']); + } } diff --git a/src/CountryHandler/Belgium.php b/src/CountryHandler/Belgium.php index 29efca8..9a5b297 100644 --- a/src/CountryHandler/Belgium.php +++ b/src/CountryHandler/Belgium.php @@ -24,11 +24,6 @@ final class Belgium extends CountryHandler */ public const PATTERN = '\\d{2}[0-1]\\d[0-3]\\d{6}'; - /** - * @param string $tin - * - * @return bool - */ protected function hasValidDate(string $tin): bool { return 0 !== $this->getDateType($tin); @@ -41,14 +36,14 @@ protected function hasValidRule(string $tin): bool private function getDateType(string $tin): int { - $year = (int) (substr($tin, 0, 2)); - $month = (int) (substr($tin, 2, 2)); - $day = (int) (substr($tin, 4, 2)); + $year = (int) (mb_substr($tin, 0, 2)); + $month = (int) (mb_substr($tin, 2, 2)); + $day = (int) (mb_substr($tin, 4, 2)); $y1 = checkdate($month, $day, 1900 + $year); $y2 = checkdate($month, $day, 2000 + $year); - if (0 === $day || 0 === $month || $y1 && $y2) { + if (0 === $day || 0 === $month || ($y1 && $y2)) { return 3; } @@ -65,9 +60,9 @@ private function getDateType(string $tin): int private function isFollowBelgiumRule1(string $tin): bool { - $divisionRemainderBy97 = (int) (substr($tin, 0, 9)) % 97; + $divisionRemainderBy97 = (int) (mb_substr($tin, 0, 9)) % 97; - return 97 - $divisionRemainderBy97 === (int) (substr($tin, 9, 3)); + return 97 - $divisionRemainderBy97 === (int) (mb_substr($tin, 9, 3)); } private function isFollowBelgiumRule1AndIsDateValid(string $tin): bool @@ -79,9 +74,9 @@ private function isFollowBelgiumRule1AndIsDateValid(string $tin): bool private function isFollowBelgiumRule2(string $tin): bool { - $divisionRemainderBy97 = (2 + (int) substr($tin, 0, 9)) % 97; + $divisionRemainderBy97 = (2 + (int) mb_substr($tin, 0, 9)) % 97; - return 97 - $divisionRemainderBy97 === (int) (substr($tin, 9, 3)); + return 97 - $divisionRemainderBy97 === (int) (mb_substr($tin, 9, 3)); } private function isFollowBelgiumRule2AndIsDateValid(string $tin): bool diff --git a/src/CountryHandler/Bulgaria.php b/src/CountryHandler/Bulgaria.php index 746d6cb..89783b2 100644 --- a/src/CountryHandler/Bulgaria.php +++ b/src/CountryHandler/Bulgaria.php @@ -24,16 +24,11 @@ final class Bulgaria extends CountryHandler */ public const PATTERN = '\\d{10}'; - /** - * @param string $tin - * - * @return bool - */ protected function hasValidDate(string $tin): bool { - $year = (int) (substr($tin, 0, 2)); - $month = (int) (substr($tin, 2, 2)); - $day = (int) (substr($tin, 4, 2)); + $year = (int) (mb_substr($tin, 0, 2)); + $month = (int) (mb_substr($tin, 2, 2)); + $day = (int) (mb_substr($tin, 4, 2)); if (21 <= $month && 32 >= $month) { return checkdate($month - 20, $day, 1800 + $year); diff --git a/src/CountryHandler/CountryHandler.php b/src/CountryHandler/CountryHandler.php index b98f2a2..955c68d 100644 --- a/src/CountryHandler/CountryHandler.php +++ b/src/CountryHandler/CountryHandler.php @@ -6,8 +6,6 @@ use loophp\Tin\Exception\TINException; -use function strlen; - /** * Base handler class. */ @@ -20,21 +18,16 @@ abstract class CountryHandler implements CountryHandlerInterface /** * CountryHandler constructor. - * - * @param string $tin */ final public function __construct(string $tin = '') { $this->tin = $tin; } - /** - * @return string - */ public function getTIN(): string { if (null !== $string = preg_replace('#[^[:alnum:]\-+]#u', '', $this->tin)) { - return strtoupper($string); + return mb_strtoupper($string); } return ''; @@ -45,7 +38,7 @@ public function getTIN(): string */ final public static function supports(string $country): bool { - return strtoupper($country) === strtoupper(static::COUNTRYCODE); + return mb_strtoupper($country) === mb_strtoupper(static::COUNTRYCODE); } final public function validate(): bool @@ -74,8 +67,6 @@ final public function validate(): bool /** * @param string $tin * The TIN. - * - * @return CountryHandlerInterface */ final public function withTIN(string $tin): CountryHandlerInterface { @@ -87,26 +78,16 @@ final public function withTIN(string $tin): CountryHandlerInterface /** * Get digit at a given position. - * - * @param string $str - * @param int $index - * - * @return int */ protected function digitAt(string $str, int $index): int { - return (int) $str[$index] ?? 0; + return (int) ($str[$index] ?? 0); } - /** - * @param int $int - * - * @return int - */ protected function digitsSum(int $int): int { return array_reduce( - (array) str_split((string) $int), + (array) mb_str_split((string) $int), static function (int $carry, string $digit): int { return $carry + (int) $digit; }, @@ -118,26 +99,17 @@ static function (int $carry, string $digit): int { * Get the alphabetical position. * * eg: A = 1 - * - * @param string $character - * - * @return int */ protected function getAlphabeticalPosition(string $character): int { return false !== ($return = array_combine(range('a', 'z'), range(1, 26))) ? - $return[strtolower($character)] : + $return[mb_strtolower($character)] : 0; } - /** - * @param int $number - * - * @return int - */ protected function getLastDigit(int $number): int { - $split = (array) str_split((string) $number); + $split = (array) mb_str_split((string) $number); return (int) end($split); } @@ -149,21 +121,12 @@ protected function hasValidDate(string $tin): bool /** * Match length. - * - * @param string $tin - * - * @return bool */ protected function hasValidLength(string $tin): bool { return $this->matchLength($this->getTIN(), static::LENGTH); } - /** - * @param string $tin - * - * @return bool - */ protected function hasValidPattern(string $tin): bool { return $this->matchPattern($this->getTIN(), static::PATTERN); @@ -174,23 +137,11 @@ protected function hasValidRule(string $tin): bool return true; } - /** - * @param string $tin - * @param int $length - * - * @return bool - */ protected function matchLength(string $tin, int $length): bool { - return strlen($tin) === $length; + return mb_strlen($tin) === $length; } - /** - * @param string $subject - * @param string $pattern - * - * @return bool - */ protected function matchPattern(string $subject, string $pattern): bool { return 1 === preg_match(sprintf('/%s/', $pattern), $subject); diff --git a/src/CountryHandler/CountryHandlerInterface.php b/src/CountryHandler/CountryHandlerInterface.php index f42a42e..83b72f7 100644 --- a/src/CountryHandler/CountryHandlerInterface.php +++ b/src/CountryHandler/CountryHandlerInterface.php @@ -41,15 +41,8 @@ public static function supports(string $country): bool; * Validate a tin number. * * @throws TINException - * - * @return bool */ public function validate(): bool; - /** - * @param string $tin - * - * @return CountryHandlerInterface - */ public function withTIN(string $tin): CountryHandlerInterface; } diff --git a/src/CountryHandler/Cyprus.php b/src/CountryHandler/Cyprus.php index 1f91b13..7283734 100644 --- a/src/CountryHandler/Cyprus.php +++ b/src/CountryHandler/Cyprus.php @@ -52,11 +52,6 @@ protected function hasValidRule(string $tin): bool return $remainderBy26 + 65 === $c9; } - /** - * @param int $x - * - * @return int - */ private function recodeValue(int $x): int { switch ($x) { diff --git a/src/CountryHandler/CzechRepublic.php b/src/CountryHandler/CzechRepublic.php index c52ca9a..fc6a6b8 100644 --- a/src/CountryHandler/CzechRepublic.php +++ b/src/CountryHandler/CzechRepublic.php @@ -53,11 +53,6 @@ final class CzechRepublic extends CountryHandler */ private const MONTH_FEMALE = 50; - /** - * @param string $tin - * - * @return bool - */ protected function hasValidDate(string $tin): bool { // If we reach this point, it means that it's already validated. @@ -130,7 +125,7 @@ protected function hasValidRule(string $tin): bool // number's modulo should be 0 if (true === $hasModulo) { - $number = (int) $matches['year'] . $matches['month'] . $matches['day'] . $matches['sequence']; + $number = (int) ($matches['year'] . $matches['month'] . $matches['day'] . $matches['sequence']); $modulo = $number % self::MODULO; // from year 1954 to 1985 and sometimes even after that, modulo can be 10 which results in 0 as modulo diff --git a/src/CountryHandler/Denmark.php b/src/CountryHandler/Denmark.php index 3d8c687..302f8f6 100644 --- a/src/CountryHandler/Denmark.php +++ b/src/CountryHandler/Denmark.php @@ -28,9 +28,9 @@ final class Denmark extends CountryHandler protected function hasValidDate(string $tin): bool { - $day = (int) (substr($tin, 0, 2)); - $month = (int) (substr($tin, 2, 2)); - $year = (int) (substr($tin, 4, 2)); + $day = (int) (mb_substr($tin, 0, 2)); + $month = (int) (mb_substr($tin, 2, 2)); + $year = (int) (mb_substr($tin, 4, 2)); $d1 = checkdate($month, $day, 1900 + $year); $d2 = checkdate($month, $day, 2000 + $year); @@ -65,17 +65,13 @@ protected function hasValidDate(string $tin): bool * 1st of January 1990 * 1st of January 1991 * 1st of January 1992 - * - * @param string $tin - * - * @return bool */ protected function hasValidRule(string $tin): bool { - $serialNumber = (int) (substr($tin, 6, 4)); - $dayOfBirth = (int) (substr($tin, 0, 2)); - $monthOfBirth = (int) (substr($tin, 2, 2)); - $yearOfBirth = (int) (substr($tin, 4, 2)); + $serialNumber = (int) (mb_substr($tin, 6, 4)); + $dayOfBirth = (int) (mb_substr($tin, 0, 2)); + $monthOfBirth = (int) (mb_substr($tin, 2, 2)); + $yearOfBirth = (int) (mb_substr($tin, 4, 2)); if (37 <= $yearOfBirth && 57 >= $yearOfBirth && 5000 <= $serialNumber && 8999 >= $serialNumber) { return false; diff --git a/src/CountryHandler/Estonia.php b/src/CountryHandler/Estonia.php index a09487f..05a998e 100644 --- a/src/CountryHandler/Estonia.php +++ b/src/CountryHandler/Estonia.php @@ -26,9 +26,9 @@ final class Estonia extends CountryHandler protected function hasValidDate(string $tin): bool { - $year = (int) (substr($tin, 1, 2)); - $month = (int) (substr($tin, 3, 2)); - $day = (int) (substr($tin, 5, 2)); + $year = (int) (mb_substr($tin, 1, 2)); + $month = (int) (mb_substr($tin, 3, 2)); + $day = (int) (mb_substr($tin, 5, 2)); $d1 = checkdate($month, $day, 1900 + $year); $d2 = checkdate($month, $day, 2000 + $year); @@ -38,7 +38,7 @@ protected function hasValidDate(string $tin): bool protected function hasValidRule(string $tin): bool { - $range = (int) (substr($tin, 7, 3)); + $range = (int) (mb_substr($tin, 7, 3)); if (false === (0 < $range && 711 > $range)) { return false; diff --git a/src/CountryHandler/Finland.php b/src/CountryHandler/Finland.php index acc749e..9cd8b81 100644 --- a/src/CountryHandler/Finland.php +++ b/src/CountryHandler/Finland.php @@ -26,10 +26,10 @@ final class Finland extends CountryHandler protected function hasValidDate(string $tin): bool { - $day = (int) (substr($tin, 0, 2)); - $month = (int) (substr($tin, 2, 2)); - $year = (int) (substr($tin, 4, 2)); - $c7 = substr($tin, 6, 1); + $day = (int) (mb_substr($tin, 0, 2)); + $month = (int) (mb_substr($tin, 2, 2)); + $year = (int) (mb_substr($tin, 4, 2)); + $c7 = mb_substr($tin, 6, 1); if ('+' === $c7) { return checkdate($month, $day, 1800 + $year); @@ -44,7 +44,7 @@ protected function hasValidDate(string $tin): bool protected function hasValidRule(string $tin): bool { - $number = (int) (substr($tin, 0, 6) . substr($tin, 7, 3)); + $number = (int) (mb_substr($tin, 0, 6) . mb_substr($tin, 7, 3)); $remainderBy31 = $number % 31; $c11 = $tin[10]; diff --git a/src/CountryHandler/France.php b/src/CountryHandler/France.php index 4dbc8fd..3683023 100644 --- a/src/CountryHandler/France.php +++ b/src/CountryHandler/France.php @@ -29,13 +29,13 @@ final class France extends CountryHandler */ protected function hasValidRule(string $tin): bool { - $number = (int) (substr($tin, 0, 10)); + $number = (int) (mb_substr($tin, 0, 10)); $remainderBy511 = $number % 511; $checkDigits = 100 > $remainderBy511 ? - 10 > $remainderBy511 ? (int) (substr($tin, 12, 13)) : (int) (substr($tin, 11, 13)) : - (int) (substr($tin, 10, 13)); + 10 > $remainderBy511 ? (int) (mb_substr($tin, 12, 13)) : (int) (mb_substr($tin, 11, 13)) : + (int) (mb_substr($tin, 10, 13)); return $remainderBy511 === $checkDigits; } diff --git a/src/CountryHandler/Germany.php b/src/CountryHandler/Germany.php index ed391e8..c40c647 100644 --- a/src/CountryHandler/Germany.php +++ b/src/CountryHandler/Germany.php @@ -4,8 +4,6 @@ namespace loophp\Tin\CountryHandler; -use function strlen; - /** * Germany. */ @@ -54,10 +52,10 @@ protected function hasValidRule(string $tin): bool private function calculateCheckDigit(string $tin): int { - $chars = (array) str_split($tin); + $chars = (array) mb_str_split($tin); $remainder_mod_eleven = 10; - for ($length = strlen($tin), $counter = 0; $length - 1 > $counter; ++$counter) { + for ($length = mb_strlen($tin), $counter = 0; $length - 1 > $counter; ++$counter) { $digit = (int) ($chars[$counter]); $remainder_mod_ten = ($digit + $remainder_mod_eleven) % 10; @@ -93,7 +91,13 @@ private function isFollowPattern1(string $tin): bool return false; } + /** + * @var array + */ $tab = []; + /** + * @var array + */ $pos = []; for ($i = 0; 10 > $i; ++$i) { @@ -132,7 +136,14 @@ private function isFollowPattern2(string $tin): bool return false; } + /** + * @var array + */ $tab = []; + + /** + * @var array + */ $pos = []; for ($i = 0; 10 > $i; ++$i) { diff --git a/src/CountryHandler/Hungary.php b/src/CountryHandler/Hungary.php index abdcbb1..6c049bf 100644 --- a/src/CountryHandler/Hungary.php +++ b/src/CountryHandler/Hungary.php @@ -30,7 +30,7 @@ protected function hasValidRule(string $tin): bool $sum = 0; for ($i = 0; 9 > $i; ++$i) { - $c11 = (int) (substr($tin, $i, 1)); + $c11 = (int) (mb_substr($tin, $i, 1)); $sum += $c11 * ($i + 1); } $remainderBy11 = $sum % 11; diff --git a/src/CountryHandler/Ireland.php b/src/CountryHandler/Ireland.php index 381b82e..620dff9 100644 --- a/src/CountryHandler/Ireland.php +++ b/src/CountryHandler/Ireland.php @@ -4,8 +4,6 @@ namespace loophp\Tin\CountryHandler; -use function strlen; - /** * Ireland. */ @@ -59,7 +57,7 @@ protected function hasValidRule(string $tin): bool $c5 = $this->digitAt($tin, 4); $c6 = $this->digitAt($tin, 5); $c7 = $this->digitAt($tin, 6); - $c9 = (9 <= strlen($tin)) ? $this->letterToNumber($tin[8]) : 0; + $c9 = (9 <= mb_strlen($tin)) ? $this->letterToNumber($tin[8]) : 0; $c8 = $tin[7]; $sum = $c9 * 9 + $c1 * 8 + $c2 * 7 + $c3 * 6 + $c4 * 5 + $c5 * 4 + $c6 * 3 + $c7 * 2; $remainderBy23 = $sum % 23; diff --git a/src/CountryHandler/Italy.php b/src/CountryHandler/Italy.php index 7ca7d10..cf7e214 100644 --- a/src/CountryHandler/Italy.php +++ b/src/CountryHandler/Italy.php @@ -5,7 +5,6 @@ namespace loophp\Tin\CountryHandler; use function in_array; -use function strlen; use const FILE_IGNORE_NEW_LINES; @@ -45,10 +44,10 @@ final class Italy extends CountryHandler protected function hasValidDate(string $tin): bool { - $day = (int) ($this->convertCharToNumber(substr($tin, 9, 2))); + $day = (int) ($this->convertCharToNumber(mb_substr($tin, 9, 2))); $c9 = $tin[8]; $month = $this->getMonthNumber($c9); - $year = (int) ($this->convertCharToNumber(substr($tin, 6, 2))); + $year = (int) ($this->convertCharToNumber(mb_substr($tin, 6, 2))); if (1 <= $day && 31 >= $day) { $d1 = checkdate($month, $day, 1900 + $year); @@ -69,10 +68,10 @@ protected function hasValidPattern(string $tin): bool $this->listSet = $listSet; } - $code = substr($tin, 11, 1) . $this->convertCharToNumber(substr($tin, 12, 3)); + $code = mb_substr($tin, 11, 1) . $this->convertCharToNumber(mb_substr($tin, 12, 3)); $containsUpper = in_array($code, $this->listSet, true); - $containsLower = in_array(strtolower($code), $this->listSet, true); + $containsLower = in_array(mb_strtolower($code), $this->listSet, true); return ($containsUpper || $containsLower) && parent::hasValidPattern($tin); } @@ -97,7 +96,7 @@ private function convertCharToNumber(string $oldStr): string { $newStr = ''; - for ($i = 0; strlen($oldStr) > $i; ++$i) { + for ($i = 0; mb_strlen($oldStr) > $i; ++$i) { $newStr .= $this->getNumberFromChar($oldStr[$i]); } diff --git a/src/CountryHandler/Latvia.php b/src/CountryHandler/Latvia.php index e24ed34..ef514e6 100644 --- a/src/CountryHandler/Latvia.php +++ b/src/CountryHandler/Latvia.php @@ -19,21 +19,16 @@ final class Latvia extends CountryHandler */ public const LENGTH = 11; - /** - * @param string $tin - * - * @return bool - */ protected function hasValidDate(string $tin): bool { - $c1c2 = substr($tin, 0, 2); + $c1c2 = mb_substr($tin, 0, 2); if ('32' === $c1c2) { return true; } $day = (int) $c1c2; - $month = (int) (substr($tin, 2, 2)); - $year = (int) (substr($tin, 4, 2)); + $month = (int) (mb_substr($tin, 2, 2)); + $year = (int) (mb_substr($tin, 4, 2)); $y1 = checkdate($month, $day, 1900 + $year); $y2 = checkdate($month, $day, 2000 + $year); diff --git a/src/CountryHandler/Lithuania.php b/src/CountryHandler/Lithuania.php index 0658f4c..0d1bfe6 100644 --- a/src/CountryHandler/Lithuania.php +++ b/src/CountryHandler/Lithuania.php @@ -27,10 +27,10 @@ final class Lithuania extends CountryHandler protected function hasValidRule(string $tin): bool { $sum = 0; - $c11 = (int) (substr($tin, 10)); + $c11 = (int) (mb_substr($tin, 10)); for ($i = 0; 10 > $i; ++$i) { - $sum += $this->multiplyAccordingToWeight((int) (substr($tin, $i, 1)), $i); + $sum += $this->multiplyAccordingToWeight((int) (mb_substr($tin, $i, 1)), $i); } $remainderBy11 = $sum % 11; @@ -40,7 +40,7 @@ protected function hasValidRule(string $tin): bool $sum2 = 0; for ($j = 0; 10 > $j; ++$j) { - $sum2 += $this->multiplyAccordingToWeight2((int) (substr($tin, $j, 1)), $j); + $sum2 += $this->multiplyAccordingToWeight2((int) (mb_substr($tin, $j, 1)), $j); } $remainderBy11 = $sum2 % 11; diff --git a/src/CountryHandler/Luxembourg.php b/src/CountryHandler/Luxembourg.php index a7cf655..d9c05de 100644 --- a/src/CountryHandler/Luxembourg.php +++ b/src/CountryHandler/Luxembourg.php @@ -58,9 +58,9 @@ final class Luxembourg extends CountryHandler protected function hasValidDate(string $tin): bool { - $year = (int) (substr($tin, 0, 4)); - $month = (int) (substr($tin, 4, 2)); - $day = (int) (substr($tin, 6, 2)); + $year = (int) (mb_substr($tin, 0, 4)); + $month = (int) (mb_substr($tin, 4, 2)); + $day = (int) (mb_substr($tin, 6, 2)); return checkdate($month, $day, $year); } diff --git a/src/CountryHandler/Poland.php b/src/CountryHandler/Poland.php index 8b83d08..0dbe656 100644 --- a/src/CountryHandler/Poland.php +++ b/src/CountryHandler/Poland.php @@ -36,9 +36,9 @@ final class Poland extends CountryHandler protected function hasValidDateWhenPattern2(string $tin): bool { - $year = (int) (substr($tin, 0, 2)); - $month = (int) (substr($tin, 2, 2)); - $day = (int) (substr($tin, 4, 2)); + $year = (int) (mb_substr($tin, 0, 2)); + $month = (int) (mb_substr($tin, 2, 2)); + $day = (int) (mb_substr($tin, 4, 2)); if (1 <= $month && 12 >= $month) { return checkdate($month, $day, 1900 + $year); diff --git a/src/CountryHandler/Romania.php b/src/CountryHandler/Romania.php index 54c4f46..533adac 100644 --- a/src/CountryHandler/Romania.php +++ b/src/CountryHandler/Romania.php @@ -19,16 +19,11 @@ final class Romania extends CountryHandler */ public const LENGTH = 13; - /** - * @param string $tin - * - * @return bool - */ protected function hasValidDate(string $tin): bool { - $year = (int) (substr($tin, 1, 2)); - $month = (int) (substr($tin, 3, 2)); - $day = (int) (substr($tin, 5, 2)); + $year = (int) (mb_substr($tin, 1, 2)); + $month = (int) (mb_substr($tin, 3, 2)); + $day = (int) (mb_substr($tin, 5, 2)); $y1 = checkdate($month, $day, 1900 + $year); $y2 = checkdate($month, $day, 2000 + $year); @@ -44,7 +39,7 @@ protected function hasValidRule(string $tin): bool return false; } - $county = (int) (substr($tin, 7, 2)); + $county = (int) (mb_substr($tin, 7, 2)); if (47 < $county && 51 !== $county && 52 !== $county) { return false; diff --git a/src/CountryHandler/Slovakia.php b/src/CountryHandler/Slovakia.php index bfc70f5..d480f46 100644 --- a/src/CountryHandler/Slovakia.php +++ b/src/CountryHandler/Slovakia.php @@ -21,7 +21,7 @@ final class Slovakia extends CountryHandler protected function hasValidLength(string $tin): bool { - $c1c2 = substr($tin, 0, 2); + $c1c2 = mb_substr($tin, 0, 2); if (54 > $c1c2) { return $this->matchLength($tin, self::LENGTH - 1); diff --git a/src/CountryHandler/Slovenia.php b/src/CountryHandler/Slovenia.php index 3442f9d..91732c8 100644 --- a/src/CountryHandler/Slovenia.php +++ b/src/CountryHandler/Slovenia.php @@ -31,7 +31,7 @@ protected function hasValidRule(string $tin): bool private function isFollowRangeRule(string $tin): bool { - $intTIN = (int) (substr($tin, 0, 7)); + $intTIN = (int) (mb_substr($tin, 0, 7)); return 999999 < $intTIN && 10000000 > $intTIN; } diff --git a/src/CountryHandler/Spain.php b/src/CountryHandler/Spain.php index caf5c7f..628d895 100644 --- a/src/CountryHandler/Spain.php +++ b/src/CountryHandler/Spain.php @@ -4,8 +4,6 @@ namespace loophp\Tin\CountryHandler; -use function strlen; - use const STR_PAD_LEFT; /** @@ -95,8 +93,8 @@ private function isFollowPattern2(string $tin): bool private function isFollowRule1(string $tin): bool { - $number = (int) (substr($tin, 0, strlen($tin) - 1)); - $checkDigit = $tin[strlen($tin) - 1]; + $number = (int) (mb_substr($tin, 0, mb_strlen($tin) - 1)); + $checkDigit = $tin[mb_strlen($tin) - 1]; $remainderBy23 = $number % 23; $sum = $remainderBy23 + 1; @@ -106,8 +104,8 @@ private function isFollowRule1(string $tin): bool private function isFollowRule2(string $tin): bool { $c1 = (string) $this->getNumberFromChar($tin[0]); - $number = (int) ($c1 . substr($tin, 1, strlen($tin))); - $checkDigit = $tin[strlen($tin) - 1]; + $number = (int) ($c1 . mb_substr($tin, 1, mb_strlen($tin))); + $checkDigit = $tin[mb_strlen($tin) - 1]; $remainderBy23 = $number % 23; $sum = $remainderBy23 + 1; diff --git a/src/CountryHandler/Sweden.php b/src/CountryHandler/Sweden.php index d20224e..3fb7e5e 100644 --- a/src/CountryHandler/Sweden.php +++ b/src/CountryHandler/Sweden.php @@ -4,8 +4,6 @@ namespace loophp\Tin\CountryHandler; -use function strlen; - /** * Sweden. */ @@ -61,44 +59,44 @@ protected function hasValidPattern(string $tin): bool protected function hasValidRule(string $tin): bool { - return ((int) '10' === strlen($tin) + return ((int) '10' === mb_strlen($tin) && $this->isFollowSwedenRule1And2($tin)) - || ((int) '12' === strlen($tin) + || ((int) '12' === mb_strlen($tin) && $this->isFollowSwedenRule3And4($tin)); } private function hasValidDate1(string $tin): bool { - $year = (int) (substr($tin, 0, 2)); - $month = (int) (substr($tin, 2, 2)); - $day = (int) (substr($tin, 4, 2)); + $year = (int) (mb_substr($tin, 0, 2)); + $month = (int) (mb_substr($tin, 2, 2)); + $day = (int) (mb_substr($tin, 4, 2)); return checkdate($month, $day, 1900 + $year) || checkdate($month, $day, 2000 + $year); } private function hasValidDate2(string $tin): bool { - $year = (int) (substr($tin, 0, 2)); - $month = (int) (substr($tin, 2, 2)); - $day = (int) (substr($tin, 4, 2)); + $year = (int) (mb_substr($tin, 0, 2)); + $month = (int) (mb_substr($tin, 2, 2)); + $day = (int) (mb_substr($tin, 4, 2)); return checkdate($month, $day - 60, 1900 + $year) || checkdate($month, $day - 60, 2000 + $year); } private function hasValidDate3(string $tin): bool { - $year = (int) (substr($tin, 0, 4)); - $month = (int) (substr($tin, 4, 2)); - $day = (int) (substr($tin, 6, 2)); + $year = (int) (mb_substr($tin, 0, 4)); + $month = (int) (mb_substr($tin, 4, 2)); + $day = (int) (mb_substr($tin, 6, 2)); return checkdate($month, $day, $year); } private function hasValidDate4(string $tin): bool { - $year = (int) (substr($tin, 0, 4)); - $month = (int) (substr($tin, 4, 2)); - $day = (int) (substr($tin, 6, 2)); + $year = (int) (mb_substr($tin, 0, 4)); + $month = (int) (mb_substr($tin, 4, 2)); + $day = (int) (mb_substr($tin, 6, 2)); return checkdate($month, $day - 60, $year); } diff --git a/src/CountryHandler/UnitedKingdom.php b/src/CountryHandler/UnitedKingdom.php index fded32b..cf110de 100644 --- a/src/CountryHandler/UnitedKingdom.php +++ b/src/CountryHandler/UnitedKingdom.php @@ -4,8 +4,6 @@ namespace loophp\Tin\CountryHandler; -use function strlen; - /** * United Kingdom. */ @@ -40,7 +38,7 @@ public function getTIN(): string { $tin = parent::getTIN(); - if (8 === strlen($tin)) { + if (8 === mb_strlen($tin)) { $tin .= ' '; } @@ -83,7 +81,7 @@ private function isFollowPattern2(string $tin): bool private function isFollowStructureSubRule2(string $tin): bool { - $c1c2 = substr($tin, 0, 2); + $c1c2 = mb_substr($tin, 0, 2); return 'GB' !== $c1c2 && 'NK' !== $c1c2 && 'TN' !== $c1c2 && 'ZZ' !== $c1c2; } diff --git a/src/TIN.php b/src/TIN.php index 5c465b7..2da9c7e 100644 --- a/src/TIN.php +++ b/src/TIN.php @@ -79,10 +79,13 @@ final class TIN */ private $slug; + private function __construct(string $slug) + { + $this->slug = $slug; + } + /** * @throws TINException - * - * @return bool */ public function check(): bool { @@ -91,34 +94,16 @@ public function check(): bool return $this->getAlgorithm($parsedTin['country'], $parsedTin['tin'])->validate(); } - /** - * @param string $countryCode - * @param string $tin - * - * @return TIN - */ public static function from(string $countryCode, string $tin): TIN { return self::fromSlug($countryCode . $tin); } - /** - * @param string $slug - * - * @return TIN - */ public static function fromSlug(string $slug): TIN { - $instance = new self(); - - $instance->slug = $slug; - - return $instance; + return new self($slug); } - /** - * @return bool - */ public function isValid(): bool { try { @@ -131,18 +116,17 @@ public function isValid(): bool } /** - * @param string $country - * @param string|null $tin - * * @throws TINException - * - * @return CountryHandlerInterface */ private function getAlgorithm(string $country, ?string $tin = null): CountryHandlerInterface { foreach ($this->algorithms as $algorithm) { if (true === $algorithm::supports($country)) { - return new $algorithm($tin); + $handler = new $algorithm($tin); + + if ($handler instanceof CountryHandlerInterface) { + return $handler; + } } } @@ -150,8 +134,6 @@ private function getAlgorithm(string $country, ?string $tin = null): CountryHand } /** - * @param string $slug - * * @throws TINException * * @return array