diff --git a/.github/workflows/format_php.yml b/.github/workflows/format_php.yml index 6b91144..0ef2144 100644 --- a/.github/workflows/format_php.yml +++ b/.github/workflows/format_php.yml @@ -13,9 +13,6 @@ jobs: with: ref: ${{ github.head_ref }} - - name: Install - run: composer install - - name: Run php-cs-fixer uses: docker://oskarstark/php-cs-fixer-ga diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e6cd19a..de969b8 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,7 +9,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: [7.4, 8.0, 8.1, 8.2] + php: [8.2] stability: [prefer-lowest, prefer-stable] name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/README.md b/README.md index 74bc1ee..6a6033b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Using Javascript? Check out [@stefanzweifel/js-swiss-cantons](https://github.com ## Installation -The easiest way to install the package is by using composer. The package requires PHP 7.4. +The easiest way to install the package is by using composer. The package requires PHP 8.2. ```shell composer require "wnx/php-swiss-cantons" diff --git a/composer.json b/composer.json index b4cf0e0..e231e29 100644 --- a/composer.json +++ b/composer.json @@ -17,15 +17,15 @@ } ], "require": { - "php": ">=7.4.0 || ^8.0", + "php": "^8.2", "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^9 | ^10", + "phpunit/phpunit": "^10.1", "friendsofphp/php-cs-fixer": "^3", "vimeo/psalm": "^5.0", "league/csv": "^9.7", - "symfony/console": "^5.0 | ^6.0", + "symfony/console": "^6.0", "rector/rector": "^0.15.24" }, "autoload": { diff --git a/rector.php b/rector.php index 4ed828c..34a1608 100644 --- a/rector.php +++ b/rector.php @@ -4,6 +4,7 @@ use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; +use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Set\ValueObject\LevelSetList; return static function (RectorConfig $rectorConfig): void { @@ -17,6 +18,7 @@ // define sets of rules $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_74 - ]); + LevelSetList::UP_TO_PHP_82, + PHPUnitSetList::PHPUNIT_100, + ]); }; diff --git a/src/Canton.php b/src/Canton.php index 7e48e22..8413da7 100644 --- a/src/Canton.php +++ b/src/Canton.php @@ -6,17 +6,17 @@ class Canton { - public const LANG_GERMAN = 'de'; + final public const LANG_GERMAN = 'de'; - public const LANG_FRENCH = 'fr'; + final public const LANG_FRENCH = 'fr'; - public const LANG_ITALIAN = 'it'; + final public const LANG_ITALIAN = 'it'; - public const LANG_ENGLISH = 'en'; + final public const LANG_ENGLISH = 'en'; - public const LANG_ROMANSH = 'rm'; + final public const LANG_ROMANSH = 'rm'; - public const AVAILABLE_LANGUAGES = [ + final public const AVAILABLE_LANGUAGES = [ self::LANG_GERMAN, self::LANG_FRENCH, self::LANG_ITALIAN, diff --git a/src/CantonSearch.php b/src/CantonSearch.php index 41284c2..2d8601a 100644 --- a/src/CantonSearch.php +++ b/src/CantonSearch.php @@ -13,9 +13,7 @@ public function __construct() public function findByAbbreviation(string $abbreviation): ?Canton { - $result = array_filter($this->dataSet, function (Canton $canton) use ($abbreviation) { - return $canton->getAbbreviation() === strtoupper($abbreviation); - }); + $result = array_filter($this->dataSet, fn (Canton $canton) => $canton->getAbbreviation() === strtoupper($abbreviation)); if (count($result) === 0) { return null; @@ -26,9 +24,7 @@ public function findByAbbreviation(string $abbreviation): ?Canton public function findByName(string $name): ?Canton { - $result = array_filter($this->dataSet, function (Canton $canton) use ($name) { - return in_array($name, $canton->getNamesArray()); - }); + $result = array_filter($this->dataSet, fn (Canton $canton) => in_array($name, $canton->getNamesArray())); if (count($result) === 0) { return null; diff --git a/src/Cantons.php b/src/Cantons.php index abe2db1..ebe5293 100644 --- a/src/Cantons.php +++ b/src/Cantons.php @@ -8,7 +8,7 @@ class Cantons public function __construct() { - $this->cantons = json_decode(file_get_contents(__DIR__.'/data/cantons.json'), true); + $this->cantons = json_decode(file_get_contents(__DIR__.'/data/cantons.json'), true, 512, JSON_THROW_ON_ERROR); } /** @@ -18,9 +18,7 @@ public function __construct() */ public function getAll(): array { - return array_map(function ($canton) { - return new Canton($canton); - }, $this->cantons); + return array_map(fn ($canton) => new Canton($canton), $this->cantons); } /** diff --git a/src/Console/UpdateZipcodeDatasetCommand.php b/src/Console/UpdateZipcodeDatasetCommand.php index d7b04d4..f7e59fd 100644 --- a/src/Console/UpdateZipcodeDatasetCommand.php +++ b/src/Console/UpdateZipcodeDatasetCommand.php @@ -13,8 +13,8 @@ class UpdateZipcodeDatasetCommand extends Command { - public const PATH_TO_CSV = __DIR__ . '/../data/zipcodes.csv'; - public const PATH_TO_JSON = __DIR__ . '/../data/zipcodes.json'; + final public const PATH_TO_CSV = __DIR__ . '/../data/zipcodes.csv'; + final public const PATH_TO_JSON = __DIR__ . '/../data/zipcodes.json'; protected function configure(): void { diff --git a/src/ZipcodeSearch.php b/src/ZipcodeSearch.php index a482d04..ed750b8 100644 --- a/src/ZipcodeSearch.php +++ b/src/ZipcodeSearch.php @@ -16,9 +16,7 @@ public function __construct() */ public function findbyZipcode(int $zipcode): ?array { - $result = array_filter($this->dataSet, function (array $city) use ($zipcode) { - return $city['zipcode'] === intval($zipcode); - }); + $result = array_filter($this->dataSet, fn (array $city) => $city['zipcode'] === intval($zipcode)); if (count($result) === 0) { return null; @@ -29,6 +27,6 @@ public function findbyZipcode(int $zipcode): ?array public function getDataSet(): array { - return json_decode(file_get_contents(__DIR__.'/data/zipcodes.json'), true); + return json_decode(file_get_contents(__DIR__.'/data/zipcodes.json'), true, 512, JSON_THROW_ON_ERROR); } } diff --git a/tests/CantonManagerTest.php b/tests/CantonManagerTest.php index 10a72e8..a7bb825 100644 --- a/tests/CantonManagerTest.php +++ b/tests/CantonManagerTest.php @@ -2,13 +2,14 @@ namespace Wnx\SwissCantons\Tests; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Wnx\SwissCantons\CantonManager; use Wnx\SwissCantons\Exceptions\CantonException; class CantonManagerTest extends TestCase { - /** @test */ + #[Test] public function it_returns_correct_canton_instance_for_abbreviation() { $canton = new CantonManager(); @@ -20,7 +21,7 @@ public function it_returns_correct_canton_instance_for_abbreviation() ); } - /** @test */ + #[Test] public function it_returns_correct_canton_if_abbreviation_is_lowercase() { $cantonManager = new CantonManager(); @@ -44,7 +45,7 @@ public function it_returns_correct_canton_if_abbreviation_is_lowercase() ); } - /** @test */ + #[Test] public function it_throws_exception_if_no_canton_for_abbreviation_is_found() { $this->expectException(CantonException::class); @@ -53,7 +54,7 @@ public function it_throws_exception_if_no_canton_for_abbreviation_is_found() $result = $canton->getByAbbreviation('FOO'); } - /** @test */ + #[Test] public function it_returns_correct_canton_instance_for_name() { $canton = new CantonManager(); @@ -62,7 +63,7 @@ public function it_returns_correct_canton_instance_for_name() $this->assertEquals('ZH', $result->getAbbreviation()); } - /** @test */ + #[Test] public function it_throws_exception_if_not_canton_for_name_is_found() { $this->expectException(CantonException::class); @@ -71,7 +72,7 @@ public function it_throws_exception_if_not_canton_for_name_is_found() $result = $canton->getByName('FOO'); } - /** @test */ + #[Test] public function it_returns_canton_for_zipcode() { $canton = new CantonManager(); @@ -82,7 +83,7 @@ public function it_returns_canton_for_zipcode() $this->assertEquals('Berne', $result->setLanguage('en')->getName()); } - /** @test */ + #[Test] public function it_throws_exception_if_no_canton_for_zipcode_could_be_found() { $this->expectException(CantonException::class); @@ -91,7 +92,7 @@ public function it_throws_exception_if_no_canton_for_zipcode_could_be_found() $result = $canton->getByZipcode(9999); } - /** @test */ + #[Test] public function it_throws_exception_if_lichtenstein_zipcode_is_searched_for() { $this->expectException(CantonException::class); diff --git a/tests/CantonSearchTest.php b/tests/CantonSearchTest.php index 79d13a3..b09476d 100644 --- a/tests/CantonSearchTest.php +++ b/tests/CantonSearchTest.php @@ -2,13 +2,14 @@ namespace Wnx\SwissCantons\Tests; +use PHPUnit\Framework\Attributes\Test; use Wnx\SwissCantons\Canton; use PHPUnit\Framework\TestCase; use Wnx\SwissCantons\CantonSearch; class CantonSearchTest extends TestCase { - /** @test */ + #[Test] public function it_finds_canton_by_abbreviation() { $cantonSearch = new CantonSearch(); @@ -18,7 +19,7 @@ public function it_finds_canton_by_abbreviation() $this->assertEquals('SH', $canton->getAbbreviation()); } - /** @test */ + #[Test] public function it_returns_null_if_no_canton_for_abbreviation_was_found() { $cantonSearch = new CantonSearch(); @@ -27,7 +28,7 @@ public function it_returns_null_if_no_canton_for_abbreviation_was_found() $this->assertNull($canton); } - /** @test */ + #[Test] public function it_finds_canton_by_name() { $cantonSearch = new CantonSearch(); @@ -38,7 +39,7 @@ public function it_finds_canton_by_name() $this->assertEquals('Zürich', $canton->getNamesArray()['de']); } - /** @test */ + #[Test] public function it_returns_null_if_no_canton_for_name_was_found() { $cantonSearch = new CantonSearch(); diff --git a/tests/CantonTest.php b/tests/CantonTest.php index abe5196..c68d167 100644 --- a/tests/CantonTest.php +++ b/tests/CantonTest.php @@ -2,6 +2,7 @@ namespace Wnx\SwissCantons\Tests; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Wnx\SwissCantons\Canton; use Wnx\SwissCantons\Exceptions\InvalidLanguageException; @@ -22,7 +23,7 @@ protected function getExampleCanton(): array ]; } - /** @test */ + #[Test] public function it_sets_language() { $canton = new Canton($this->getExampleCanton()); @@ -31,7 +32,7 @@ public function it_sets_language() $this->assertEquals('fr', $canton->getLanguage()); } - /** @test */ + #[Test] public function it_transformers_uppercase_language_string_to_lowercase() { $canton = new Canton($this->getExampleCanton()); @@ -40,7 +41,7 @@ public function it_transformers_uppercase_language_string_to_lowercase() $this->assertEquals('de', $canton->getLanguage()); } - /** @test */ + #[Test] public function it_only_allows_national_languages() { $this->expectException(InvalidLanguageException::class); @@ -49,7 +50,7 @@ public function it_only_allows_national_languages() $canton->setLanguage('es'); } - /** @test */ + #[Test] public function it_sets_and_returns_abbreviation() { $canton = new Canton($this->getExampleCanton()); @@ -57,7 +58,7 @@ public function it_sets_and_returns_abbreviation() $this->assertEquals('ZH', $canton->getAbbreviation()); } - /** @test */ + #[Test] public function it_sets_names_array() { $canton = new Canton($this->getExampleCanton()); @@ -68,7 +69,7 @@ public function it_sets_names_array() ); } - /** @test */ + #[Test] public function it_returns_correct_name_for_given_language() { $canton = new Canton($this->getExampleCanton()); diff --git a/tests/CantonsTest.php b/tests/CantonsTest.php index b5358e7..96016e5 100644 --- a/tests/CantonsTest.php +++ b/tests/CantonsTest.php @@ -3,12 +3,13 @@ namespace Wnx\SwissCantons\Tests; use Exception; +use PHPUnit\Framework\Attributes\Test; use Wnx\SwissCantons\Cantons; use PHPUnit\Framework\TestCase; class CantonsTest extends TestCase { - /** @test */ + #[Test] public function it_returns_json_source_as_array() { $cantons = new Cantons(); @@ -16,7 +17,7 @@ public function it_returns_json_source_as_array() $this->assertTrue(is_array($cantons->getAll())); } - /** @test */ + #[Test] public function it_contains_abbreviation_and_name() { $cantons = (new Cantons())->getAll(); @@ -29,7 +30,7 @@ public function it_contains_abbreviation_and_name() $this->assertArrayHasKey('en', $cantons[0]->getNamesArray()); } - /** @test */ + #[Test] public function it_returns_an_array_with_abbreviation_as_key_and_name_as_value() { $cantons = (new Cantons())->getAllAsArray(); @@ -41,7 +42,7 @@ public function it_returns_an_array_with_abbreviation_as_key_and_name_as_value() $this->assertEquals('Zurich', $cantons['ZH']); } - /** @test */ + #[Test] public function it_returns_an_array_with_abbreviation_and_name_but_in_a_different_language() { $cantons = (new Cantons())->getAllAsArray('de'); @@ -53,7 +54,7 @@ public function it_returns_an_array_with_abbreviation_and_name_but_in_a_differen $this->assertEquals('Zürich', $cantons['ZH']); } - /** @test */ + #[Test] public function it_throws_an_exception_if_passed_langauge_is_not_available() { $this->expectException(Exception::class); diff --git a/tests/ZipcodeSearchTest.php b/tests/ZipcodeSearchTest.php index f974153..0413e6d 100644 --- a/tests/ZipcodeSearchTest.php +++ b/tests/ZipcodeSearchTest.php @@ -2,12 +2,13 @@ namespace Wnx\SwissCantons\Tests; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Wnx\SwissCantons\ZipcodeSearch; class ZipcodeSearchTest extends TestCase { - /** @test */ + #[Test] public function it_returns_dataset_as_array() { $cantonSearch = new ZipcodeSearch(); @@ -15,7 +16,7 @@ public function it_returns_dataset_as_array() $this->assertIsArray($cantonSearch->getDataSet()); } - /** @test */ + #[Test] public function it_finds_canton_by_zipcode() { $zipcodeSearch = new ZipcodeSearch(); @@ -27,7 +28,7 @@ public function it_finds_canton_by_zipcode() $this->assertEquals('Bern', $result['city']); } - /** @test */ + #[Test] public function it_does_not_find_result_for_not_available_zipcode() { $zipcodeSearch = new ZipcodeSearch(); @@ -37,7 +38,7 @@ public function it_does_not_find_result_for_not_available_zipcode() $this->assertEquals(null, $result); } - /** @test */ + #[Test] public function it_does_not_find_liechtenstein_zipcodes() { $zipcodeSearch = new ZipcodeSearch();