diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 57f081a..3e0457d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,8 @@ name: Build -on: [ push, pull_request ] - +on: + - pull_request + - push jobs: tests: runs-on: "ubuntu-latest" @@ -12,7 +13,7 @@ jobs: - "8.3" steps: - name: "Checkout" - uses: "actions/checkout@v2" + uses: "actions/checkout@v4" - name: "Install PHP" uses: "shivammathur/setup-php@v2" @@ -21,11 +22,10 @@ jobs: tools: "cs2pr" - name: "Install dependencies with Composer" - uses: "ramsey/composer-install@v2" + uses: "ramsey/composer-install@v3" - - name: "Run phpcs" - if: ${{ matrix.php-version == '8.3' }} - run: "vendor/bin/phpcs -n -s --report=checkstyle | cs2pr" + - name: "Run PHP CS Fixer" + run: "./vendor/bin/php-cs-fixer fix --format checkstyle | cs2pr" - name: "Run PHPUnit" run: "vendor/bin/phpunit" diff --git a/.gitignore b/.gitignore index 8e26f45..ec2000c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ vendor composer.lock .idea .phpunit.result.cache +.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..1a07c4d --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,12 @@ +in(['src', 'tests']) +; + +return (new PhpCsFixer\Config()) + ->setRules([ + '@PSR12' => true, + ]) + ->setFinder($finder) + ; diff --git a/README.md b/README.md index 0b5f745..7809716 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ This is a simple PHP library to help you deal with Europe's VAT rules. ## Installation -[PHP](https://php.net) version 8.1 or higher with the CURL and JSON extension is required. +[PHP](https://php.net) version 8.2 or higher with the CURL and JSON extension is required. For VAT number existence checking, the PHP SOAP extension is required as well. diff --git a/composer.json b/composer.json index 213ac88..0fe3f11 100755 --- a/composer.json +++ b/composer.json @@ -10,13 +10,13 @@ } ], "require": { - "php": ">=8.1", + "php": ">=8.2", "ext-curl": "*", "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^9.5", - "squizlabs/php_codesniffer": "^3.5" + "phpunit/phpunit": "^11.1", + "friendsofphp/php-cs-fixer": "^3.54" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 812ee72..fe907ce 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,31 +1,16 @@ - - - - ./tests - - - - - ./src - - - - - + + + + ./tests + + + + + + + + ./src + + diff --git a/src/Rates.php b/src/Rates.php index 533c7b2..cd7ae67 100644 --- a/src/Rates.php +++ b/src/Rates.php @@ -71,7 +71,7 @@ private function loadFromFile(): void throw new Exception("Unserializable file content"); } - $data = unserialize($contents, [ + $data = @unserialize($contents, [ 'allowed_classes' => [ Period::class, DateTimeImmutable::class @@ -153,7 +153,6 @@ public function getRateForCountry(string $countryCode, string $level = self::RAT */ public function getRateForCountryOnDate(string $countryCode, \DateTimeInterface $datetime, string $level = self::RATE_STANDARD): float { - $activePeriod = $this->resolvePeriod($countryCode, $datetime); - return $activePeriod->getRate($level); + return $this->resolvePeriod($countryCode, $datetime)->getRate($level); } } diff --git a/src/Vies/Client.php b/src/Vies/Client.php index 67134aa..2b9ae44 100644 --- a/src/Vies/Client.php +++ b/src/Vies/Client.php @@ -43,6 +43,19 @@ public function __construct(int $timeout = 10) * @throws ViesException */ public function checkVat(string $countryCode, string $vatNumber): bool + { + return (bool)$this->getInfo($countryCode, $vatNumber)->valid; + } + + /** + * @param string $countryCode + * @param string $vatNumber + * + * @return object + * + * @throws ViesException + */ + public function getInfo(string $countryCode, string $vatNumber): object { try { $response = $this->getClient()->checkVat( @@ -55,7 +68,7 @@ public function checkVat(string $countryCode, string $vatNumber): bool throw new ViesException($e->getMessage(), $e->getCode()); } - return (bool) $response->valid; + return $response; } /** diff --git a/tests/Clients/ClientsTest.php b/tests/Clients/ClientsTest.php index e3d2055..0f87972 100644 --- a/tests/Clients/ClientsTest.php +++ b/tests/Clients/ClientsTest.php @@ -2,18 +2,21 @@ namespace Ibericode\Vat\Tests\Clients; +use Ibericode\Vat\Clients\ClientException; use Ibericode\Vat\Clients\IbericodeVatRatesClient; use Ibericode\Vat\Clients\Client; use Ibericode\Vat\Period; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ClientsTest extends TestCase { /** * @group remote-http - * @dataProvider clientProvider + * @throws ClientException */ - public function testClient(Client $client) + #[DataProvider('clientsProvider')] + public function testClient(Client $client): void { $data = $client->fetch(); $this->assertIsArray($data); @@ -22,7 +25,7 @@ public function testClient(Client $client) $this->assertInstanceOf(Period::class, $data['NL'][0]); } - public function clientProvider() + public static function clientsProvider(): \Generator { yield [new IbericodeVatRatesClient()]; } diff --git a/tests/CountriesTest.php b/tests/CountriesTest.php index 85381bc..d97d13e 100644 --- a/tests/CountriesTest.php +++ b/tests/CountriesTest.php @@ -9,14 +9,14 @@ class CountriesTest extends TestCase { - public function testIterator() + public function testIterator(): void { $countries = new Countries(); $this->assertCount(249, $countries); } - public function testArrayAccess() + public function testArrayAccess(): void { $countries = new Countries(); @@ -27,35 +27,35 @@ public function testArrayAccess() $countries['FOO']; } - public function testArrayAccessWithInvalidCountryCode() + public function testArrayAccessWithInvalidCountryCode(): void { $countries = new Countries(); $this->expectException(Exception::class); $countries['FOO']; } - public function testArrayAccessSetValue() + public function testArrayAccessSetValue(): void { $countries = new Countries(); $this->expectException(Exception::class); $countries['FOO'] = 'bar'; } - public function testArrayAccessUnsetValue() + public function testArrayAccessUnsetValue(): void { $countries = new Countries(); $this->expectException(Exception::class); unset($countries['FOO']); } - public function testHasCode() + public function testHasCode(): void { $countries = new Countries(); $this->assertFalse($countries->hasCountryCode('FOO')); $this->assertTrue($countries->hasCountryCode('NL')); } - public function testIsCodeInEU() + public function testIsCodeInEU(): void { $countries = new Countries(); $this->assertFalse($countries->isCountryCodeInEU('FOO')); diff --git a/tests/GeolocatorTest.php b/tests/GeolocatorTest.php index 9f2c2ba..137ffdc 100644 --- a/tests/GeolocatorTest.php +++ b/tests/GeolocatorTest.php @@ -3,22 +3,23 @@ namespace Ibericode\Vat\Tests; use Ibericode\Vat\Geolocator; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class GeolocatorTest extends TestCase { /** - * @dataProvider provider * @group remote-http */ - public function testClient($service) + #[DataProvider('servicesProvider')] + public function testService($service): void { $geolocator = new Geolocator($service); $country = $geolocator->locateIpAddress('8.8.8.8'); $this->assertEquals('US', $country); } - public function provider() + public static function servicesProvider(): \Generator { yield ['ip2c.org']; } diff --git a/tests/RatesTest.php b/tests/RatesTest.php index 12e865b..0bb596f 100644 --- a/tests/RatesTest.php +++ b/tests/RatesTest.php @@ -19,7 +19,7 @@ protected function setUp(): void } } - private function getRatesClientMock() + private function getRatesClientMock(): \PHPUnit\Framework\MockObject\MockObject { $client = $this->getMockBuilder(IbericodeVatRatesClient::class) ->getMock(); diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 847bc49..64d78f8 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -3,6 +3,7 @@ namespace Ibericode\Vat\Tests; use Ibericode\Vat\Validator; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -12,9 +13,9 @@ class ValidatorTest extends TestCase { /** - * @covers Validator::validateVatNumberFormat + * @coversXXX Validator::validateVatNumberFormat */ - public function testValidateVatNumberFormat() + public function testValidateVatNumberFormat(): void { $valid = [ 'ATU12345678', @@ -118,16 +119,14 @@ public function testValidateVatNumberFormat() } } - /** - * @dataProvider validIpAddresses - */ - public function testValidateIpAddressWithValid($value) + #[DataProvider('validIpAddresses')] + public function testValidateIpAddressWithValid($value): void { $validator = new Validator(); $this->assertTrue($validator->validateIpAddress($value)); } - public function validIpAddresses() + public static function validIpAddresses(): array { return [ ['8.8.8.8'], @@ -135,16 +134,14 @@ public function validIpAddresses() ]; } - /** - * @dataProvider invalidIpAddresses - */ - public function testValidateIpAddressWithInvalidValues($value) + #[DataProvider('invalidIpAddresses')] + public function testValidateIpAddressWithInvalidValues($value): void { $validator = new Validator(); $this->assertFalse($validator->validateIpAddress($value)); } - public function invalidIpAddresses() + public static function invalidIpAddresses(): array { return [ ['0.8.8.8.8'], @@ -153,18 +150,14 @@ public function invalidIpAddresses() ]; } - /** - * @dataProvider validCountryCodes - */ - public function testValidateCountryCodeWithValidValues($value) + #[DataProvider('validCountryCodes')] + public function testValidateCountryCodeWithValidValues($value): void { $validator = new Validator(); $this->assertTrue($validator->validateCountryCode($value)); } - /** - * @dataProvider invalidCountryCodes - */ + #[DataProvider('invalidCountryCodes')] public function testValidateCountryCodeWithInvalidValues($value) { $validator = new Validator(); @@ -172,7 +165,7 @@ public function testValidateCountryCodeWithInvalidValues($value) } - public function validCountryCodes() + public static function validCountryCodes(): array { return [ ['NL'], @@ -182,7 +175,7 @@ public function validCountryCodes() ]; } - public function invalidCountryCodes() + public static function invalidCountryCodes(): array { return [ ['FOO'],