From a1e9534c49ff7f3bced6d5d17c6a075309858fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20H=C3=A9lias?= Date: Wed, 29 Nov 2023 17:10:36 +0100 Subject: [PATCH] Drop PHP7.4 & rework CI --- .github/workflows/static.yml | 2 +- .github/workflows/tests.yml | 31 +++++++++++++------ .gitignore | 1 - .php_cs.dist.php => .php-cs-fixer.php | 0 CHANGELOG.md | 2 ++ composer.json | 2 +- phpstan.neon.dist => phpstan.neon | 0 phpunit.xml | 31 +++++++++++++++++++ phpunit.xml.dist | 20 ------------ src/Doctrine/DBAL/Types/PhoneNumberType.php | 4 +-- .../PhoneNumberToArrayTransformer.php | 16 +++++----- .../PhoneNumberToStringTransformer.php | 20 ++++++------ .../Normalizer/PhoneNumberNormalizer.php | 2 +- src/Templating/Helper/PhoneNumberHelper.php | 21 ++----------- .../Constraints/PhoneNumberValidator.php | 13 +++++--- .../Helper/PhoneNumberHelperTest.php | 9 ------ 16 files changed, 88 insertions(+), 86 deletions(-) rename .php_cs.dist.php => .php-cs-fixer.php (100%) rename phpstan.neon.dist => phpstan.neon (100%) create mode 100644 phpunit.xml delete mode 100644 phpunit.xml.dist diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 7899eb99..2b7545a3 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -44,4 +44,4 @@ jobs: composer-options: "--working-dir=tools/php-cs-fixer" - name: PHP-CS-Fixer - run: tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff --config=.php_cs.dist.php + run: tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff --config=.php-cs-fixer.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 43012a4d..1401b1bc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,14 +13,24 @@ jobs: strategy: matrix: include: - - php: '8.0' + # Lowest Deps + - php: 8.0 symfony-require: 5.4.* - - php: '8.0' - symfony-require: 6.3.* - - php: '8.2' + composer-flags: '--prefer-stable --prefer-lowest' + # LTS with latest stable PHP + - php: 8.2 + symfony-require: 6.4.* + composer-flags: '--prefer-stable' + # Active release + - php: 8.3 symfony-require: 7.0.* + composer-flags: '--prefer-stable --ignore-platform-req=php+' + # Development release + - php: nightly + symfony-require: 7.1.*@dev + composer-flags: '--ignore-platform-req=php+' stability: dev - - php: '8.1' + can-fail: true fail-fast: false steps: @@ -30,7 +40,8 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: "${{ matrix.php }}" + php-version: ${{ matrix.php }} + extensions: intl-74.1 coverage: none - name: Configure Composer minimum stability @@ -39,8 +50,8 @@ jobs: - name: Install dependencies env: - SYMFONY_REQUIRE: "${{ matrix.symfony-require }}" - run: composer update --prefer-dist + SYMFONY_REQUIRE: ${{ matrix.symfony-require }} + run: composer update ${{ matrix.composer-flags }} --no-interaction --no-progress --optimize-autoloader - - name: "Run PHPUnit" - run: "vendor/bin/phpunit" + - name: Run PHPUnit + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index 68815a47..1ed5aeb9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ /.phpunit.result.cache /composer.lock /composer.phar -/phpunit.xml /tools/*/composer.lock /tools/*/vendor /vendor/ diff --git a/.php_cs.dist.php b/.php-cs-fixer.php similarity index 100% rename from .php_cs.dist.php rename to .php-cs-fixer.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d494c03..0cc1d801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changed + - Remove support of Symfony 4.x, 5.3 & < 6.3 - Add support for Symfony 7 +- Remove support of PHP 7.4 ## [3.9.3] - 2023-11-29 diff --git a/composer.json b/composer.json index 18704a78..575acdaf 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "sort-packages": true }, "require": { - "php": ">=7.4", + "php": ">=8.0", "giggsey/libphonenumber-for-php": "^8.0", "symfony/framework-bundle": "^5.4 || ^6.3 || ^7.0", "symfony/intl": "^5.4 || ^6.3 || ^7.0", diff --git a/phpstan.neon.dist b/phpstan.neon similarity index 100% rename from phpstan.neon.dist rename to phpstan.neon diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..1fbcc1eb --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + tests + + + + + + src + + + + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 3f252c42..00000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - tests - - - - - src - - - - - - diff --git a/src/Doctrine/DBAL/Types/PhoneNumberType.php b/src/Doctrine/DBAL/Types/PhoneNumberType.php index 0a7e42f2..0dfb6a36 100644 --- a/src/Doctrine/DBAL/Types/PhoneNumberType.php +++ b/src/Doctrine/DBAL/Types/PhoneNumberType.php @@ -56,9 +56,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str throw new ConversionException('Expected \libphonenumber\PhoneNumber, got '.\gettype($value)); } - $util = PhoneNumberUtil::getInstance(); - - return $util->format($value, PhoneNumberFormat::E164); + return PhoneNumberUtil::getInstance()->format($value, PhoneNumberFormat::E164); } public function convertToPHPValue($value, AbstractPlatform $platform): ?PhoneNumber diff --git a/src/Form/DataTransformer/PhoneNumberToArrayTransformer.php b/src/Form/DataTransformer/PhoneNumberToArrayTransformer.php index 307421b6..32f284e7 100644 --- a/src/Form/DataTransformer/PhoneNumberToArrayTransformer.php +++ b/src/Form/DataTransformer/PhoneNumberToArrayTransformer.php @@ -36,23 +36,25 @@ public function __construct(array $countryChoices) $this->countryChoices = $countryChoices; } - public function transform($phoneNumber): array + public function transform($value): array { - if (null === $phoneNumber) { + if (null === $value) { return ['country' => '', 'number' => '']; - } elseif (false === $phoneNumber instanceof PhoneNumber) { + } + + if (false === $value instanceof PhoneNumber) { throw new TransformationFailedException('Expected a \libphonenumber\PhoneNumber.'); } $util = PhoneNumberUtil::getInstance(); - if (false === \in_array($util->getRegionCodeForNumber($phoneNumber), $this->countryChoices)) { + if (false === \in_array($util->getRegionCodeForNumber($value), $this->countryChoices)) { throw new TransformationFailedException('Invalid country.'); } return [ - 'country' => $util->getRegionCodeForNumber($phoneNumber), - 'number' => $util->format($phoneNumber, PhoneNumberFormat::NATIONAL), + 'country' => $util->getRegionCodeForNumber($value), + 'number' => $util->format($value, PhoneNumberFormat::NATIONAL), ]; } @@ -78,7 +80,7 @@ public function reverseTransform($value): ?PhoneNumber throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); } - if (false === \in_array($util->getRegionCodeForNumber($phoneNumber), $this->countryChoices)) { + if (null !== $phoneNumber && false === \in_array($util->getRegionCodeForNumber($phoneNumber), $this->countryChoices)) { throw new TransformationFailedException('Invalid country.'); } diff --git a/src/Form/DataTransformer/PhoneNumberToStringTransformer.php b/src/Form/DataTransformer/PhoneNumberToStringTransformer.php index d2e3bcd5..c203b5dc 100644 --- a/src/Form/DataTransformer/PhoneNumberToStringTransformer.php +++ b/src/Form/DataTransformer/PhoneNumberToStringTransformer.php @@ -51,33 +51,33 @@ public function __construct( $this->format = $format; } - public function transform($phoneNumber): string + public function transform($value): string { - if (null === $phoneNumber) { + if (null === $value) { return ''; - } elseif (false === $phoneNumber instanceof PhoneNumber) { + } + + if (false === $value instanceof PhoneNumber) { throw new TransformationFailedException('Expected a \libphonenumber\PhoneNumber.'); } $util = PhoneNumberUtil::getInstance(); if (PhoneNumberFormat::NATIONAL === $this->format) { - return $util->formatOutOfCountryCallingNumber($phoneNumber, $this->defaultRegion); + return $util->formatOutOfCountryCallingNumber($value, $this->defaultRegion); } - return $util->format($phoneNumber, $this->format); + return $util->format($value, $this->format); } - public function reverseTransform($string): ?PhoneNumber + public function reverseTransform($value): ?PhoneNumber { - if (!$string && '0' !== $string) { + if (!$value && '0' !== $value) { return null; } - $util = PhoneNumberUtil::getInstance(); - try { - return $util->parse($string, $this->defaultRegion); + return PhoneNumberUtil::getInstance()->parse($value, $this->defaultRegion); } catch (NumberParseException $e) { throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); } diff --git a/src/Serializer/Normalizer/PhoneNumberNormalizer.php b/src/Serializer/Normalizer/PhoneNumberNormalizer.php index e350423a..2c79e6de 100644 --- a/src/Serializer/Normalizer/PhoneNumberNormalizer.php +++ b/src/Serializer/Normalizer/PhoneNumberNormalizer.php @@ -76,7 +76,7 @@ public function supportsNormalization($data, $format = null, array $context = [] /** * @throws UnexpectedValueException */ - public function denormalize($data, $class, $format = null, array $context = []): ?PhoneNumber + public function denormalize($data, $type, $format = null, array $context = []): ?PhoneNumber { if (null === $data) { return null; diff --git a/src/Templating/Helper/PhoneNumberHelper.php b/src/Templating/Helper/PhoneNumberHelper.php index d96c3015..d9cc5e52 100644 --- a/src/Templating/Helper/PhoneNumberHelper.php +++ b/src/Templating/Helper/PhoneNumberHelper.php @@ -39,17 +39,7 @@ public function __construct(PhoneNumberUtil $phoneNumberUtil) $this->phoneNumberUtil = $phoneNumberUtil; } - /** - * Format a phone number. - * - * @param PhoneNumber|string $phoneNumber phone number - * @param int|string $format format, or format constant name - * - * @return string formatted phone number - * - * @throws InvalidArgumentException if an argument is invalid - */ - public function format($phoneNumber, $format = PhoneNumberFormat::INTERNATIONAL): string + public function format(PhoneNumber|string $phoneNumber, string|int $format = PhoneNumberFormat::INTERNATIONAL): string { $phoneNumber = $this->getPhoneNumber($phoneNumber); @@ -102,14 +92,7 @@ public function isType($phoneNumber, $type = PhoneNumberType::UNKNOWN): bool return $this->phoneNumberUtil->getNumberType($phoneNumber) === $type; } - /** - * @param PhoneNumber|string $phoneNumber - * - * @return PhoneNumber|void - * - * @throws \libphonenumber\NumberParseException - */ - private function getPhoneNumber($phoneNumber) + private function getPhoneNumber(PhoneNumber|string $phoneNumber): PhoneNumber { if (\is_string($phoneNumber)) { $phoneNumber = $this->phoneNumberUtil->parse($phoneNumber); diff --git a/src/Validator/Constraints/PhoneNumberValidator.php b/src/Validator/Constraints/PhoneNumberValidator.php index de330910..1a5beebf 100644 --- a/src/Validator/Constraints/PhoneNumberValidator.php +++ b/src/Validator/Constraints/PhoneNumberValidator.php @@ -16,6 +16,7 @@ use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberType; use libphonenumber\PhoneNumberUtil; +use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as PhoneNumberConstraint; use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; @@ -59,6 +60,10 @@ public function __construct(PhoneNumberUtil $phoneUtil = null, string $defaultRe public function validate($value, Constraint $constraint): void { + if (!$constraint instanceof PhoneNumberConstraint) { + return; + } + if (null === $value || '' === $value) { return; } @@ -137,7 +142,7 @@ public function validate($value, Constraint $constraint): void } } - private function getRegion(Constraint $constraint): ?string + private function getRegion(PhoneNumberConstraint $constraint): ?string { $defaultRegion = null; if (null !== $path = $constraint->regionPath) { @@ -176,10 +181,10 @@ private function getPropertyAccessor(): PropertyAccessorInterface /** * Add a violation. * - * @param mixed $value the value that should be validated - * @param Constraint $constraint the constraint for the validation + * @param mixed $value the value that should be validated + * @param PhoneNumberConstraint $constraint the constraint for the validation */ - private function addViolation($value, Constraint $constraint): void + private function addViolation($value, PhoneNumberConstraint $constraint): void { $this->context->buildViolation($constraint->getMessage()) ->setParameter('{{ types }}', implode(', ', $constraint->getTypeNames())) diff --git a/tests/Templating/Helper/PhoneNumberHelperTest.php b/tests/Templating/Helper/PhoneNumberHelperTest.php index 27938864..3730a7ec 100644 --- a/tests/Templating/Helper/PhoneNumberHelperTest.php +++ b/tests/Templating/Helper/PhoneNumberHelperTest.php @@ -89,15 +89,6 @@ public function testFormatAcceptString() $this->assertEquals('+371 22 222 222', $result); } - public function testFormatAcceptNotAllowValue() - { - $phoneNumberUtil = PhoneNumberUtil::getInstance(); - $helper = new PhoneNumberHelper($phoneNumberUtil); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('The phone number supplied is not PhoneNumber or string.'); - $helper->format(0037122222222); - } - public function formatOutOfCountryCallingNumberAcceptString() { $phoneNumberUtil = PhoneNumberUtil::getInstance();