From f1d12b96fb86473a784c94b931dfb12208cc777a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:44:46 +0500 Subject: [PATCH 01/12] update .circleci/config.yml --- .circleci/config.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index dd89dfb..880abdc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,40 +10,40 @@ jobs: steps: - checkout - run: php -v - - run: composer install --no-interaction --prefer-source --no-suggest - - run: php vendor/bin/phpunit --colors=always + - run: composer install --no-interaction + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text php72: docker: - image: circleci/php:7.2-cli-node-browsers steps: - checkout - run: php -v - - run: composer install --no-interaction --prefer-source --no-suggest - - run: php vendor/bin/phpunit --colors=always + - run: composer install --no-interaction + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text php73: docker: - image: circleci/php:7.3-cli-node-browsers steps: - checkout - run: php -v - - run: composer install --no-interaction --prefer-source --no-suggest - - run: php vendor/bin/phpunit --colors=always + - run: composer install --no-interaction + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text php74: docker: - image: circleci/php:7.4-cli-node-browsers steps: - checkout - run: php -v - - run: composer install --no-interaction --prefer-source --no-suggest - - run: php vendor/bin/phpunit --colors=always + - run: composer install --no-interaction + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text php80: docker: - image: circleci/php:8.0-cli-node-browsers steps: - checkout - run: php -v - - run: composer install --no-interaction --prefer-source --no-suggest - - run: php vendor/bin/phpunit --colors=always + - run: composer install --no-interaction + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text workflows: version: 2 build: From ac002adb28ad5a13382ec20bafcda1f5d8d83ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:44:54 +0500 Subject: [PATCH 02/12] update .editorconfig --- .editorconfig | 3 --- 1 file changed, 3 deletions(-) diff --git a/.editorconfig b/.editorconfig index 27e2667..91aebb8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,8 +11,5 @@ indent_size = 4 trim_trailing_whitespace = true insert_final_newline = true -[*.md] -trim_trailing_whitespace = false - [*.yml] indent_size = 2 From 36dcea26549b1c2428f15c47a104d08ec51492d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:45:01 +0500 Subject: [PATCH 03/12] update .gitignore --- .gitignore | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 86bfd55..a6bde41 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ -.php_cs.cache -.phpunit.result.cache -composer.lock -coverage.xml -phpbench.json -phpcs.xml -phpunit.xml -vendor/ +/.php_cs.cache +/.phpunit.result.cache +/composer.lock +/coverage.xml +/phpbench.json +/phpcs.xml +/phpunit.xml +/psalm.xml +/vendor/ From 17f4614e2503cfa04347b3d8941151589271397f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:45:09 +0500 Subject: [PATCH 04/12] update .scrutinizer.yml --- .scrutinizer.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d95425d..1e6a96f 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,4 +1,7 @@ build: + environment: + php: + version: '8.0' nodes: analysis: tests: @@ -7,7 +10,7 @@ build: coverage: tests: override: - - command: php vendor/bin/phpunit --coverage-clover coverage.xml + - command: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-clover coverage.xml coverage: file: coverage.xml format: clover From 0f28ea1c2feb50be8e28d2d630f03daf8f525e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:45:16 +0500 Subject: [PATCH 05/12] delete .travis.yml --- .travis.yml | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b0e5e8d..0000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ -language: php - -matrix: - include: - - php: 7.1 - - php: 7.2 - - php: 7.3 - - php: 7.4 - - php: nightly - fast_finish: true - -before_install: - - travis_retry composer self-update - -install: - - travis_retry composer install --no-interaction --prefer-source --no-suggest - -script: php vendor/bin/phpunit --colors=always --coverage-text From c9358910d0534ce820d25e8c0eaff8dd2ad15897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:45:25 +0500 Subject: [PATCH 06/12] update LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 19aa2e3..5786b11 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Sunrise // PHP +Copyright (c) 2018 Anatoly Nekhay Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 0b721c413b9f8c0cd7987ab9912a4f4bfd332a99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:45:31 +0500 Subject: [PATCH 07/12] update README.md --- README.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b77b6f1..9282671 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## Simple VIN decoder for PHP 7.1+ (incl. PHP 8) based on ISO-3779 +## Simple VIN decoder for PHP 7.1+ based on ISO-3779 [![Gitter](https://badges.gitter.im/sunrise-php/support.png)](https://gitter.im/sunrise-php/support) [![Build Status](https://circleci.com/gh/sunrise-php/vin.svg?style=shield)](https://circleci.com/gh/sunrise-php/vin) @@ -26,14 +26,20 @@ try { // It isn't a valid VIN... } -$vin->getVin(); // returns "WVWZZZ1KZ6W612305" -$vin->getWmi(); // returns "WVW" -$vin->getVds(); // returns "ZZZ1KZ" -$vin->getVis(); // returns "6W612305" -$vin->getRegion(); // returns "Europe" -$vin->getCountry(); // returns "Germany" -$vin->getManufacturer(); // returns "Volkswagen" -$vin->getModelYear(); // returns [2006] +$vin->getVin(); // "WVWZZZ1KZ6W612305" +$vin->getWmi(); // "WVW" +$vin->getVds(); // "ZZZ1KZ" +$vin->getVis(); // "6W612305" +$vin->getRegion(); // "Europe" +$vin->getCountry(); // "Germany" +$vin->getManufacturer(); // "Volkswagen" +$vin->getModelYear(); // [2006] + +// convert the VIN to a string +(string) $vin; + +// converts the VIN to array +$vin->toArray(); ``` ## Useful links From 9dcfa80154c9b7ed09ed45f5032784050b48e31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:45:39 +0500 Subject: [PATCH 08/12] update composer.json --- composer.json | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 4dfe8bb..8ba8429 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "sunrise/vin", "homepage": "https://github.com/sunrise-php/vin", - "description": "Simple VIN decoder for PHP 7.1+ based on ISO-3779", + "description": "VIN decoder for PHP 7.1+ based on ISO-3779", "license": "MIT", "keywords": [ "fenric", @@ -15,8 +15,8 @@ "authors": [ { "name": "Anatoly Fenric", - "email": "anatoly@fenric.ru", - "homepage": "https://anatoly.fenric.ru/" + "email": "afenric@gmail.com", + "homepage": "https://github.com/fenric" }, { "name": "李昀陞 (Peter)", @@ -48,8 +48,13 @@ }, "scripts": { "test": [ - "phpunit --colors=always --coverage-text", - "phpcs" + "phpcs", + "psalm", + "XDEBUG_MODE=coverage phpunit --coverage-text --colors=always" + ], + "build": [ + "phpdoc -d src/ -t phpdoc/", + "XDEBUG_MODE=coverage phpunit --coverage-html coverage/" ] } } From 9b3df4b09f9d57e7d5dccd1b8d793a42e9951524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:45:48 +0500 Subject: [PATCH 09/12] update phpunit.xml.dist --- phpunit.xml.dist | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2707d57..6a5dc24 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,12 +1,15 @@ - + ./src - + ./tests/ From 4a3734df8ceb64dda253d080292153856593fa7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:46:08 +0500 Subject: [PATCH 10/12] improve code --- data/manufacturers.php | 2 +- data/regions.php | 5 +- data/years.php | 2 +- src/Vin.php | 103 +++++++++++++++++++++++++++-------------- src/VinInterface.php | 28 ++--------- 5 files changed, 77 insertions(+), 63 deletions(-) diff --git a/data/manufacturers.php b/data/manufacturers.php index ba31ff5..d4b3166 100644 --- a/data/manufacturers.php +++ b/data/manufacturers.php @@ -14,7 +14,7 @@ /** * List of all manufacturers * - * @var array + * @var array * * @link https://en.wikipedia.org/wiki/Vehicle_identification_number#List_of_common_WMI * @link https://en.wikibooks.org/wiki/Vehicle_Identification_Numbers_(VIN_codes)/World_Manufacturer_Identifier_(WMI)#List_of_all_WMIs diff --git a/data/regions.php b/data/regions.php index 7130458..1384690 100644 --- a/data/regions.php +++ b/data/regions.php @@ -14,7 +14,10 @@ /** * List of all regions * - * @var array + * @var array, + * }> * * @link https://en.wikipedia.org/wiki/Vehicle_identification_number#Country_or_Region_codes * @link https://en.wikibooks.org/wiki/Vehicle_Identification_Numbers_(VIN_codes)/World_Manufacturer_Identifier_(WMI)#WMI_Regions diff --git a/data/years.php b/data/years.php index b739770..3af12e2 100644 --- a/data/years.php +++ b/data/years.php @@ -15,7 +15,7 @@ /** * List of all model years * - * @var array + * @var array * * @link https://en.wikipedia.org/wiki/Vehicle_identification_number#Model_year_encoding * @link https://en.wikibooks.org/wiki/Vehicle_Identification_Numbers_(VIN_codes)/Model_year diff --git a/src/Vin.php b/src/Vin.php index 2fdf73a..95d239a 100644 --- a/src/Vin.php +++ b/src/Vin.php @@ -26,6 +26,13 @@ use function strpbrk; use function strtoupper; +/** + * Import constants + */ +use const Sunrise\Vin\MANUFACTURERS; +use const Sunrise\Vin\REGIONS; +use const Sunrise\Vin\YEARS; + /** * Vehicle Identification Number */ @@ -36,11 +43,13 @@ class Vin implements VinInterface * Regular expression for a VIN parsing/validation (ISO 3779) * * @var string + * + * @link https://www.iso.org/standard/52200.html */ public const REGEX = '/^(?[0-9A-HJ-NPR-Z]{3})(?[0-9A-HJ-NPR-Z]{6})(?[0-9A-HJ-NPR-Z]{8})$/'; /** - * The VIN code + * The VIN value * * @var string */ @@ -70,28 +79,28 @@ class Vin implements VinInterface /** * Vehicle region * - * @var null|string + * @var string|null */ private $region; /** * Vehicle country * - * @var null|string + * @var string|null */ private $country; /** * Vehicle manufacturer * - * @var null|string + * @var string|null */ private $manufacturer; /** * Vehicle model year * - * @var int[] + * @var list */ private $modelYear; @@ -100,34 +109,34 @@ class Vin implements VinInterface * * @param string $value * - * @throws InvalidArgumentException If the given string is not a valid VIN. + * @throws InvalidArgumentException + * If the given value isn't a valid VIN. */ public function __construct(string $value) { - // The given VIN must be in upper case... + // VIN must be in uppercase... $value = strtoupper($value); if (!preg_match(self::REGEX, $value, $match)) { - throw new InvalidArgumentException( - sprintf('The value "%s" is not a valid VIN', $value) - ); + throw new InvalidArgumentException(sprintf( + 'The value "%s" is not a valid VIN', + $value + )); } - // Base values $this->vin = $value; $this->wmi = $match['wmi']; $this->vds = $match['vds']; $this->vis = $match['vis']; - // Parsed values - $this->region = $this->determineRegion(); - $this->country = $this->determineCountry(); - $this->manufacturer = $this->determineManufacturer(); - $this->modelYear = $this->determineModelYear(); + $this->region = $this->determineVehicleRegion(); + $this->country = $this->determineVehicleCountry(); + $this->manufacturer = $this->determineVehicleManufacturer(); + $this->modelYear = $this->determineVehicleModelYear(); } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getVin() : string { @@ -135,7 +144,7 @@ public function getVin() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getWmi() : string { @@ -143,7 +152,7 @@ public function getWmi() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getVds() : string { @@ -151,7 +160,7 @@ public function getVds() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getVis() : string { @@ -159,7 +168,7 @@ public function getVis() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getRegion() : ?string { @@ -167,7 +176,7 @@ public function getRegion() : ?string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getCountry() : ?string { @@ -175,7 +184,7 @@ public function getCountry() : ?string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getManufacturer() : ?string { @@ -183,7 +192,7 @@ public function getManufacturer() : ?string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getModelYear() : array { @@ -193,7 +202,16 @@ public function getModelYear() : array /** * Converts the object to array * - * @return array + * @return array{ + * vin: string, + * wmi: string, + * vds: string, + * vis: string, + * region: ?string, + * country: ?string, + * manufacturer: ?string, + * modelYear: list, + * } */ public function toArray() : array { @@ -209,12 +227,22 @@ public function toArray() : array ]; } + /** + * Converts the object to string + * + * @return string + */ + public function __toString() : string + { + return $this->vin; + } + /** * Tries to determine vehicle region * - * @return null|string + * @return string|null */ - private function determineRegion() : ?string + private function determineVehicleRegion() : ?string { return REGIONS[$this->wmi[0]]['region'] ?? null; } @@ -222,17 +250,20 @@ private function determineRegion() : ?string /** * Tries to determine vehicle country * - * @return null|string + * @return string|null */ - private function determineCountry() : ?string + private function determineVehicleCountry() : ?string { $countries = REGIONS[$this->wmi[0]]['countries'] ?? null; - if (null === $countries) { + if ($countries === null) { return null; } foreach ($countries as $chars => $name) { - if (!(false === strpbrk($this->wmi[1], (string) $chars))) { + // there are keys that consist only of numbers... + $chars = (string) $chars; + + if (strpbrk($this->wmi[1], $chars) !== false) { return $name; } } @@ -243,9 +274,9 @@ private function determineCountry() : ?string /** * Tries to determine vehicle manufacturer * - * @return null|string + * @return string|null */ - private function determineManufacturer() : ?string + private function determineVehicleManufacturer() : ?string { return MANUFACTURERS[$this->wmi] ?? MANUFACTURERS[$this->wmi[0] . $this->wmi[1]] ?? null; } @@ -253,11 +284,11 @@ private function determineManufacturer() : ?string /** * Tries to determine vehicle model year(s) * - * @return int[] + * @return list */ - private function determineModelYear() : array + private function determineVehicleModelYear() : array { - $comingYear = (int) date('Y') + 1; + $comingYear = date('Y') + 1; $estimatedYears = []; foreach (YEARS as $year => $char) { diff --git a/src/VinInterface.php b/src/VinInterface.php index 1a9eed2..0f857b5 100644 --- a/src/VinInterface.php +++ b/src/VinInterface.php @@ -24,8 +24,6 @@ interface VinInterface /** * Gets the VIN * - * The length of this string must be 17 characters. - * * @return string */ public function getVin() : string; @@ -33,8 +31,6 @@ public function getVin() : string; /** * Gets WMI (World Manufacturer Identifier) from the VIN * - * The length of this string must be 3 characters. - * * @return string */ public function getWmi() : string; @@ -42,8 +38,6 @@ public function getWmi() : string; /** * Gets VDS (Vehicle Descriptor Section) from the VIN * - * The length of this string must be 6 characters. - * * @return string */ public function getVds() : string; @@ -51,8 +45,6 @@ public function getVds() : string; /** * Gets VIS (Vehicle Identifier Section) from the VIN * - * The length of this string must be 8 characters. - * * @return string */ public function getVis() : string; @@ -60,40 +52,28 @@ public function getVis() : string; /** * Gets a region from the VIN * - * The region must be determined by the first character of the VIN. - * - * @return null|string + * @return string|null */ public function getRegion() : ?string; /** * Gets a country from the VIN * - * The country must be determined by the second character of the VIN. - * - * @return null|string + * @return string|null */ public function getCountry() : ?string; /** * Gets a manufacturer from the VIN * - * The manufacturer must be determined by the first 2 or 3 characters of the VIN. - * - * @return null|string + * @return string|null */ public function getManufacturer() : ?string; /** * Gets a model year from the VIN * - * The model year must be determined by the tenth character of the VIN. - * - * NOTE! The model year may not be determined correctly. - * - * @return int[] - * - * @since 1.0.13 + * @return list */ public function getModelYear() : array; } From 07fa705d9cae62d5d7c0e95d92064f30a8991ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:46:17 +0500 Subject: [PATCH 11/12] improve tests --- tests/VinTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/VinTest.php b/tests/VinTest.php index 7ed4af7..958abec 100644 --- a/tests/VinTest.php +++ b/tests/VinTest.php @@ -280,4 +280,14 @@ public function testFuturityModelYear() : void $this->assertSame([2000], $vin->getModelYear()); } + + /** + * @return void + */ + public function testToString() : void + { + $vin = new Vin(self::TEST_VIN_WITH_FUTURITY_MODEL_YEAR); + + $this->assertSame(self::TEST_VIN_WITH_FUTURITY_MODEL_YEAR, (string) $vin); + } } From 8920a56fef5722dbfeca4c9578edd346e4c56edb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 21:46:24 +0500 Subject: [PATCH 12/12] add psalm.xml.dist --- psalm.xml.dist | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 psalm.xml.dist diff --git a/psalm.xml.dist b/psalm.xml.dist new file mode 100644 index 0000000..38cce4c --- /dev/null +++ b/psalm.xml.dist @@ -0,0 +1,14 @@ + + + + + + + + +