Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from spaze/spaze/phpstan-level-max
Browse files Browse the repository at this point in the history
Get to PHPStan level max + bleedingEdge
  • Loading branch information
spaze authored Sep 7, 2020
2 parents eddd6f8 + ee916a9 commit 299a054
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/composer.lock export-ignore
/phpstan.neon export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,4 @@ This library is licensed under the MIT license. Please see [License file](LICENS

<a name="contributing"></a>
## Contributing
Run PHPUnit tests with `composer test`, see `scripts` in `composer.json`. Tests are also run on GitHub with Actions on each push.
Run PHPUnit tests and static analysis with `composer test`, see `scripts` in `composer.json`. Tests are also run on GitHub with Actions on each push.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
}
],
"require": {
"php": ">=7.3",
"ext-soap": "*"
"php": ">=7.3",
"ext-soap": "*",
"phpstan/phpstan": "^0.12.42"
},
"require-dev": {
"phpunit/php-timer": "1.0.*",
Expand All @@ -29,8 +30,10 @@
},
"minimum-stability": "stable",
"scripts": {
"phpstan-dev": "vendor/bin/phpstan --ansi analyse --configuration phpstan.neon",
"phpunit-dev": "php vendor/phpunit/phpunit/phpunit --colors=always --verbose",
"test": [
"@phpstan-dev",
"@phpunit-dev"
]
}
Expand Down
65 changes: 62 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
paths:
- src
level: max
ignoreErrors:
-
message: '#^Call to an undefined method SoapClient::checkVatApprox\(\)\.$#'
path: src/VatCalculator.php
count: 1

includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
10 changes: 10 additions & 0 deletions src/Exceptions/NoVatRulesForCountryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types = 1);

namespace Spaze\VatCalculator\Exceptions;

use Exception;

class NoVatRulesForCountryException extends Exception
{
}
8 changes: 1 addition & 7 deletions src/VatCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,7 @@ public function getTaxRateForLocation(string $countryCode, ?string $postalCode,
*/
public function isValidVatNumber(string $vatNumber): bool
{
$details = $this->getVatDetails($vatNumber);

if ($details) {
return $details->isValid();
} else {
return false;
}
return $this->getVatDetails($vatNumber)->isValid();
}


Expand Down
28 changes: 22 additions & 6 deletions src/VatRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use DateTimeImmutable;
use DateTimeInterface;
use Spaze\VatCalculator\Exceptions\NoVatRulesForCountryException;

class VatRates
{
Expand All @@ -23,7 +24,7 @@ class VatRates
*
* Taken from: http://ec.europa.eu/taxation_customs/resources/documents/taxation/vat/how_vat_works/rates/vat_rates_en.pdf
*
* @var array<string, array>
* @var array<string, array{rate: float, rates?: array, exceptions?: array, since?: array}>
*/
private $taxRules = [
'AT' => [ // Austria
Expand Down Expand Up @@ -189,7 +190,7 @@ class VatRates
* Non-EU countries with their own VAT requirements, countries in this list
* need to be added manually by `addRateForCountry()` for the rate to be applied.
*
* @var array<string, array>
* @var array<string, array{rate: float, rates?: array, exceptions?: array, since?: array}>
*/
private $optionalTaxRules = [
'CH' => [ // Switzerland
Expand Down Expand Up @@ -368,10 +369,17 @@ public function __construct()
}


/**
* @param string $country
* @throws NoVatRulesForCountryException
*/
public function addRateForCountry(string $country): void
{
$country = strtoupper($country);
$this->taxRules[$country] = $this->optionalTaxRules[$country] ?? null;
if (!isset($this->optionalTaxRules[$country])) {
throw new NoVatRulesForCountryException("No optional tax rules specified for {$country}");
}
$this->taxRules[$country] = $this->optionalTaxRules[$country];
}


Expand Down Expand Up @@ -400,21 +408,29 @@ public function getTaxRateForLocation(string $countryCode, ?string $postalCode,
if (!preg_match($postalCodeException['postalCode'], $postalCode)) {
continue;
}
if (isset($postalCodeException['name'])) {
if (isset($postalCodeException['name'], $this->taxRules[$postalCodeException['code']]['exceptions'])) {
return $this->taxRules[$postalCodeException['code']]['exceptions'][$postalCodeException['name']];
}
return $this->getRules($postalCodeException['code'], $date)['rate'];
}
}

$rules = $this->getRules($countryCode, $date);
if ($type !== VatRates::GENERAL) {
return isset($this->taxRules[$countryCode]['rates'][$type]) ? $this->taxRules[$countryCode]['rates'][$type] : 0;
if (isset($rules['rates'])) {
return $rules['rates'][$type];
}
}

return $this->getRules($countryCode, $date)['rate'];
return $rules['rate'];
}


/**
* @param string $countryCode
* @param DateTimeInterface|null $date
* @return array{rate: float, rates?: array, exceptions?: array, since?: array}
*/
private function getRules(string $countryCode, ?DateTimeInterface $date = null): array
{
if (!isset($this->taxRules[$countryCode])) {
Expand Down
2 changes: 2 additions & 0 deletions tests/VatRatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateTimeImmutable;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
use Spaze\VatCalculator\Exceptions\NoVatRulesForCountryException;

class VatRatesTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -36,6 +37,7 @@ public function testAddRateUnknownCountry(): void
$country = 'yEs';
$this->assertFalse($this->vatRates->shouldCollectVat($country));
$this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country, null));
$this->setExpectedException(NoVatRulesForCountryException::class);
$this->vatRates->addRateForCountry($country);
$this->assertFalse($this->vatRates->shouldCollectVat($country));
$this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country, null));
Expand Down

0 comments on commit 299a054

Please sign in to comment.