Skip to content

Commit

Permalink
Enable Psalm, Infection and Insights reports.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 25, 2020
1 parent dab7433 commit bf5cdfc
Show file tree
Hide file tree
Showing 32 changed files with 159 additions and 227 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github: drupol
custom: ["https://www.paypal.me/drupol"]
24 changes: 21 additions & 3 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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') }}
Expand All @@ -42,16 +48,28 @@ 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 }}

- name: Send PSALM data
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 1 addition & 5 deletions grumphp.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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

15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
5 changes: 5 additions & 0 deletions spec/loophp/Tin/TINSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public function it_is_initializable()
{
$this->shouldHaveType(TIN::class);
}

public function let()
{
$this->beConstructedThrough('fromSlug', ['foo123']);
}
}
21 changes: 8 additions & 13 deletions src/CountryHandler/Belgium.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 3 additions & 8 deletions src/CountryHandler/Bulgaria.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 7 additions & 56 deletions src/CountryHandler/CountryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use loophp\Tin\Exception\TINException;

use function strlen;

/**
* Base handler class.
*/
Expand All @@ -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 '';
Expand All @@ -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
Expand Down Expand Up @@ -74,8 +67,6 @@ final public function validate(): bool
/**
* @param string $tin
* The TIN.
*
* @return CountryHandlerInterface
*/
final public function withTIN(string $tin): CountryHandlerInterface
{
Expand All @@ -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;
},
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions src/CountryHandler/CountryHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 0 additions & 5 deletions src/CountryHandler/Cyprus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit bf5cdfc

Please sign in to comment.