From 7d5adf05d9e23b62e732ff7a1e20d8348a88e597 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Tue, 3 Oct 2023 12:33:03 +0200 Subject: [PATCH] Require PHP 8.1+ --- .github/workflows/static-analysis.yml | 4 +- .github/workflows/test.yml | 6 +- .gitignore | 2 +- Dockerfile8 => Dockerfile | 0 Dockerfile71 | 25 ---- MIGRATION.md | 52 ++++++++ Makefile | 17 +-- composer.json | 10 +- docker-compose.yaml | 19 +-- lib/AbstractCollection.php | 17 +-- lib/AbstractSectionCollection.php | 31 ++--- lib/Cache/CacheCollection.php | 11 +- lib/Cache/FileCache.php | 14 +- lib/Cache/RedisCache.php | 21 +-- lib/Cache/RuntimeCache.php | 2 +- lib/Calendar.php | 107 +++++++--------- lib/CalendarCollection.php | 15 +-- lib/CodePropertyTrait.php | 35 ----- lib/ContextTransforms.php | 13 +- lib/Currency.php | 19 ++- lib/CurrencyCollection.php | 8 +- lib/CurrencyFormatter.php | 4 +- lib/CurrencyNotDefined.php | 36 ++---- lib/DateTimeAccessor.php | 11 +- lib/DateTimeFormatter.php | 57 +++------ lib/GitHub/UrlResolver.php | 27 +--- lib/Locale.php | 73 ++++------- lib/Locale/ListPattern.php | 36 +----- lib/LocaleCollection.php | 8 +- lib/LocalePropertyTrait.php | 30 ----- lib/Localizable.php | 2 +- lib/LocalizedCurrency.php | 16 +-- lib/LocalizedCurrencyFormatter.php | 21 +-- lib/LocalizedDateTime.php | 8 +- lib/LocalizedNumberFormatter.php | 8 +- lib/LocalizedObject.php | 26 +--- lib/Number.php | 20 +-- lib/NumberFormatter.php | 26 ++-- lib/NumberPattern.php | 155 ++++++----------------- lib/NumberPatternParser.php | 11 +- lib/Numbers.php | 9 +- lib/Numbers/Symbols.php | 137 +++----------------- lib/Plurals.php | 10 +- lib/Plurals/Operands.php | 10 +- lib/Plurals/OperandsCache.php | 6 +- lib/Plurals/Relation.php | 27 +--- lib/Plurals/RelationCache.php | 9 +- lib/Plurals/Rule.php | 15 +-- lib/Plurals/RuleCache.php | 9 +- lib/Plurals/Samples.php | 22 ++-- lib/Plurals/SamplesCache.php | 9 +- lib/Provider/CachedProvider.php | 18 +-- lib/Provider/WebProvider.php | 29 ++--- lib/Repository.php | 49 +++---- lib/RepositoryPropertyTrait.php | 30 ----- lib/Supplemental.php | 7 +- lib/Supplemental/CurrencyData.php | 26 +--- lib/Supplemental/Fraction.php | 53 ++------ lib/Territory.php | 19 +-- lib/TerritoryCollection.php | 15 +-- lib/TerritoryNotDefined.php | 32 ++--- lib/Units.php | 59 ++++----- lib/Units/NumberPerUnit.php | 41 ++---- lib/Units/NumberWithUnit.php | 32 +---- lib/Units/Sequence.php | 13 +- lib/Units/Unit.php | 18 +-- phpstan.neon | 1 - phpunit.xml | 20 +-- tests/CalendarCollectionTest.php | 2 +- tests/CalendarTest.php | 6 +- tests/CodePropertyTraitTest.php | 32 ----- tests/CodePropertyTraitTest/A.php | 17 --- tests/ContextTransformsTest.php | 2 +- tests/CurrencyNotDefinedTest.php | 2 +- tests/CurrencyTest.php | 2 +- tests/DateFormatterTest.php | 2 +- tests/DateTimeAccessorTest.php | 2 +- tests/DateTimeFormatterTest.php | 4 +- tests/GitHub/UrlResolverTest.php | 2 +- tests/ListFormatterTest.php | 2 +- tests/LocalePropertyTraitTest.php | 25 ---- tests/LocalePropertyTraitTest/A.php | 18 --- tests/LocaleTest.php | 8 +- tests/LocaleTest/LocalizableSample.php | 2 +- tests/LocalizedCurrencyFormatterTest.php | 4 +- tests/LocalizedCurrencyTest.php | 4 +- tests/LocalizedDateTimeTest.php | 2 +- tests/LocalizedListFormatterTest.php | 2 +- tests/LocalizedLocaleTest.php | 2 +- tests/LocalizedNumberFormatterTest.php | 2 +- tests/LocalizedTerritoryTest.php | 2 +- tests/NumberFormatterTest.php | 2 +- tests/NumberPatternTest.php | 4 +- tests/NumberTest.php | 4 +- tests/NumbersTest.php | 8 +- tests/Plurals/OperandsTest.php | 2 +- tests/Plurals/RelationTest.php | 2 +- tests/Plurals/RuleTest.php | 2 +- tests/Plurals/SamplesTest.php | 2 +- tests/PluralsTest.php | 8 +- tests/RepositoryPropertyTraitTest.php | 25 ---- tests/RepositoryPropertyTraitTest/A.php | 18 --- tests/RepositoryTest.php | 15 ++- tests/Supplemental/CurrencyDataTest.php | 2 +- tests/Supplemental/FractionTest.php | 2 +- tests/SupplementalTest.php | 4 +- tests/TerritoryNotDefinedTest.php | 2 +- tests/TerritoryTest.php | 12 +- tests/TimeFormatterTest.php | 2 +- tests/Units/NumberPerUnitTest.php | 2 +- tests/Units/NumberWithUnitTest.php | 4 +- tests/Units/SequenceTest.php | 4 +- tests/Units/UnitTest.php | 2 +- tests/UnitsTest.php | 8 +- 114 files changed, 617 insertions(+), 1393 deletions(-) rename Dockerfile8 => Dockerfile (100%) delete mode 100644 Dockerfile71 delete mode 100644 lib/CodePropertyTrait.php delete mode 100644 lib/LocalePropertyTrait.php delete mode 100644 lib/RepositoryPropertyTrait.php delete mode 100644 tests/CodePropertyTraitTest.php delete mode 100644 tests/CodePropertyTraitTest/A.php delete mode 100644 tests/LocalePropertyTraitTest.php delete mode 100644 tests/LocalePropertyTraitTest/A.php delete mode 100644 tests/RepositoryPropertyTraitTest.php delete mode 100644 tests/RepositoryPropertyTraitTest/A.php diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 5e89832..449e4b5 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -7,14 +7,14 @@ on: jobs: phpstan: name: phpstan - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - name: Install PHP uses: shivammathur/setup-php@v2 with: - php-version: "7.1" + php-version: "8.1" ini-values: memory_limit=-1 tools: composer:v2 - name: Cache dependencies diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 62edb30..f3edb88 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,7 +7,7 @@ on: jobs: phpunit: name: phpunit - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest services: redis: # Docker Hub image @@ -24,8 +24,6 @@ jobs: strategy: matrix: php-version: - - "7.1" - - "7.4" - "8.1" steps: - name: Checkout @@ -58,7 +56,7 @@ jobs: ICANBOOGIE_CLDR_CHECK_URL: 'yes' - name: Upload code coverage - if: ${{ matrix.php-version == '7.1' }} + if: ${{ matrix.php-version == '8.1' }} env: COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | diff --git a/.gitignore b/.gitignore index 5e0c8fb..6a36af1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -.phpunit.result.cache +.phpunit.cache build cache composer.lock diff --git a/Dockerfile8 b/Dockerfile similarity index 100% rename from Dockerfile8 rename to Dockerfile diff --git a/Dockerfile71 b/Dockerfile71 deleted file mode 100644 index d604793..0000000 --- a/Dockerfile71 +++ /dev/null @@ -1,25 +0,0 @@ -FROM php:7.1-cli-buster - -RUN apt-get update && \ - apt-get install -y autoconf pkg-config && \ - pecl channel-update pecl.php.net && \ - pecl install redis-5.3.7 xdebug-2.9.8 && \ - docker-php-ext-enable opcache redis xdebug - -ENV COMPOSER_ALLOW_SUPERUSER 1 - -RUN apt-get update && \ - apt-get install unzip && \ - curl -s https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer | php -- --quiet && \ - mv composer.phar /usr/local/bin/composer - -RUN echo '\ -display_errors=On\n\ -error_reporting=E_ALL\n\ -date.timezone=UTC\n\ -' >> /usr/local/etc/php/conf.d/php.ini - -RUN echo '\ -xdebug.remote_enable=1\n\ -xdebug.remote_host=host.docker.internal\n\ -' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini diff --git a/MIGRATION.md b/MIGRATION.md index 1c944ff..d19cc2c 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -1,5 +1,31 @@ # Migration + + +## v5.x to v6.0 + +### New Requirements + +- PHP >=8.1 <8.2 + +### New features + +None + +### Backward Incompatible Changes + +None + +### Deprecated Features + +None + +### Other Changes + +None + + + ## v4.x to v5.0 ### New Requirements @@ -168,3 +194,29 @@ None - Compatible with PHP 8.1+ - Targets [CLDR v36](https://www.unicode.org/reports/tr35/tr35-57/tr35.html) - Improved type annotations, including generics. + + diff --git a/Makefile b/Makefile index f421258..fb0ec81 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,6 @@ PHPUNIT = vendor/bin/phpunit # do not edit the following lines -.PHONY: usage -usage: - @echo "test: Runs the test suite.\ndoc: Creates the documentation.\nclean: Removes the documentation, the dependencies and the Composer files." - vendor: @composer install @@ -24,7 +20,7 @@ test: test-dependencies .PHONY: test-coverage test-coverage: test-dependencies @mkdir -p build/coverage - @XDEBUG_MODE=coverage $(PHPUNIT) --coverage-html build/coverage --coverage-text + @XDEBUG_MODE=coverage $(PHPUNIT) --coverage-html build/coverage .PHONY: test-coveralls test-coveralls: test-dependencies @@ -36,22 +32,17 @@ test-cleanup: @rm -f cache/* .PHONY: test-container -test-container: test-container-71 - -.PHONY: test-container-71 -test-container-71: - @-docker-compose run --rm app71 bash - @docker-compose down +test-container: test-container-81 .PHONY: test-container-81 test-container-81: @-docker-compose run --rm app81 bash - @docker-compose down + @docker-compose down -v .PHONY: test-container-82 test-container-82: @-docker-compose run --rm app82 bash - @docker-compose down + @docker-compose down -v .PHONY: lint lint: diff --git a/composer.json b/composer.json index b9b0021..2d4954e 100644 --- a/composer.json +++ b/composer.json @@ -28,17 +28,17 @@ "sort-packages": true }, "require": { - "php": ">=7.1 <8.2", + "php": ">=8.1 <8.2", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "icanboogie/common": "^1.3", - "icanboogie/accessor": "^3.0|^4.0" + "icanboogie/common": "^2.0", + "icanboogie/accessor": "^4.0" }, "require-dev": { "ext-redis": "*", - "phpstan/phpstan": "^1.4", - "phpunit/phpunit": "^7.5.20|^8.5" + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^10.3" }, "autoload": { "psr-4": { diff --git a/docker-compose.yaml b/docker-compose.yaml index 248c6ae..f195a94 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,25 +1,10 @@ --- version: "3.2" services: - app71: - build: - context: . - dockerfile: Dockerfile71 - environment: - PHP_IDE_CONFIG: 'serverName=icanboogie-cldr' - ICANBOOGIE_CLDR_REDIS_HOST: redis - ICANBOOGIE_CLDR_REDIS_PORT: 6379 - depends_on: - - redis - volumes: - - .:/app:delegated - - ~/.composer:/root/.composer:delegated - working_dir: /app - app81: build: context: . - dockerfile: Dockerfile8 + dockerfile: Dockerfile args: PHP_VERSION: '8.1' environment: @@ -36,7 +21,7 @@ services: app82: build: context: . - dockerfile: Dockerfile8 + dockerfile: Dockerfile args: PHP_VERSION: '8.2' environment: diff --git a/lib/AbstractCollection.php b/lib/AbstractCollection.php index e8c7e69..4b00d44 100644 --- a/lib/AbstractCollection.php +++ b/lib/AbstractCollection.php @@ -13,6 +13,7 @@ use ArrayAccess; use BadMethodCallException; +use Closure; /** * An abstract collection. @@ -27,16 +28,11 @@ abstract class AbstractCollection implements ArrayAccess /** * @var array */ - private $collection = []; + private array $collection = []; - /** - * @var callable(string): T - */ - private $create_instance; - - public function __construct(callable $create_instance) - { - $this->create_instance = $create_instance; + public function __construct( + private readonly Closure $create_instance + ) { } /** @@ -55,7 +51,6 @@ public function offsetExists($offset): bool #[\ReturnTypeWillChange] public function offsetGet($offset) { - return $this->collection[$offset] - ?? $this->collection[$offset] = ($this->create_instance)($offset); + return $this->collection[$offset] ??= ($this->create_instance)($offset); } } diff --git a/lib/AbstractSectionCollection.php b/lib/AbstractSectionCollection.php index f8d7524..ef0f579 100644 --- a/lib/AbstractSectionCollection.php +++ b/lib/AbstractSectionCollection.php @@ -12,28 +12,18 @@ namespace ICanBoogie\CLDR; use ArrayAccess; -use ICanBoogie\Accessor\AccessorTrait; use ICanBoogie\OffsetNotDefined; /** * @implements ArrayAccess - * - * @property-read Repository $repository */ abstract class AbstractSectionCollection implements ArrayAccess { - use AccessorTrait; use CollectionTrait; - use RepositoryPropertyTrait; - /** - * @var Repository - */ - private $repository; - - public function __construct(Repository $repository) - { - $this->repository = $repository; + public function __construct( + public readonly Repository $repository + ) { } abstract public function offsetExists($offset): bool; @@ -44,7 +34,7 @@ abstract public function offsetExists($offset): bool; * * @phpstan-ignore-next-line */ - private $sections = []; + private array $sections = []; /** * @param string $offset @@ -52,19 +42,18 @@ abstract public function offsetExists($offset): bool; * @throws OffsetNotDefined * @throws ResourceNotFound */ - #[\ReturnTypeWillChange] - public function offsetGet($offset) /* @phpstan-ignore-line */ + #[\ReturnTypeWillChange] // @phpstan-ignore-line + public function offsetGet($offset) { if (!$this->offsetExists($offset)) { throw new OffsetNotDefined([ $offset, $this ]); } - return $this->sections[$offset] - ?? $this->sections[$offset] = $this->repository->fetch( - $this->path_for($offset), - $this->data_path_for($offset) - ); + return $this->sections[$offset] ??= $this->repository->fetch( + $this->path_for($offset), + $this->data_path_for($offset) + ); } /** diff --git a/lib/Cache/CacheCollection.php b/lib/Cache/CacheCollection.php index 734a57d..78f0f12 100644 --- a/lib/Cache/CacheCollection.php +++ b/lib/Cache/CacheCollection.php @@ -18,17 +18,12 @@ */ final class CacheCollection implements Cache { - /** - * @var Cache[] - */ - private $collection; - /** * @param Cache[] $collection */ - public function __construct(array $collection) - { - $this->collection = $collection; + public function __construct( + private readonly array $collection + ) { } public function get(string $path): ?array diff --git a/lib/Cache/FileCache.php b/lib/Cache/FileCache.php index 4334aa8..886a203 100644 --- a/lib/Cache/FileCache.php +++ b/lib/Cache/FileCache.php @@ -39,17 +39,12 @@ */ final class FileCache implements Cache { - /** - * @var bool - */ - static private $release_after; + static private bool $release_after; /** * Absolute path to the storage directory. - * - * @var string */ - private $path; + private string $path; /** * @param string $path Absolute path to the storage directory. @@ -58,10 +53,7 @@ public function __construct(string $path) { $this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; - if (self::$release_after === null) - { - self::$release_after = !(strpos(PHP_OS, 'WIN') === 0); - } + self::$release_after ??= !(str_starts_with(PHP_OS, 'WIN')); } public function get(string $path): ?array diff --git a/lib/Cache/RedisCache.php b/lib/Cache/RedisCache.php index af7fc14..fe056a3 100644 --- a/lib/Cache/RedisCache.php +++ b/lib/Cache/RedisCache.php @@ -25,23 +25,10 @@ final class RedisCache implements Cache { public const DEFAULT_PREFIX = 'icanboogie-cldr-'; - /** - * @var Redis|RedisCluster - */ - private $redis; - - /** - * @var string - */ - private $prefix; - - /** - * @param Redis|RedisCluster $redis - */ - public function __construct($redis, string $prefix = self::DEFAULT_PREFIX) - { - $this->redis = $redis; - $this->prefix = $prefix; + public function __construct( + private readonly RedisCluster|Redis $redis, + private readonly string $prefix = self::DEFAULT_PREFIX + ) { } public function get(string $path): ?array diff --git a/lib/Cache/RuntimeCache.php b/lib/Cache/RuntimeCache.php index c65773a..229e590 100644 --- a/lib/Cache/RuntimeCache.php +++ b/lib/Cache/RuntimeCache.php @@ -18,7 +18,7 @@ final class RuntimeCache implements Cache /** * @var array */ - private $cache = []; + private array $cache = []; /** * @inheritDoc diff --git a/lib/Calendar.php b/lib/Calendar.php index 85a386c..a6e23f7 100644 --- a/lib/Calendar.php +++ b/lib/Calendar.php @@ -15,12 +15,9 @@ use DateTimeInterface; use ICanBoogie\Accessor\AccessorTrait; -use function var_dump; - /** * Representation of a locale calendar. * - * @property-read Locale $locale The locale this calendar is defined in. * @property-read DateTimeFormatter $datetime_formatter A datetime formatter. * @property-read DateFormatter $date_formatter A date formatter. * @property-read TimeFormatter $time_formatter A time formatter. @@ -87,12 +84,11 @@ final class Calendar extends ArrayObject * @uses lazy_get_time_formatter */ use AccessorTrait; - use LocalePropertyTrait; /** * @var array */ - static private $era_widths_mapping = [ + static private array $era_widths_mapping = [ self::WIDTH_ABBR => self::ERA_ABBR, self::WIDTH_NARROW => self::ERA_NARROW, @@ -116,17 +112,15 @@ private function lazy_get_time_formatter(): TimeFormatter return new TimeFormatter($this); } - /** - * @var ContextTransforms - */ - private $context_transforms; + private readonly ContextTransforms $context_transforms; /** * @param array $data */ - public function __construct(Locale $locale, array $data) - { - $this->locale = $locale; + public function __construct( + public readonly Locale $locale, + array $data + ) { $this->context_transforms = $locale->context_transforms; $data = $this->transform_data($data); @@ -164,32 +158,32 @@ public function __get(string $property) } /** - * @param DateTimeInterface|string|int $datetime - * - * @see \ICanBoogie\CLDR\DateTimeFormatter::format + * @see DateTimeFormatter::format */ - public function format_datetime($datetime, string $pattern_or_width_or_skeleton): string - { + public function format_datetime( + DateTimeInterface|int|string $datetime, + string $pattern_or_width_or_skeleton + ): string { return $this->datetime_formatter->format($datetime, $pattern_or_width_or_skeleton); } /** - * @param DateTimeInterface|string|int $datetime - * - * @see \ICanBoogie\CLDR\DateFormatter::format + * @see DateFormatter::format */ - public function format_date($datetime, string $pattern_or_width_or_skeleton): string - { + public function format_date( + DateTimeInterface|int|string $datetime, + string $pattern_or_width_or_skeleton + ): string { return $this->date_formatter->format($datetime, $pattern_or_width_or_skeleton); } /** - * @param DateTimeInterface|string|int $datetime - * - * @see \ICanBoogie\CLDR\TimeFormatter::format + * @see TimeFormatter::format */ - public function format_time($datetime, string $pattern_or_width_or_skeleton): string - { + public function format_time( + DateTimeInterface|int|string $datetime, + string $pattern_or_width_or_skeleton + ): string { return $this->time_formatter->format($datetime, $pattern_or_width_or_skeleton); } @@ -347,7 +341,7 @@ private function transform_quarters(array $names, string $width, bool $standalon { if ($standalone) { - if ($width != self::WIDTH_WIDE) + if ($width !== self::WIDTH_WIDE) { return $names; } @@ -359,35 +353,25 @@ private function transform_quarters(array $names, string $width, bool $standalon ); } - switch ($width) - { - case self::WIDTH_ABBR: - - return $this->apply_transform( - $names, - ContextTransforms::USAGE_QUARTER_ABBREVIATED, - ContextTransforms::TYPE_STAND_ALONE - ); - - case self::WIDTH_WIDE: - - return $this->apply_transform( - $names, - ContextTransforms::USAGE_QUARTER_FORMAT_WIDE, - ContextTransforms::TYPE_STAND_ALONE - ); - - case self::WIDTH_NARROW: - - return $this->apply_transform( - $names, - ContextTransforms::USAGE_QUARTER_NARROW, - ContextTransforms::TYPE_STAND_ALONE - ); - - } - - return $names; // @codeCoverageIgnore + return match ($width) { + self::WIDTH_ABBR => $this->apply_transform( + $names, + ContextTransforms::USAGE_QUARTER_ABBREVIATED, + ContextTransforms::TYPE_STAND_ALONE + ), + self::WIDTH_WIDE => $this->apply_transform( + $names, + ContextTransforms::USAGE_QUARTER_FORMAT_WIDE, + ContextTransforms::TYPE_STAND_ALONE + ), + self::WIDTH_NARROW => $this->apply_transform( + $names, + ContextTransforms::USAGE_QUARTER_NARROW, + ContextTransforms::TYPE_STAND_ALONE + ), + default => $names, + }; + // @codeCoverageIgnore } /** @@ -401,10 +385,9 @@ private function apply_transform(array $names, string $usage, string $type): arr { $context_transforms = $this->context_transforms; - return array_map(function (string $str) use ($context_transforms, $usage, $type): string { - - return $context_transforms->transform($str, $usage, $type); - - }, $names); + return array_map( + static fn(string $str): string => $context_transforms->transform($str, $usage, $type), + $names + ); } } diff --git a/lib/CalendarCollection.php b/lib/CalendarCollection.php index d23d9b7..5cfeb81 100644 --- a/lib/CalendarCollection.php +++ b/lib/CalendarCollection.php @@ -11,8 +11,6 @@ namespace ICanBoogie\CLDR; -use ICanBoogie\Accessor\AccessorTrait; - use function is_array; /** @@ -29,16 +27,9 @@ */ final class CalendarCollection extends AbstractCollection { - /** - * @uses get_locale - */ - use AccessorTrait; - use LocalePropertyTrait; - - public function __construct(Locale $locale) - { - $this->locale = $locale; - + public function __construct( + public readonly Locale $locale + ) { parent::__construct(function (string $id): Calendar { $data = $this->locale["ca-$id"]; diff --git a/lib/CodePropertyTrait.php b/lib/CodePropertyTrait.php deleted file mode 100644 index 638c9c7..0000000 --- a/lib/CodePropertyTrait.php +++ /dev/null @@ -1,35 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ICanBoogie\CLDR; - -/** - * A trait for classes implementing the `code` property. - * - * @property-read string $code - */ -trait CodePropertyTrait -{ - /** - * @var string - */ - private $code; - - protected function get_code(): string - { - return $this->code; - } - - public function __toString(): string - { - return $this->code; - } -} diff --git a/lib/ContextTransforms.php b/lib/ContextTransforms.php index ee18da4..0837dad 100644 --- a/lib/ContextTransforms.php +++ b/lib/ContextTransforms.php @@ -12,9 +12,9 @@ namespace ICanBoogie\CLDR; use LogicException; + use function mb_strtoupper; use function mb_substr; -use function var_dump; /** * @see http://unicode.org/reports/tr35/tr35-general.html#contextTransformUsage_type_attribute_values @@ -60,17 +60,12 @@ final class ContextTransforms public const TRANSFORM_TITLECASE_FIRSTWORD = 'titlecase-firstword'; public const TRANSFORM_NO_CHANGE = 'no-change'; - /** - * @var array> - */ - private $rules; - /** * @param array> $rules */ - public function __construct(array $rules) - { - $this->rules = $rules; + public function __construct( + private readonly array $rules + ) { } /** diff --git a/lib/Currency.php b/lib/Currency.php index 86382b0..9d6a6e5 100644 --- a/lib/Currency.php +++ b/lib/Currency.php @@ -35,7 +35,6 @@ * * @internal * - * @property-read string $code The ISO 4217 code for the currency. * @property-read Fraction $fraction * * @see http://unicode.org/reports/tr35/tr35-numbers.html#Supplemental_Currency_Data @@ -43,17 +42,23 @@ final class Currency { /** - * @uses get_code * @uses lazy_get_fraction */ use AccessorTrait; - use RepositoryPropertyTrait; - use CodePropertyTrait; - public function __construct(Repository $repository, string $code) + /** + * @param string $code + * The ISO 4217 code for the currency. + */ + public function __construct( + public readonly Repository $repository, + public readonly string $code + ) { + } + + public function __toString(): string { - $this->repository = $repository; - $this->code = $code; + return $this->code; } private function lazy_get_fraction(): Fraction diff --git a/lib/CurrencyCollection.php b/lib/CurrencyCollection.php index 7de9c1f..d7120ec 100644 --- a/lib/CurrencyCollection.php +++ b/lib/CurrencyCollection.php @@ -42,12 +42,10 @@ final class CurrencyCollection extends AbstractCollection * @uses lazy_get_codes */ use AccessorTrait; - use RepositoryPropertyTrait; - - public function __construct(Repository $repository) - { - $this->repository = $repository; + public function __construct( + public readonly Repository $repository + ) { parent::__construct(function (string $currency_code): Currency { $this->assert_defined($currency_code); diff --git a/lib/CurrencyFormatter.php b/lib/CurrencyFormatter.php index 20faf36..5dd6769 100644 --- a/lib/CurrencyFormatter.php +++ b/lib/CurrencyFormatter.php @@ -26,8 +26,8 @@ final class CurrencyFormatter extends NumberFormatter * @inheritDoc */ public function format( - $number, - $pattern, + float|int|string $number, + NumberPattern|string $pattern, Symbols $symbols = null, string $currencySymbol = self::DEFAULT_CURRENCY_SYMBOL ): string { diff --git a/lib/CurrencyNotDefined.php b/lib/CurrencyNotDefined.php index eb02b69..9b32835 100644 --- a/lib/CurrencyNotDefined.php +++ b/lib/CurrencyNotDefined.php @@ -11,41 +11,25 @@ namespace ICanBoogie\CLDR; -use ICanBoogie\Accessor\AccessorTrait; use InvalidArgumentException; use Throwable; /** * Exception thrown when a currency is not defined. - * - * @property-read string $currency_code The ISO code of the currency. */ class CurrencyNotDefined extends InvalidArgumentException implements Exception { /** - * @uses get_currency_code + * @param string $currency_code + * The ISO code of the currency. */ - use AccessorTrait; - - /** - * @var string - */ - private $currency_code; - - private function get_currency_code(): string - { - return $this->currency_code; - } - - public function __construct(string $currency_code, string $message = null, Throwable $previous = null) - { - $this->currency_code = $currency_code; - - if (!$message) - { - $message = "Currency not defined for code: $currency_code."; - } - - parent::__construct($message, 0, $previous); + public function __construct( + public readonly string $currency_code, + string $message = null, + Throwable $previous = null + ) { + $message ??= "Currency not defined for code: $currency_code."; + + parent::__construct($message, previous: $previous); } } diff --git a/lib/DateTimeAccessor.php b/lib/DateTimeAccessor.php index 874c957..818168b 100644 --- a/lib/DateTimeAccessor.php +++ b/lib/DateTimeAccessor.php @@ -31,14 +31,9 @@ */ class DateTimeAccessor { - /** - * @var DateTimeInterface - */ - private $datetime; - - public function __construct(DateTimeInterface $datetime) - { - $this->datetime = $datetime; + public function __construct( + private readonly DateTimeInterface $datetime + ) { } /** diff --git a/lib/DateTimeFormatter.php b/lib/DateTimeFormatter.php index 90815e4..af25b89 100644 --- a/lib/DateTimeFormatter.php +++ b/lib/DateTimeFormatter.php @@ -13,7 +13,7 @@ use DateTimeImmutable; use DateTimeInterface; -use ICanBoogie\Accessor\AccessorTrait; + use function ceil; use function floor; use function in_array; @@ -23,6 +23,7 @@ use function str_repeat; use function strlen; use function substr; + use const STR_PAD_LEFT; /** @@ -35,11 +36,6 @@ */ class DateTimeFormatter implements Formatter { - /** - * @uses get_calendar - */ - use AccessorTrait; - public const WIDTH_FULL = 'full'; public const WIDTH_LONG = 'long'; public const WIDTH_MEDIUM = 'medium'; @@ -48,7 +44,7 @@ class DateTimeFormatter implements Formatter /** * @var string[] */ - static private $widths = [ + static private array $widths = [ self::WIDTH_FULL, self::WIDTH_LONG, @@ -63,7 +59,7 @@ class DateTimeFormatter implements Formatter * @var array * Where _key_ is a pattern character and _value_ its formatter. */ - private static $formatters = [ + private static array $formatters = [ 'G' => 'format_era', 'y' => 'format_year', @@ -174,21 +170,10 @@ static private function numeric_pad(int $value, int $length = 2): string return str_pad((string) $value, $length, '0', STR_PAD_LEFT); } - /** - * The calendar used to format the datetime. - * - * @var Calendar - */ - private $calendar; - - private function get_calendar(): Calendar - { - return $this->calendar; - } + public function __construct( + public readonly Calendar $calendar + ) { - public function __construct(Calendar $calendar) - { - $this->calendar = $calendar; } /** @@ -365,8 +350,8 @@ private function format_year(DateTimeAccessor $datetime, int $length): string * Quarter - Use one or two "Q" for the numerical quarter, three for the abbreviation, or four * for the full (wide) name. [1..2,3,4] * - * @uses \ICanBoogie\CLDR\Calendar::$abbreviated_quarters - * @uses \ICanBoogie\CLDR\Calendar::$wide_quarters + * @uses Calendar::$abbreviated_quarters + * @uses Calendar::$wide_quarters */ private function format_quarter( DateTimeAccessor $datetime, @@ -396,8 +381,8 @@ private function format_quarter( * Stand-Alone Quarter - Use one or two "q" for the numerical quarter, three for the * abbreviation, or four for the full (wide) name. [1..2,3,4] * - * @uses \ICanBoogie\CLDR\Calendar::$standalone_abbreviated_quarters - * @uses \ICanBoogie\CLDR\Calendar::$standalone_wide_quarters + * @uses Calendar::$standalone_abbreviated_quarters + * @uses Calendar::$standalone_wide_quarters */ private function format_standalone_quarter(DateTimeAccessor $datetime, int $length): string { @@ -417,9 +402,9 @@ private function format_standalone_quarter(DateTimeAccessor $datetime, int $leng * Month - Use one or two "M" for the numerical month, three for the abbreviation, four for * the full name, or five for the narrow name. [1..2,3,4,5] * - * @uses \ICanBoogie\CLDR\Calendar::$abbreviated_months - * @uses \ICanBoogie\CLDR\Calendar::$wide_months - * @uses \ICanBoogie\CLDR\Calendar::$narrow_months + * @uses Calendar::$abbreviated_months + * @uses Calendar::$wide_months + * @uses Calendar::$narrow_months */ private function format_month( DateTimeAccessor $datetime, @@ -451,9 +436,9 @@ private function format_month( * Stand-Alone Month - Use one or two "L" for the numerical month, three for the abbreviation, * or four for the full (wide) name, or 5 for the narrow name. [1..2,3,4,5] * - * @uses \ICanBoogie\CLDR\Calendar::$standalone_abbreviated_months - * @uses \ICanBoogie\CLDR\Calendar::$standalone_wide_months - * @uses \ICanBoogie\CLDR\Calendar::$standalone_narrow_months + * @uses Calendar::$standalone_abbreviated_months + * @uses Calendar::$standalone_wide_months + * @uses Calendar::$standalone_narrow_months */ private function format_standalone_month(DateTimeAccessor $datetime, int $length): string { @@ -594,10 +579,10 @@ private function format_day_in_week(DateTimeAccessor $datetime, int $length): st * three for the abbreviated day name, four for the full (wide) name, five for the narrow name, * or six for the short name. * - * @uses \ICanBoogie\CLDR\Calendar::$standalone_abbreviated_days - * @uses \ICanBoogie\CLDR\Calendar::$standalone_wide_days - * @uses \ICanBoogie\CLDR\Calendar::$standalone_narrow_days - * @uses \ICanBoogie\CLDR\Calendar::$standalone_short_days + * @uses Calendar::$standalone_abbreviated_days + * @uses Calendar::$standalone_wide_days + * @uses Calendar::$standalone_narrow_days + * @uses Calendar::$standalone_short_days */ private function format_day_in_week_stand_alone(DateTimeAccessor $datetime, int $length): string { diff --git a/lib/GitHub/UrlResolver.php b/lib/GitHub/UrlResolver.php index 0dce402..ac4d87b 100644 --- a/lib/GitHub/UrlResolver.php +++ b/lib/GitHub/UrlResolver.php @@ -42,30 +42,11 @@ class UrlResolver ]; - /** - * @var string - */ - private $origin; - - /** - * @var string - */ - private $version; - - /** - * @var string - */ - private $variation; - public function __construct( - string $origin = self::DEFAULT_ORIGIN, - string $version = self::DEFAULT_VERSION, - string $variation = self::DEFAULT_VARIATION - ) - { - $this->origin = $origin; - $this->version = $version; - $this->variation = $variation; + private readonly string $origin = self::DEFAULT_ORIGIN, + private readonly string $version = self::DEFAULT_VERSION, + private readonly string $variation = self::DEFAULT_VARIATION + ) { } /** diff --git a/lib/Locale.php b/lib/Locale.php index a2f1fe6..c032c63 100644 --- a/lib/Locale.php +++ b/lib/Locale.php @@ -11,6 +11,7 @@ namespace ICanBoogie\CLDR; +use ICanBoogie\Accessor\AccessorTrait; use InvalidArgumentException; use LogicException; @@ -20,20 +21,28 @@ /** * Representation of a locale. * - * @property-read string $code Locale identifier. * @property-read string $language Unicode language. + * @uses self::get_language() * @property-read CalendarCollection $calendars The calendar collection of the locale. + * @uses self::lazy_get_calendars() * @property-read Calendar $calendar The preferred calendar for this locale. + * @uses self::lazy_get_calendar() * @property-read Numbers $numbers + * @uses self::lazy_get_numbers() * @property-read LocalizedNumberFormatter $number_formatter + * @uses self::lazy_get_number_formatter() * @property-read LocalizedCurrencyFormatter $currency_formatter + * @uses self::lazy_get_currency_formatter() * @property-read LocalizedListFormatter $list_formatter + * @uses self::lazy_get_list_formatter() * @property-read ContextTransforms $context_transforms + * @uses self::lazy_get_context_transforms() * @property-read Units $units + * @uses self::lazy_get_units() */ class Locale extends AbstractSectionCollection { - use CodePropertyTrait; + use AccessorTrait; /** * Where _key_ is an offset and _value_ and array where `0` is a pattern for the path and `1` the data path. @@ -74,22 +83,14 @@ class Locale extends AbstractSectionCollection ]; /** - * @var Repository + * @param string $code + * The ISO code of the locale. */ - private $repository; - - /** - * @param string $code The ISO code of the locale. - */ - public function __construct(Repository $repository, string $code) - { - if (!$code) - { - throw new InvalidArgumentException("Locale identifier cannot be empty."); - } - - $this->repository = $repository; - $this->code = $code; + public function __construct( + Repository $repository, + public readonly string $code + ) { + strlen($code) > 0 or throw new InvalidArgumentException("Locale identifier cannot be empty."); parent::__construct($repository); } @@ -109,9 +110,6 @@ protected function data_path_for(string $offset): string return "main/$this->code/" . self::OFFSET_MAPPING[$offset][1]; } - /** - * @uses get_language - */ protected function get_language(): string { [ $language ] = explode('-', $this->code, 2); @@ -119,59 +117,38 @@ protected function get_language(): string return $language; } - /** - * @uses lazy_get_calendars - */ protected function lazy_get_calendars(): CalendarCollection { return new CalendarCollection($this); } - /** - * @uses lazy_get_calendar - */ protected function lazy_get_calendar(): Calendar { /** @var Calendar */ return $this->calendars['gregorian']; // TODO-20131101: use preferred data } - /** - * @uses lazy_get_numbers - */ protected function lazy_get_numbers(): Numbers { /** @phpstan-ignore-next-line */ return new Numbers($this, $this['numbers']); } - /** - * @uses lazy_get_number_formatter - */ protected function lazy_get_number_formatter(): LocalizedNumberFormatter { return $this->localize($this->repository->number_formatter); } - /** - * @uses lazy_get_currency_formatter - */ protected function lazy_get_currency_formatter(): LocalizedCurrencyFormatter { return $this->localize($this->repository->currency_formatter); } - /** - * @uses lazy_get_list_formatter - */ protected function lazy_get_list_formatter(): LocalizedListFormatter { return $this->localize($this->repository->list_formatter); } - /** - * @uses lazy_get_context_transforms - */ protected function lazy_get_context_transforms(): ContextTransforms { try @@ -186,9 +163,6 @@ protected function lazy_get_context_transforms(): ContextTransforms } } - /** - * @uses lazy_get_units - */ protected function lazy_get_units(): Units { return new Units($this); @@ -258,11 +232,11 @@ public function format_number($number, string $pattern = null): string } /** - * @param float|int $number + * @param float|int|numeric-string $number * * @see LocalizedNumberFormatter::format */ - public function format_percent($number, string $pattern = null): string + public function format_percent(float|int|string $number, string $pattern = null): string { return $this->number_formatter->format( $number, @@ -273,12 +247,11 @@ public function format_percent($number, string $pattern = null): string /** * Formats currency using localized conventions. * - * @param float|int $number - * @param Currency|string $currency + * @param float|int|numeric-string $number */ public function format_currency( - $number, - $currency, + float|int|string $number, + Currency|string $currency, string $pattern = LocalizedCurrencyFormatter::PATTERN_STANDARD ): string { return $this->currency_formatter->format($number, $currency, $pattern); diff --git a/lib/Locale/ListPattern.php b/lib/Locale/ListPattern.php index 359e235..4089308 100644 --- a/lib/Locale/ListPattern.php +++ b/lib/Locale/ListPattern.php @@ -16,30 +16,6 @@ */ final class ListPattern { - /** - * @var string - * @readonly - */ - public $two; - - /** - * @var string - * @readonly - */ - public $start; - - /** - * @var string - * @readonly - */ - public $middle; - - /** - * @var string - * @readonly - */ - public $end; - /** * @param array{ * 2: string, @@ -59,14 +35,10 @@ static public function from(array $list_pattern): self } private function __construct( - string $two, - string $start, - string $middle, - string $end + public readonly string $two, + public readonly string $start, + public readonly string $middle, + public readonly string $end ) { - $this->two = $two; - $this->start = $start; - $this->middle = $middle; - $this->end = $end; } } diff --git a/lib/LocaleCollection.php b/lib/LocaleCollection.php index 27ccbec..d57ed64 100644 --- a/lib/LocaleCollection.php +++ b/lib/LocaleCollection.php @@ -22,12 +22,10 @@ class LocaleCollection extends AbstractCollection { use AccessorTrait; - use RepositoryPropertyTrait; - - public function __construct(Repository $repository) - { - $this->repository = $repository; + public function __construct( + public readonly Repository $repository + ) { parent::__construct(function (string $code): Locale { $this->assert_locale_is_valid($code); diff --git a/lib/LocalePropertyTrait.php b/lib/LocalePropertyTrait.php deleted file mode 100644 index e2e790b..0000000 --- a/lib/LocalePropertyTrait.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ICanBoogie\CLDR; - -/** - * A trait for classes implementing the `locale` property. - * - * @property-read Locale $locale - */ -trait LocalePropertyTrait -{ - /** - * @var Locale - */ - private $locale; - - private function get_locale(): Locale - { - return $this->locale; - } -} diff --git a/lib/Localizable.php b/lib/Localizable.php index 4a3688e..d066422 100644 --- a/lib/Localizable.php +++ b/lib/Localizable.php @@ -28,5 +28,5 @@ interface Localizable * * @return LocalizedObject */ - static public function localize($source, Locale $locale, array $options = []): LocalizedObject; + static public function localize(object $source, Locale $locale, array $options = []): LocalizedObject; } diff --git a/lib/LocalizedCurrency.php b/lib/LocalizedCurrency.php index 1f5537a..64ecbd4 100644 --- a/lib/LocalizedCurrency.php +++ b/lib/LocalizedCurrency.php @@ -59,10 +59,7 @@ public function name_for(int $count = null): string return $this->locale['currencies'][$this->target->code][$offset]; } - /** - * @var string|null - */ - private $symbol; + private string $symbol; /** * Returns the localized symbol of the currency. @@ -71,17 +68,18 @@ public function name_for(int $count = null): string */ protected function get_symbol(): string { - return $this->symbol - ?? $this->symbol = $this->locale['currencies'][$this->target->code]['symbol']; // @phpstan-ignore-line + return $this->symbol ??= $this->locale['currencies'][$this->target->code]['symbol']; // @phpstan-ignore-line } /** * Formats currency using localized conventions. * - * @param float|int $number + * @param float|int|numeric-string $number */ - public function format($number, string $pattern = LocalizedCurrencyFormatter::PATTERN_STANDARD): string - { + public function format( + float|int|string $number, + string $pattern = LocalizedCurrencyFormatter::PATTERN_STANDARD + ): string { return $this->formatter->format($number, $this->target, $pattern); } } diff --git a/lib/LocalizedCurrencyFormatter.php b/lib/LocalizedCurrencyFormatter.php index 568a421..02b66d5 100644 --- a/lib/LocalizedCurrencyFormatter.php +++ b/lib/LocalizedCurrencyFormatter.php @@ -24,22 +24,27 @@ class LocalizedCurrencyFormatter extends LocalizedObject implements Formatter /** * Formats currency using localized conventions. * - * @param float|int $number - * @param Currency|string $currency + * @param float|int|numeric-string $number + * @param string|Currency $currency */ - public function __invoke($number, $currency, string $pattern = self::PATTERN_STANDARD): string - { + public function __invoke( + float|int|string $number, + Currency|string $currency, + string $pattern = self::PATTERN_STANDARD + ): string { return $this->format($number, $currency, $pattern); } /** * Formats currency using localized conventions. * - * @param float|int $number - * @param Currency|string $currency + * @param float|int|numeric-string $number */ - public function format($number, $currency, string $pattern = self::PATTERN_STANDARD): string - { + public function format( + float|int|string $number, + Currency|string $currency, + string $pattern = self::PATTERN_STANDARD + ): string { return $this->target->format( $number, $this->resolve_pattern($pattern), diff --git a/lib/LocalizedDateTime.php b/lib/LocalizedDateTime.php index 2c925bc..9d12277 100644 --- a/lib/LocalizedDateTime.php +++ b/lib/LocalizedDateTime.php @@ -53,7 +53,7 @@ final class LocalizedDateTime extends LocalizedObjectWithFormatter /** * @var string[] */ - static private $format_widths = [ 'full', 'long', 'medium', 'short' ]; + static private array $format_widths = [ 'full', 'long', 'medium', 'short' ]; /** * @inheritDoc @@ -74,7 +74,7 @@ public function __get($property) { $width = substr($property, 3); - if (strpos($property, 'as_') === 0 && in_array($width, self::$format_widths)) + if (str_starts_with($property, 'as_') && in_array($width, self::$format_widths)) { return $this->{ 'format_as_' . $width }(); } @@ -83,7 +83,7 @@ public function __get($property) { return parent::__get($property); } - catch (PropertyNotDefined $e) + catch (PropertyNotDefined) { return $this->target->$property; } @@ -108,7 +108,7 @@ public function __set($property, $value): void */ public function __call($method, $arguments) { - if (strpos($method, 'format_as_') === 0 && in_array($width = substr($method, 10), self::$format_widths)) + if (str_starts_with($method, 'format_as_') && in_array($width = substr($method, 10), self::$format_widths)) { return $this->format($width); } diff --git a/lib/LocalizedNumberFormatter.php b/lib/LocalizedNumberFormatter.php index 05268e7..d690dab 100644 --- a/lib/LocalizedNumberFormatter.php +++ b/lib/LocalizedNumberFormatter.php @@ -21,9 +21,9 @@ class LocalizedNumberFormatter extends LocalizedObject implements Formatter /** * Formats a number. * - * @param float|int $number The number to format. + * @param float|int|numeric-string $number */ - public function __invoke($number, string $pattern = null): string + public function __invoke(float|int|string $number, string $pattern = null): string { return $this->format($number, $pattern); } @@ -31,9 +31,9 @@ public function __invoke($number, string $pattern = null): string /** * Formats a number. * - * @param float|int $number The number to format. + * @param float|int|numeric-string $number */ - public function format($number, string $pattern = null): string + public function format(float|int|string $number, string $pattern = null): string { $numbers = $this->locale->numbers; diff --git a/lib/LocalizedObject.php b/lib/LocalizedObject.php index 2d56fea..a1c709d 100644 --- a/lib/LocalizedObject.php +++ b/lib/LocalizedObject.php @@ -27,7 +27,6 @@ abstract class LocalizedObject * @uses get_locale */ use AccessorTrait; - use LocalePropertyTrait; /** * Creates a localized instance from the specified source and location. @@ -38,32 +37,19 @@ abstract class LocalizedObject * * @return static */ - static public function from($source, Locale $locale, array $options = []): LocalizedObject + static public function from(object $source, Locale $locale, array $options = []): LocalizedObject { return new static($source, $locale, $options); // @phpstan-ignore-line } - /** - * The object to localize. - * - * @var T - * @readonly - */ - public $target; - - /** - * @var array - */ - protected $options; - /** * @param T $target The object to localize. * @param array $options */ - public function __construct($target, Locale $locale, array $options = []) - { - $this->target = $target; - $this->locale = $locale; - $this->options = $options; + public function __construct( + public readonly object $target, + public readonly Locale $locale, + protected readonly array $options = [] + ) { } } diff --git a/lib/Number.php b/lib/Number.php index 08f3d43..7eacad3 100644 --- a/lib/Number.php +++ b/lib/Number.php @@ -26,9 +26,9 @@ final class Number /** * Returns the precision of a number. * - * @param numeric $number + * @param float|int|numeric-string $number */ - static public function precision_from($number): int + static public function precision_from(float|int|string $number): int { $number = (string) $number; $pos = strrpos($number, '.'); @@ -44,24 +44,24 @@ static public function precision_from($number): int /** * Returns a number rounded to the specified precision. * - * @param float|int $number + * @param float|int|numeric-string $number */ - static public function round_to($number, int $precision): float + static public function round_to(float|int|string $number, int $precision): float { - return round($number, $precision); + return round($number+0, $precision); } /** * Parses a number. * - * @param numeric $number + * @param float|int|numeric-string $number * * @return array{ 0: int, 1: string|null} * Where `0` is the integer part and `1` the fractional part. The fractional part is `null` if * `$number` has no decimal separator. The fractional part is returned as a string to preserve '03' from * '1.03'. */ - static public function parse($number, int $precision = null): array + static public function parse(float|int|string $number, int $precision = null): array { if ($precision === null) { @@ -78,12 +78,12 @@ static public function parse($number, int $precision = null): array } /** - * @param numeric $number + * @param float|int|numeric-string $number * @param int|null $c * - * @return numeric + * @return float|int|numeric-string */ - static public function expand_compact_decimal_exponent($number, int &$c = null) + static public function expand_compact_decimal_exponent(float|int|string $number, int &$c = null): float|int|string { $c = 0; diff --git a/lib/NumberFormatter.php b/lib/NumberFormatter.php index 7d5625d..79e8f1a 100644 --- a/lib/NumberFormatter.php +++ b/lib/NumberFormatter.php @@ -24,11 +24,16 @@ class NumberFormatter implements Formatter * Note, if the pattern contains '%', the number will be multiplied by 100 first. If the * pattern contains '‰', the number will be multiplied by 1000. * - * @param float|int $number The number to be formatted. - * @param string $pattern The pattern used to format the number. + * @param float|int|numeric-string $number + * The number to format. + * @param string|NumberPattern $pattern + * The pattern used to format the number. */ - public function __invoke($number, string $pattern, Symbols $symbols = null): string - { + public function __invoke( + float|int|string $number, + NumberPattern|string $pattern, + Symbols $symbols = null, + ): string { return $this->format($number, $pattern, $symbols); } @@ -38,11 +43,16 @@ public function __invoke($number, string $pattern, Symbols $symbols = null): str * Note, if the pattern contains '%', the number will be multiplied by 100 first. If the * pattern contains '‰', the number will be multiplied by 1000. * - * @param float|int $number The number to be formatted. - * @param string|NumberPattern $pattern The pattern used to format the number. + * @param float|int|numeric-string $number + * The number to format. + * @param string|NumberPattern $pattern + * The pattern used to format the number. */ - public function format($number, $pattern, Symbols $symbols = null): string - { + public function format( + float|int|string $number, + NumberPattern|string $pattern, + Symbols $symbols = null, + ): string { if (!$pattern instanceof NumberPattern) { $pattern = NumberPattern::from($pattern); diff --git a/lib/NumberPattern.php b/lib/NumberPattern.php index 12e49e9..13fe945 100644 --- a/lib/NumberPattern.php +++ b/lib/NumberPattern.php @@ -11,9 +11,7 @@ namespace ICanBoogie\CLDR; -use ICanBoogie\Accessor\AccessorTrait; use function abs; -use function array_key_exists; use function implode; use function ltrim; use function round; @@ -22,6 +20,7 @@ use function strlen; use function strpos; use function substr; + use const STR_PAD_LEFT; /** @@ -32,7 +31,7 @@ final class NumberPattern /** * @var NumberPattern[] */ - static private $instances = []; + static private array $instances = []; static public function from(string $pattern): NumberPattern { @@ -59,116 +58,42 @@ static public function from(string $pattern): NumberPattern } /** - * @var string - */ - private $pattern; - - /** - * Prefix to positive number. - * - * @var string - * @readonly - */ - public $positive_prefix; - - /** - * Suffix to positive number. - * - * @var string - * @readonly - */ - public $positive_suffix; - - /** - * Prefix to negative number. - * - * @var string - * @readonly - */ - public $negative_prefix; - - /** - * Suffix to negative number. - * - * @var string - * @readonly - */ - public $negative_suffix; - - /** - * 100 for percent, 1000 for per mille. - * - * @var int - * @readonly - */ - public $multiplier; - - /** - * The number of required digits after decimal point. The string is padded with zeros if there is not enough digits. - * `-1` means the decimal point should be dropped. - * - * @var int - * @readonly + * @param string $pattern + * @param string $positive_prefix + * Prefix to positive number. + * @param string $positive_suffix + * Suffix to positive number. + * @param string $negative_prefix + * Prefix to negative number. + * @param string $negative_suffix + * Suffix to negative number. + * @param int $multiplier + * 100 for percent, 1000 for per mille. + * @param int $decimal_digits + * The number of required digits after decimal point. The string is padded with zeros if there is not enough digits. + * `-1` means the decimal point should be dropped. + * @param int $max_decimal_digits + * The maximum number of digits after decimal point. Additional digits will be truncated. + * @param int $integer_digits + * The number of required digits before decimal point. The string is padded with zeros if there is not enough digits. + * @param int $group_size1 + * The primary grouping size. `0` means no grouping. + * @param int $group_size2 + * The secondary grouping size. `0` means no secondary grouping. */ - public $decimal_digits; - - /** - * The maximum number of digits after decimal point. Additional digits will be truncated. - * - * @var int - * @readonly - */ - public $max_decimal_digits; - - /** - * The number of required digits before decimal point. The string is padded with zeros if there is not enough - * digits. - * - * @var int - * @readonly - */ - public $integer_digits; - - /** - * The primary grouping size. `0` means no grouping. - * - * @var int - * @readonly - */ - public $group_size1; - - /** - * The secondary grouping size. `0` means no secondary grouping. - * - * @var int - * @readonly - */ - public $group_size2; - private function __construct( - string $pattern, - string $positive_prefix, - string $positive_suffix, - string $negative_prefix, - string $negative_suffix, - int $multiplier, - int $decimal_digits, - int $max_decimal_digits, - int $integer_digits, - int $group_size1, - int $group_size2 + public readonly string $pattern, + public readonly string $positive_prefix, + public readonly string $positive_suffix, + public readonly string $negative_prefix, + public readonly string $negative_suffix, + public readonly int $multiplier, + public readonly int $decimal_digits, + public readonly int $max_decimal_digits, + public readonly int $integer_digits, + public readonly int $group_size1, + public readonly int $group_size2 ) { - $this->pattern = $pattern; - $this->positive_prefix = $positive_prefix; - $this->positive_suffix = $positive_suffix; - $this->negative_prefix = $negative_prefix; - $this->negative_suffix = $negative_suffix; - $this->multiplier = $multiplier; - $this->decimal_digits = $decimal_digits; - $this->max_decimal_digits = $max_decimal_digits; - $this->integer_digits = $integer_digits; - $this->group_size1 = $group_size1; - $this->group_size2 = $group_size2; } public function __toString(): string @@ -179,12 +104,12 @@ public function __toString(): string /** * Parse a number according to the pattern and return its integer and decimal parts. * - * @param int|float $number + * @param float|int|numeric-string $number * * @return array{ 0: int, 1: string} * Where `0` is the integer part and `1` the decimal part. */ - public function parse_number($number): array + public function parse_number(float|int|string $number): array { $number = abs($number * $this->multiplier); @@ -230,10 +155,10 @@ public function format_integer_with_group(int $integer, string $group_symbol): s /** * Formats an integer with a decimal. * - * @param string|int $integer An integer, or a formatted integer as returned by - * {@link format_integer_with_group}. + * @param int|string $integer + * An integer, or a formatted integer as returned by {@link format_integer_with_group}. */ - public function format_integer_with_decimal($integer, string $decimal, string $decimal_symbol): string + public function format_integer_with_decimal(int|string $integer, string $decimal, string $decimal_symbol): string { if ($decimal === '0') { $decimal = ''; diff --git a/lib/NumberPatternParser.php b/lib/NumberPatternParser.php index d745e0a..a6b0245 100644 --- a/lib/NumberPatternParser.php +++ b/lib/NumberPatternParser.php @@ -28,7 +28,7 @@ final class NumberPatternParser /** * @var array */ - static private $initial_format = [ + static private array $initial_format = [ 'positive_prefix' => '', 'positive_suffix' => '', @@ -126,16 +126,13 @@ static private function parse_multiple_patterns(string &$pattern, array &$format */ static private function parse_multiplier(string $pattern, array &$format): void { - if (strpos($pattern, '%') !== false) + if (str_contains($pattern, '%')) { $format['multiplier'] = 100; } - else + elseif (str_contains($pattern, '‰')) { - if (strpos($pattern, '‰') !== false) - { - $format['multiplier'] = 1000; - } + $format['multiplier'] = 1000; } } diff --git a/lib/Numbers.php b/lib/Numbers.php index cbd5b42..d1abfdb 100644 --- a/lib/Numbers.php +++ b/lib/Numbers.php @@ -58,7 +58,6 @@ final class Numbers extends ArrayObject * @uses get_misc_patterns */ use AccessorTrait; - use LocalePropertyTrait; private function lazy_get_default_numbering_system(): string { @@ -134,10 +133,10 @@ private function get_misc_patterns(): array /** * @phpstan-ignore-next-line */ - public function __construct(Locale $locale, array $data) - { - $this->locale = $locale; - + public function __construct( + public readonly Locale $locale, + array $data + ) { parent::__construct($data); } } diff --git a/lib/Numbers/Symbols.php b/lib/Numbers/Symbols.php index dcb3cfd..596e5a7 100644 --- a/lib/Numbers/Symbols.php +++ b/lib/Numbers/Symbols.php @@ -59,130 +59,25 @@ public static function defaults(): self { static $defaults; - return $defaults ?? $defaults = self::from(self::DEFAULTS); + return $defaults ??= self::from(self::DEFAULTS); } - /** - * @readonly - * @var string - */ - public $decimal; - - /** - * @readonly - * @var string - */ - public $group; - - /** - * @readonly - * @var string - */ - public $list; - - /** - * @readonly - * @var string - */ - public $percentSign; - - /** - * @readonly - * @var string - */ - public $minusSign; - - /** - * @readonly - * @var string - */ - public $plusSign; - - /** - * @readonly - * @var string - */ - public $approximatelySign; - - /** - * @readonly - * @var string - */ - public $exponential; - - /** - * @readonly - * @var string - */ - public $superscriptingExponent; - - /** - * @readonly - * @var string - */ - public $perMille; - - /** - * @readonly - * @var string - */ - public $infinity; - - /** - * @readonly - * @var string - */ - public $nan; - - /** - * @readonly - * @var string - */ - public $currencyDecimal; - - /** - * @readonly - * @var string - */ - public $currencyGroup; - - /** - * @readonly - * @var string - */ - public $timeSeparator; - public function __construct( - string $decimal = '.', - string $group = ',', - string $list = ';', - string $percentSign = '%', - string $minusSign = '-', - string $plusSign = '+', - string $approximatelySign = '~', - string $exponential = 'E', - string $superscriptingExponent = '×', - string $perMille = '‰', - string $infinity = '∞', - string $nan = '☹', - string $currencyDecimal = '.', - string $currencyGroup = ',', - string $timeSeparator = ':' + public readonly string $decimal = '.', + public readonly string $group = ',', + public readonly string $list = ';', + public readonly string $percentSign = '%', + public readonly string $minusSign = '-', + public readonly string $plusSign = '+', + public readonly string $approximatelySign = '~', + public readonly string $exponential = 'E', + public readonly string $superscriptingExponent = '×', + public readonly string $perMille = '‰', + public readonly string $infinity = '∞', + public readonly string $nan = '☹', + public readonly string $currencyDecimal = '.', + public readonly string $currencyGroup = ',', + public readonly string $timeSeparator = ':' ) { - $this->decimal = $decimal; - $this->group = $group; - $this->list = $list; - $this->percentSign = $percentSign; - $this->minusSign = $minusSign; - $this->plusSign = $plusSign; - $this->approximatelySign = $approximatelySign; - $this->exponential = $exponential; - $this->superscriptingExponent = $superscriptingExponent; - $this->perMille = $perMille; - $this->infinity = $infinity; - $this->nan = $nan; - $this->currencyDecimal = $currencyDecimal; - $this->currencyGroup = $currencyGroup; - $this->timeSeparator = $timeSeparator; } } diff --git a/lib/Plurals.php b/lib/Plurals.php index 05ab523..8a9269f 100644 --- a/lib/Plurals.php +++ b/lib/Plurals.php @@ -57,11 +57,11 @@ final class Plurals extends ArrayObject private $samples = []; /** - * @param numeric $number + * @param float|int|numeric-string $number * * @return self::COUNT_* */ - public function rule_for($number, string $locale): string + public function rule_for(float|int|string $number, string $locale): string { $rules = $this->rule_instances_for($locale); @@ -89,8 +89,7 @@ public function rules_for(string $locale): array */ public function samples_for(string $locale): array { - return $this->samples[$locale] - ?? $this->samples[$locale] = $this->create_samples_for($locale); + return $this->samples[$locale] ??= $this->create_samples_for($locale); } /** @@ -98,8 +97,7 @@ public function samples_for(string $locale): array */ private function rule_instances_for(string $locale): array { - return $this->rules[$locale] - ?? $this->rules[$locale] = $this->create_rules_for($locale); + return $this->rules[$locale] ??= $this->create_rules_for($locale); } /** diff --git a/lib/Plurals/Operands.php b/lib/Plurals/Operands.php index 8d94efb..2b5c0ba 100644 --- a/lib/Plurals/Operands.php +++ b/lib/Plurals/Operands.php @@ -128,9 +128,9 @@ private function get_e(): int } /** - * @param numeric $number + * @param float|int|numeric-string $number */ - private function __construct($number) + private function __construct(float|int|string $number) { $number = $this->expand_compact_decimal_exponent($number); @@ -181,11 +181,11 @@ public function to_array(): array } /** - * @param numeric $number + * @param float|int|numeric-string $number * - * @return numeric + * @return float|int|numeric-string */ - private function expand_compact_decimal_exponent($number) + private function expand_compact_decimal_exponent(float|int|string $number): float|int|string { return Number::expand_compact_decimal_exponent($number, $this->e); } diff --git a/lib/Plurals/OperandsCache.php b/lib/Plurals/OperandsCache.php index a9bc564..3c690be 100644 --- a/lib/Plurals/OperandsCache.php +++ b/lib/Plurals/OperandsCache.php @@ -19,13 +19,13 @@ final class OperandsCache /** * @var array */ - static private $instances = []; + static private array $instances = []; /** - * @param numeric $number + * @param float|int|numeric-string $number * @param callable():Operands $new */ - static public function get($number, callable $new): Operands + static public function get(float|int|string $number, callable $new): Operands { $key = "number-$number"; diff --git a/lib/Plurals/Relation.php b/lib/Plurals/Relation.php index ef8dda5..b44c19a 100644 --- a/lib/Plurals/Relation.php +++ b/lib/Plurals/Relation.php @@ -147,33 +147,20 @@ static private function unwind_range(string $range): array } /** - * @param numeric $number + * @param float|int|numeric-string $number */ - static private function precision_from($number): int + static private function precision_from(float|int|string $number): int { return Number::precision_from($number); } - /** - * @var string|null - */ - private $x_expression; - - /** - * @var string - */ - private $conditions; - - private function __construct(?string $x_expression, string $conditions) - { - $this->x_expression = $x_expression; - $this->conditions = $conditions; + private function __construct( + private readonly ?string $x_expression, + private readonly string $conditions + ) { } - /** - * @return int|float|null - */ - public function resolve_x(Operands $operands) + public function resolve_x(Operands $operands): float|int|null { if ($this->x_expression === null) { diff --git a/lib/Plurals/RelationCache.php b/lib/Plurals/RelationCache.php index bf19c6a..2b8198f 100644 --- a/lib/Plurals/RelationCache.php +++ b/lib/Plurals/RelationCache.php @@ -20,18 +20,13 @@ final class RelationCache * @var array * Where _key_ is a relation statement and _value_ a {@link Relation}. */ - static private $instances = []; + static private array $instances = []; /** * @param callable():Relation $new */ static public function get(string $relation, callable $new): Relation { - if (isset(self::$instances[$relation])) - { - return self::$instances[$relation]; - } - - return self::$instances[$relation] = $new(); + return self::$instances[$relation] ??= $new(); } } diff --git a/lib/Plurals/Rule.php b/lib/Plurals/Rule.php index b4153dc..1b6f587 100644 --- a/lib/Plurals/Rule.php +++ b/lib/Plurals/Rule.php @@ -55,25 +55,20 @@ static private function extract_relations(string $rule): array }, explode(' or ', $rule)); } - /** - * @var Relation[][] - */ - private $relations; - /** * @param Relation[][] $relations */ - private function __construct(array $relations) - { - $this->relations = $relations; + private function __construct( + private readonly array $relations + ) { } /** * Whether a number matches the rule. * - * @param numeric $number + * @param float|int|numeric-string $number */ - public function validate($number): bool + public function validate(float|int|string $number): bool { $operands = Operands::from($number); diff --git a/lib/Plurals/RuleCache.php b/lib/Plurals/RuleCache.php index 09d5a1a..a2fcd04 100644 --- a/lib/Plurals/RuleCache.php +++ b/lib/Plurals/RuleCache.php @@ -20,18 +20,13 @@ final class RuleCache * @var array * Where _key_ is a rule statement and _value_ a {@link Rule}. */ - static private $instances = []; + static private array $instances = []; /** * @param callable():Rule $new */ static public function get(string $rule, callable $new): Rule { - if (isset(self::$instances[$rule])) - { - return self::$instances[$rule]; - } - - return self::$instances[$rule] = $new(); + return self::$instances[$rule] ??= $new(); } } diff --git a/lib/Plurals/Samples.php b/lib/Plurals/Samples.php index fad0a37..2ef803b 100644 --- a/lib/Plurals/Samples.php +++ b/lib/Plurals/Samples.php @@ -47,9 +47,10 @@ final class Samples implements IteratorAggregate static public function from(string $samples): Samples { - return SamplesCache::get($samples, static function () use ($samples): Samples { - return new self(self::parse_rules($samples)); - }); + return SamplesCache::get( + $samples, + static fn(): Samples => new self(self::parse_rules($samples)) + ); } /** @@ -103,24 +104,19 @@ static private function parse_samples(string $samples_string): array } /** - * @param numeric $number + * @param float|int|numeric-string $number */ - static private function precision_from($number): int + static private function precision_from(float|int|string $number): int { return Number::precision_from($number); } - /** - * @var array - */ - private $samples; - /** * @param array $samples */ - private function __construct(array $samples) - { - $this->samples = $samples; + private function __construct( + private readonly array $samples + ) { } /** diff --git a/lib/Plurals/SamplesCache.php b/lib/Plurals/SamplesCache.php index 772f656..804f928 100644 --- a/lib/Plurals/SamplesCache.php +++ b/lib/Plurals/SamplesCache.php @@ -20,18 +20,13 @@ final class SamplesCache * @var array * Where _key_ is a rule statement and _value_ a {@link Samples}. */ - static private $instances = []; + static private array $instances = []; /** * @param callable():Samples $new */ static public function get(string $samples, callable $new): Samples { - if (isset(self::$instances[$samples])) - { - return self::$instances[$samples]; - } - - return self::$instances[$samples] = $new(); + return self::$instances[$samples] ??= $new(); } } diff --git a/lib/Provider/CachedProvider.php b/lib/Provider/CachedProvider.php index f501cd0..03277a2 100644 --- a/lib/Provider/CachedProvider.php +++ b/lib/Provider/CachedProvider.php @@ -19,20 +19,10 @@ */ final class CachedProvider implements Provider { - /** - * @var Provider - */ - private $provider; - - /** - * @var Cache - */ - private $cache; - - public function __construct(Provider $provider, Cache $cache) - { - $this->provider = $provider; - $this->cache = $cache; + public function __construct( + private readonly Provider $provider, + private readonly Cache $cache + ) { } /** diff --git a/lib/Provider/WebProvider.php b/lib/Provider/WebProvider.php index 7efa5c2..79e314b 100644 --- a/lib/Provider/WebProvider.php +++ b/lib/Provider/WebProvider.php @@ -11,6 +11,7 @@ namespace ICanBoogie\CLDR\Provider; +use CurlHandle; use ICanBoogie\CLDR\GitHub\UrlResolver; use ICanBoogie\CLDR\Provider; use ICanBoogie\CLDR\ResourceNotFound; @@ -33,19 +34,11 @@ */ final class WebProvider implements Provider { - /** - * @var mixed - */ - private $connection; - - /** - * @var UrlResolver - */ - private $url_resolver; + private CurlHandle $connection; - public function __construct(UrlResolver $url_resolver = null) - { - $this->url_resolver = $url_resolver ?? new UrlResolver(); + public function __construct( + private readonly UrlResolver $url_resolver = new UrlResolver() + ) { } /** @@ -74,19 +67,13 @@ public function provide(string $path): array /** * Returns a reusable cURL connection. - * - * @return mixed */ - private function obtain_connection() + private function obtain_connection(): CurlHandle { - return $this->connection - ?? $this->connection = $this->create_connection(); + return $this->connection ??= $this->create_connection(); } - /** - * @return mixed - */ - private function create_connection() + private function create_connection(): CurlHandle { $connection = curl_init(); diff --git a/lib/Repository.php b/lib/Repository.php index 96dada1..aa6c67c 100644 --- a/lib/Repository.php +++ b/lib/Repository.php @@ -32,21 +32,28 @@ * var_dump($repository->territories['FR']); * * - * @property-read Provider $provider * @property-read LocaleCollection $locales + * @uses self::lazy_get_locales() * @property-read Supplemental $supplemental + * @uses self::lazy_get_supplemental() * @property-read TerritoryCollection $territories + * @uses self::lazy_get_territories() * @property-read CurrencyCollection $currencies + * @uses self::lazy_get_currencies() * @property-read NumberFormatter $number_formatter + * @uses self::lazy_get_number_formatter() * @property-read CurrencyFormatter $currency_formatter + * @uses self::lazy_get_currency_formatter() * @property-read ListFormatter $list_formatter + * @uses self::lazy_get_list_formatter() * @property-read Plurals $plurals + * @uses self::lazy_get_plurals() * @property-read string[] $available_locales + * @uses self::lazy_get_available_locales() */ final class Repository { /** - * @uses get_provider * @uses lazy_get_locales * @uses lazy_get_supplemental * @uses lazy_get_territories @@ -60,16 +67,6 @@ final class Repository */ use AccessorTrait; - /** - * @var Provider - */ - private $provider; - - private function get_provider(): Provider - { - return $this->provider; - } - private function lazy_get_locales(): LocaleCollection { return new LocaleCollection($this); @@ -121,9 +118,9 @@ private function lazy_get_available_locales(): array return $this->fetch('core/availableLocales', 'availableLocales/modern'); } - public function __construct(Provider $provider) - { - $this->provider = $provider; + public function __construct( + public readonly Provider $provider + ) { } /** @@ -155,25 +152,33 @@ public function fetch(string $path, string $data_path = null): array /** * Format a number with the specified pattern. * - * @param float|int $number The number to be formatted. + * Note, if the pattern contains '%', the number will be multiplied by 100 first. If the + * pattern contains '‰', the number will be multiplied by 1000. * - * @see NumberFormatter::format() + * @param float|int|numeric-string $number + * The number to format. + * @param string|NumberPattern $pattern + * The pattern used to format the number. */ - public function format_number($number, string $pattern, Symbols $symbols = null): string - { + public function format_number( + float|int|string $number, + NumberPattern|string $pattern, + Symbols $symbols = null, + ): string { return $this->number_formatter->format($number, $pattern, $symbols); } /** * Format a number with the specified pattern. * - * @param float|int $number The number to be formatted. + * @param float|int|numeric-string $number + * The number to format. * * @see CurrencyFormatter::format() */ public function format_currency( - $number, - string $pattern, + float|int|string $number, + NumberPattern|string $pattern, Symbols $symbols = null, string $currencySymbol = CurrencyFormatter::DEFAULT_CURRENCY_SYMBOL ): string { diff --git a/lib/RepositoryPropertyTrait.php b/lib/RepositoryPropertyTrait.php deleted file mode 100644 index 713564b..0000000 --- a/lib/RepositoryPropertyTrait.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ICanBoogie\CLDR; - -/** - * A trait for classes implementing the `repository` property. - * - * @property-read Repository $repository - */ -trait RepositoryPropertyTrait -{ - /** - * @var Repository - */ - private $repository; - - private function get_repository(): Repository - { - return $this->repository; - } -} diff --git a/lib/Supplemental.php b/lib/Supplemental.php index 36c711f..b2e670c 100644 --- a/lib/Supplemental.php +++ b/lib/Supplemental.php @@ -11,6 +11,7 @@ namespace ICanBoogie\CLDR; +use ICanBoogie\Accessor\AccessorTrait; use ICanBoogie\CLDR\Supplemental\CurrencyData; /** @@ -27,9 +28,12 @@ * * * @property-read CurrencyData $currency_data + * @uses self::lazy_get_currency_data() */ final class Supplemental extends AbstractSectionCollection { + use AccessorTrait; + /** * Where _key_ is a property, matching a CLDR filename, and _value_ is an array path under "supplemental". */ @@ -66,9 +70,6 @@ final class Supplemental extends AbstractSectionCollection ]; - /** - * @uses lazy_get_currency_data - */ protected function lazy_get_currency_data(): CurrencyData { /* @phpstan-ignore-next-line */ diff --git a/lib/Supplemental/CurrencyData.php b/lib/Supplemental/CurrencyData.php index b8fcffb..dcc98d2 100644 --- a/lib/Supplemental/CurrencyData.php +++ b/lib/Supplemental/CurrencyData.php @@ -20,13 +20,6 @@ final class CurrencyData { private const FRACTION_FALLBACK = 'DEFAULT'; - /** - * @var array{ fractions: array, region: array } - * - * @phpstan-ignore-next-line - */ - private $data; - /** * @param array{ fractions: array, region: array } $data * @@ -34,26 +27,22 @@ final class CurrencyData * * @phpstan-ignore-next-line */ - public function __construct(array $data) - { - $this->data = $data; + public function __construct( + private readonly array $data + ) { } /** * @var array */ - private $fractions; + private array $fractions; public function fraction_for(string $currency_code): Fraction { - return $this->fractions[$currency_code] - ?? $this->fractions[$currency_code] = $this->build_fraction($currency_code); + return $this->fractions[$currency_code] ??= $this->build_fraction($currency_code); } - /** - * @var Fraction|null - */ - private $default_fraction; + private Fraction $default_fraction; private function build_fraction(string $currency_code): Fraction { @@ -61,8 +50,7 @@ private function build_fraction(string $currency_code): Fraction if (!$data) { - return $this->default_fraction - ?? $this->default_fraction = $this->build_fraction(self::FRACTION_FALLBACK); + return $this->default_fraction ??= $this->build_fraction(self::FRACTION_FALLBACK); } return Fraction::from($data); diff --git a/lib/Supplemental/Fraction.php b/lib/Supplemental/Fraction.php index b9b9c86..857b240 100644 --- a/lib/Supplemental/Fraction.php +++ b/lib/Supplemental/Fraction.php @@ -35,47 +35,20 @@ static public function from(array $data): self } /** - * The minimum and maximum number of decimal digits normally formatted. - * - * @readonly - * @var int + * @param int $digits + * The minimum and maximum number of decimal digits normally formatted. + * @param int $rounding + * The rounding increment, in units of 10^-digits. + * @param int $cash_digits + * The number of decimal digits to be used when formatting quantities used in cash transactions. + * @param int $cash_rounding + * The cash rounding increment, in units of 10^cashDigits. */ - public $digits; - - /** - * The rounding increment, in units of 10^-digits. - * - * @readonly - * @var int - */ - public $rounding; - - /** - * The number of decimal digits to be used when formatting quantities used in cash transactions. - * - * @readonly - * @var int - */ - public $cash_digits; - - /** - * The cash rounding increment, in units of 10^cashDigits. - * - * @readonly - * @var int - */ - public $cash_rounding; - private function __construct( - int $digits, - int $rounding, - int $cash_digits, - int $cash_rounding - ) - { - $this->digits = $digits; - $this->rounding = $rounding; - $this->cash_digits = $cash_digits; - $this->cash_rounding = $cash_rounding; + public readonly int $digits, + public readonly int $rounding, + public readonly int $cash_digits, + public readonly int $cash_rounding + ) { } } diff --git a/lib/Territory.php b/lib/Territory.php index 414ab42..cc1930a 100644 --- a/lib/Territory.php +++ b/lib/Territory.php @@ -21,7 +21,6 @@ use function in_array; use function key; use function strlen; -use function strpos; use function substr; /** @@ -55,8 +54,6 @@ final class Territory * @uses get_population */ use AccessorTrait; - use RepositoryPropertyTrait; - use CodePropertyTrait; /** * @return array @@ -136,20 +133,24 @@ private function get_population(): int return (int) $info['_population']; } - public function __construct(Repository $repository, string $code) - { - $this->repository = $repository; - $this->code = $code; - + public function __construct( + public readonly Repository $repository, + public readonly string $code + ) { $repository->territories->assert_defined($code); } + public function __toString(): string + { + return $this->code; + } + /** * @return mixed */ public function __get(string $property) { - if (strpos($property, 'name_as_') === 0) + if (str_starts_with($property, 'name_as_')) { $locale_code = substr($property, strlen('name_as_')); $locale_code = strtr($locale_code, '_', '-'); diff --git a/lib/TerritoryCollection.php b/lib/TerritoryCollection.php index d8ccd17..fea2fae 100644 --- a/lib/TerritoryCollection.php +++ b/lib/TerritoryCollection.php @@ -11,8 +11,6 @@ namespace ICanBoogie\CLDR; -use ICanBoogie\Accessor\AccessorTrait; - /** * Representation of a territory collection. * @@ -31,16 +29,9 @@ */ final class TerritoryCollection extends AbstractCollection { - /** - * @uses get_repository - */ - use AccessorTrait; - use RepositoryPropertyTrait; - - public function __construct(Repository $repository) - { - $this->repository = $repository; - + public function __construct( + public readonly Repository $repository + ) { parent::__construct(function (string $territory_code): Territory { $this->assert_defined($territory_code); diff --git a/lib/TerritoryNotDefined.php b/lib/TerritoryNotDefined.php index 5d0b2df..227c6dc 100644 --- a/lib/TerritoryNotDefined.php +++ b/lib/TerritoryNotDefined.php @@ -11,40 +11,24 @@ namespace ICanBoogie\CLDR; -use ICanBoogie\Accessor\AccessorTrait; use InvalidArgumentException; use Throwable; /** * Exception thrown when a territory is not defined. - * - * @property-read string $territory_code The ISO code of the territory. */ final class TerritoryNotDefined extends InvalidArgumentException implements Exception { /** - * @uses get_territory_code + * @param string $territory_code + * The ISO code of the territory. */ - use AccessorTrait; - - /** - * @var string - */ - private $territory_code; - - private function get_territory_code(): string - { - return $this->territory_code; - } - - public function __construct(string $territory_code, string $message = null, Throwable $previous = null) - { - $this->territory_code = $territory_code; - - if (!$message) - { - $message = "Territory not defined for code: $territory_code."; - } + public function __construct( + public readonly string $territory_code, + string $message = null, + Throwable $previous = null + ) { + $message ??= "Territory not defined for code: $territory_code."; parent::__construct($message, 0, $previous); } diff --git a/lib/Units.php b/lib/Units.php index 6dca2b8..f2a7f14 100644 --- a/lib/Units.php +++ b/lib/Units.php @@ -16,6 +16,7 @@ use ICanBoogie\CLDR\Units\NumberWithUnit; use ICanBoogie\CLDR\Units\Sequence; use ICanBoogie\CLDR\Units\Unit; +use ICanBoogie\PropertyNotDefined; use LogicException; use function count; @@ -24,8 +25,6 @@ use function strtr; /** - * @property-read Sequence $sequence - * * @property-read Unit $acceleration_g_force * @property-read Unit $acceleration_meter_per_second_squared * @property-read Unit $angle_arc_minute @@ -272,12 +271,6 @@ */ class Units { - /** - * @uses get_sequence - */ - use AccessorTrait; - use LocalePropertyTrait; - public const LENGTH_LONG = 'long'; public const LENGTH_SHORT = 'short'; public const LENGTH_NARROW = 'narrow'; @@ -304,26 +297,18 @@ static private function length_to_unit_type(string $length): string } /** - * @var array * @phpstan-ignore-next-line */ - private $data; + private readonly array $data; - /** - * @var Plurals|null - */ - private $plurals; + public readonly Sequence $sequence; - private function get_sequence(): Sequence - { - return new Sequence($this); - } - - public function __construct(Locale $locale) - { - $this->locale = $locale; + public function __construct( + public readonly Locale $locale + ) { /** @phpstan-ignore-next-line */ $this->data = $locale['units']; + $this->sequence = new Sequence($this); } public function __call(string $name, array $arguments): NumberWithUnit @@ -359,7 +344,7 @@ public function __get(string $property) return $this->$property = new Unit($this, $unit); } - return $this->accessor_get($property); + throw new PropertyNotDefined([ $property, $this ]); } /** @@ -381,9 +366,9 @@ public function name_for(string $unit, string $length = self::DEFAULT_LENGTH): s } /** - * @param int|float $number + * @param float|int|numeric-string $number */ - public function format($number, string $unit, string $length = self::DEFAULT_LENGTH): string + public function format(float|int|string $number, string $unit, string $length = self::DEFAULT_LENGTH): string { $pattern = $this->pattern_for_unit($unit, $number, $length); $number = $this->ensure_number_if_formatted($number); @@ -394,12 +379,12 @@ public function format($number, string $unit, string $length = self::DEFAULT_LEN /** * Format a combination of units is X per Y, such as _miles per hour_ or _liters per second_. * - * @param int|float $number + * @param float|int|numeric-string $number * * @see https://www.unicode.org/reports/tr35/tr35-66/tr35-general.html#compound-units */ public function format_compound( - $number, + float|int|string $number, string $number_unit, string $per_unit, string $length = self::DEFAULT_LENGTH @@ -450,9 +435,9 @@ public function format_sequence(array $units_and_numbers, string $length = self: } /** - * @param int|float $number + * @param float|int|numeric-string $number */ - private function pattern_for_unit(string $unit, $number, string $length): string + private function pattern_for_unit(string $unit, float|int|string $number, string $length): string { $this->assert_is_unit($unit); @@ -462,9 +447,9 @@ private function pattern_for_unit(string $unit, $number, string $length): string } /** - * @param int|float $number + * @param float|int|numeric-string $number */ - private function pattern_for_denominator(string $unit, $number, string $length): string + private function pattern_for_denominator(string $unit, float|int|string $number, string $length): string { $pattern = $this->pattern_for_unit($unit, $number, $length); @@ -476,20 +461,22 @@ private function pattern_for_combination(string $length): string return $this->data[$length]['per']['compoundUnitPattern']; } + private Plurals $plurals; + /** - * @param int|float $number + * @param float|int|numeric-string $number */ - private function count_for($number): string + private function count_for(float|int|string $number): string { - $plurals = $this->plurals ?? $this->plurals = $this->locale->repository->plurals; + $plurals = $this->plurals ??= $this->locale->repository->plurals; return $plurals->rule_for($number, $this->locale->language); } /** - * @param numeric $number + * @param float|int|numeric-string $number */ - private function ensure_number_if_formatted($number): string + private function ensure_number_if_formatted(float|int|string $number): string { if (is_string($number)) { diff --git a/lib/Units/NumberPerUnit.php b/lib/Units/NumberPerUnit.php index 4d6e0a6..ed350e9 100644 --- a/lib/Units/NumberPerUnit.php +++ b/lib/Units/NumberPerUnit.php @@ -31,34 +31,14 @@ final class NumberPerUnit use AccessorTrait; /** - * @var float|int + * @param float|int|numeric-string $number */ - private $number; - - /** - * @var string - */ - private $number_unit; - - /** - * @var string - */ - private $per_unit; - - /** - * @var Units - */ - private $units; - - /** - * @param float|int $number - */ - public function __construct($number, string $number_unit, string $per_unit, Units $units) - { - $this->number = $number; - $this->number_unit = $number_unit; - $this->per_unit = $per_unit; - $this->units = $units; + public function __construct( + private readonly float|int|string $number, + private readonly string $number_unit, + private readonly string $per_unit, + private readonly Units $units + ) { } public function __toString(): string @@ -86,6 +66,11 @@ private function get_as_narrow(): string */ private function as(string $length): string { - return $this->units->format_compound($this->number, $this->number_unit, $this->per_unit, $length); + return $this->units->format_compound( + $this->number, + $this->number_unit, + $this->per_unit, + $length + ); } } diff --git a/lib/Units/NumberWithUnit.php b/lib/Units/NumberWithUnit.php index 01b057b..d44e013 100644 --- a/lib/Units/NumberWithUnit.php +++ b/lib/Units/NumberWithUnit.php @@ -31,28 +31,13 @@ final class NumberWithUnit use AccessorTrait; /** - * @var float|int + * @param float|int|numeric-string $number */ - private $number; - - /** - * @var string - */ - private $unit; - - /** - * @var Units - */ - private $units; - - /** - * @param float|int $number - */ - public function __construct($number, string $unit, Units $units) - { - $this->number = $number; - $this->unit = $unit; - $this->units = $units; + public function __construct( + private readonly float|int|string $number, + private readonly string $unit, + private readonly Units $units + ) { } public function __toString(): string @@ -60,10 +45,7 @@ public function __toString(): string return $this->as(Units::DEFAULT_LENGTH); } - /** - * @param string|Unit $unit - */ - public function per($unit): NumberPerUnit + public function per(Unit|string $unit): NumberPerUnit { return new NumberPerUnit($this->number, $this->unit, $unit, $this->units); } diff --git a/lib/Units/Sequence.php b/lib/Units/Sequence.php index 10e0a38..65f478a 100644 --- a/lib/Units/Sequence.php +++ b/lib/Units/Sequence.php @@ -156,19 +156,14 @@ final class Sequence */ use AccessorTrait; - /** - * @var Units - */ - private $units; - /** * @var array */ - private $sequence = []; + private array $sequence = []; - public function __construct(Units $units) - { - $this->units = $units; + public function __construct( + private readonly Units $units + ) { } public function __call(string $name, array $arguments): self diff --git a/lib/Units/Unit.php b/lib/Units/Unit.php index d368eb5..5aedbfe 100644 --- a/lib/Units/Unit.php +++ b/lib/Units/Unit.php @@ -32,16 +32,6 @@ final class Unit */ use AccessorTrait; - /** - * @var Units - */ - private $units; - - /** - * @var string - */ - private $unit; - private function get_name(): string { return $this->long_name; @@ -62,10 +52,10 @@ private function get_narrow_name(): string return $this->name_for(Units::LENGTH_NARROW); } - public function __construct(Units $units, string $unit) - { - $this->units = $units; - $this->unit = $unit; + public function __construct( + private readonly Units $units, + private readonly string $unit + ) { } public function __toString(): string diff --git a/phpstan.neon b/phpstan.neon index d9d8132..d3e65ce 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,6 +5,5 @@ parameters: scanFiles: - tests/bootstrap.php ignoreErrors: - - '#.* is not writable.#' - '#get_.* is unused.#' - '#__call\(\) has parameter \$arguments#' diff --git a/phpunit.xml b/phpunit.xml index 88afba6..d1012ce 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,17 +1,17 @@ - + ./tests - - - - ./lib - - + + + ./lib + + diff --git a/tests/CalendarCollectionTest.php b/tests/CalendarCollectionTest.php index b33a165..3439663 100644 --- a/tests/CalendarCollectionTest.php +++ b/tests/CalendarCollectionTest.php @@ -54,7 +54,7 @@ public function test_get(string $calendar_id): void $this->assertInstanceOf(Calendar::class, $calendar); } - public function provide_test_get(): array + public static function provide_test_get(): array { return [ diff --git a/tests/CalendarTest.php b/tests/CalendarTest.php index 1e2971b..00bb876 100644 --- a/tests/CalendarTest.php +++ b/tests/CalendarTest.php @@ -41,7 +41,7 @@ public function test_property_instanceof(string $property, string $expected): vo $this->assertSame($instance, self::$calendar->$property); } - public function provider_test_property_instanceof(): array + public static function provider_test_property_instanceof(): array { return [ @@ -67,7 +67,7 @@ public function test_access(string $key): void $this->assertTrue(self::$calendar->offsetExists($key)); } - public function provide_test_access(): array + public static function provide_test_access(): array { return [ @@ -99,7 +99,7 @@ public function test_date_patterns_shortcuts(string $property, string $path): vo $this->assertEquals(self::$calendar->$property, $expected); } - public function provide_test_date_patterns_shortcuts(): array + public static function provide_test_date_patterns_shortcuts(): array { return [ diff --git a/tests/CodePropertyTraitTest.php b/tests/CodePropertyTraitTest.php deleted file mode 100644 index 0698447..0000000 --- a/tests/CodePropertyTraitTest.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ICanBoogie\CLDR; - -use ICanBoogie\CLDR\CodePropertyTraitTest\A; -use PHPUnit\Framework\TestCase; - -final class CodePropertyTraitTest extends TestCase -{ - public function test_get_code(): void - { - $expected = 'fr'; - $a = new A($expected); - $this->assertSame($expected, $a->code); - } - - public function test_to_string(): void - { - $expected = 'fr'; - $a = new A($expected); - $this->assertSame($expected, (string) $a); - } -} diff --git a/tests/CodePropertyTraitTest/A.php b/tests/CodePropertyTraitTest/A.php deleted file mode 100644 index 3bf71f3..0000000 --- a/tests/CodePropertyTraitTest/A.php +++ /dev/null @@ -1,17 +0,0 @@ -code = $code; - } -} diff --git a/tests/ContextTransformsTest.php b/tests/ContextTransformsTest.php index c3461fb..2acc82a 100644 --- a/tests/ContextTransformsTest.php +++ b/tests/ContextTransformsTest.php @@ -30,7 +30,7 @@ public function test_transform( $this->assertSame($expected, (new ContextTransforms($rules))->transform($str, $usage, $type)); } - public function provide_test_transform(): array + public static function provide_test_transform(): array { return [ diff --git a/tests/CurrencyNotDefinedTest.php b/tests/CurrencyNotDefinedTest.php index d7e5d41..ea9904c 100644 --- a/tests/CurrencyNotDefinedTest.php +++ b/tests/CurrencyNotDefinedTest.php @@ -28,7 +28,7 @@ public function test_instance(string $currency_code, ?string $message, string $e $this->assertSame($previous, $sut->getPrevious()); } - public function provide_instance(): array + public static function provide_instance(): array { $currency_code = 'EUR'; $previous = new Exception; diff --git a/tests/CurrencyTest.php b/tests/CurrencyTest.php index 425a090..a3af8ca 100644 --- a/tests/CurrencyTest.php +++ b/tests/CurrencyTest.php @@ -38,7 +38,7 @@ public function test_fraction_properties(string $code, string $property, int $ex /** * @phpstan-ignore-next-line */ - public function provide_fraction_properties(): array + public static function provide_fraction_properties(): array { return [ diff --git a/tests/DateFormatterTest.php b/tests/DateFormatterTest.php index 804381c..e68f426 100644 --- a/tests/DateFormatterTest.php +++ b/tests/DateFormatterTest.php @@ -36,7 +36,7 @@ public function test_format(string $locale, string $datetime, string $pattern, s $this->assertEquals($expected, self::$formatters[$locale]->format($datetime, $pattern)); } - public function provide_test_format(): array + public static function provide_test_format(): array { return [ diff --git a/tests/DateTimeAccessorTest.php b/tests/DateTimeAccessorTest.php index 49452bc..9c65659 100644 --- a/tests/DateTimeAccessorTest.php +++ b/tests/DateTimeAccessorTest.php @@ -26,7 +26,7 @@ public function test_properties(string $property, int $expected): void $this->assertSame($expected, (new DateTimeAccessor($datetime))->$property); } - public function provide_test_properties(): array + public static function provide_test_properties(): array { return [ diff --git a/tests/DateTimeFormatterTest.php b/tests/DateTimeFormatterTest.php index d659f35..17d6bbc 100644 --- a/tests/DateTimeFormatterTest.php +++ b/tests/DateTimeFormatterTest.php @@ -47,7 +47,7 @@ public function test_format(string $locale_id, string $datetime, string $format, $this->assertSame($expected, $formatter($datetime, $format)); } - public function provide_test_format(): array + public static function provide_test_format(): array { return [ @@ -518,7 +518,7 @@ public function test_format_with_skeleton(string $skeleton, string $pattern, str $this->assertEquals($expected_result, $result); } - public function provide_test_format_with_skeleton(): array + public static function provide_test_format_with_skeleton(): array { return [ diff --git a/tests/GitHub/UrlResolverTest.php b/tests/GitHub/UrlResolverTest.php index 1148ca9..aa67cdc 100644 --- a/tests/GitHub/UrlResolverTest.php +++ b/tests/GitHub/UrlResolverTest.php @@ -28,7 +28,7 @@ public function test_resolve(string $path, string $expected): void /** * @phpstan-ignore-next-line */ - public function provide_resolve(): array + public static function provide_resolve(): array { return [ diff --git a/tests/ListFormatterTest.php b/tests/ListFormatterTest.php index f366979..d41cd88 100644 --- a/tests/ListFormatterTest.php +++ b/tests/ListFormatterTest.php @@ -25,7 +25,7 @@ public function test_format(array $list, ListPattern $list_pattern, string $expe $this->assertSame($expected, $formatter($list, $list_pattern)); } - public function provide_test_format(): array + public static function provide_test_format(): array { $lp1 = ListPattern::from([ diff --git a/tests/LocalePropertyTraitTest.php b/tests/LocalePropertyTraitTest.php deleted file mode 100644 index 64ff0a6..0000000 --- a/tests/LocalePropertyTraitTest.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ICanBoogie\CLDR; - -use ICanBoogie\CLDR\LocalePropertyTraitTest\A; -use PHPUnit\Framework\TestCase; - -final class LocalePropertyTraitTest extends TestCase -{ - public function test_get_repository(): void - { - $locale = get_repository()->locales['fr']; - $a = new A($locale); - $this->assertSame($locale, $a->locale); - } -} diff --git a/tests/LocalePropertyTraitTest/A.php b/tests/LocalePropertyTraitTest/A.php deleted file mode 100644 index fd625a9..0000000 --- a/tests/LocalePropertyTraitTest/A.php +++ /dev/null @@ -1,18 +0,0 @@ -locale = $locale; - } -} diff --git a/tests/LocaleTest.php b/tests/LocaleTest.php index 32cd12c..c37a70c 100644 --- a/tests/LocaleTest.php +++ b/tests/LocaleTest.php @@ -46,7 +46,7 @@ public function test_get_language(string $locale_code, string $expected): void /** * @phpstan-ignore-next-line */ - public function provide_test_get_language(): array + public static function provide_test_get_language(): array { return [ @@ -71,7 +71,7 @@ public function test_properties_instanceof(string $property, string $expected): /** * @phpstan-ignore-next-line */ - public function provide_test_properties_instanceof(): array + public static function provide_test_properties_instanceof(): array { return [ @@ -101,7 +101,7 @@ public function test_sections(string $section, string $key): void /** * @phpstan-ignore-next-line */ - public function provide_test_sections(): array + public static function provide_test_sections(): array { return [ @@ -153,7 +153,7 @@ public function test_localize(string $expected, $source): void /** * @phpstan-ignore-next-line */ - public function provide_test_localize(): array + public static function provide_test_localize(): array { return [ diff --git a/tests/LocaleTest/LocalizableSample.php b/tests/LocaleTest/LocalizableSample.php index 1fc40b3..7fb8eaf 100644 --- a/tests/LocaleTest/LocalizableSample.php +++ b/tests/LocaleTest/LocalizableSample.php @@ -8,7 +8,7 @@ class LocalizableSample implements Localizable { - static public function localize($source, Locale $locale, array $options = []): LocalizedObject + static public function localize(object $source, Locale $locale, array $options = []): LocalizedObject { return new LocalizedLocalizableSample($source, $locale, $options); } diff --git a/tests/LocalizedCurrencyFormatterTest.php b/tests/LocalizedCurrencyFormatterTest.php index abca2eb..bb7640d 100644 --- a/tests/LocalizedCurrencyFormatterTest.php +++ b/tests/LocalizedCurrencyFormatterTest.php @@ -49,7 +49,7 @@ public function test_format(string $currency_code, string $locale_code, $number, $this->assertStringSame($expected, $formatter($number, $currency_code)); } - public function provide_test_format(): array + public static function provide_test_format(): array { $s1 = Spaces::NARROW_NO_BREAK_SPACE; $s2 = Spaces::NO_BREAK_SPACE; @@ -89,7 +89,7 @@ public function test_format_accounting(string $currency_code, string $locale_cod ); } - public function provide_test_format_accounting(): array + public static function provide_test_format_accounting(): array { $s1 = Spaces::NARROW_NO_BREAK_SPACE; $s2 = Spaces::NO_BREAK_SPACE; diff --git a/tests/LocalizedCurrencyTest.php b/tests/LocalizedCurrencyTest.php index 923ca83..39f70f9 100644 --- a/tests/LocalizedCurrencyTest.php +++ b/tests/LocalizedCurrencyTest.php @@ -69,7 +69,7 @@ public function test_format(string $currency_code, string $locale_code, $number, $this->assertEquals($expected, $localized->format($number)); } - public function provide_test_format(): array + public static function provide_test_format(): array { return [ @@ -96,7 +96,7 @@ public function test_format_accounting(string $currency_code, string $locale_cod $this->assertStringSame($expected, $localized->format($number, LocalizedCurrencyFormatter::PATTERN_ACCOUNTING)); } - public function provide_test_format_accounting(): array + public static function provide_test_format_accounting(): array { $s1 = Spaces::NARROW_NO_BREAK_SPACE; $s2 = Spaces::NO_BREAK_SPACE; diff --git a/tests/LocalizedDateTimeTest.php b/tests/LocalizedDateTimeTest.php index 09311c4..300a44b 100644 --- a/tests/LocalizedDateTimeTest.php +++ b/tests/LocalizedDateTimeTest.php @@ -70,7 +70,7 @@ public function test_as(string $locale, string $as, string $expected): void $this->assertEquals($expected, self::$localized_dates[$locale]->$method()); } - public function provide_test_as(): array + public static function provide_test_as(): array { return [ diff --git a/tests/LocalizedListFormatterTest.php b/tests/LocalizedListFormatterTest.php index 8e60567..27bbfb4 100644 --- a/tests/LocalizedListFormatterTest.php +++ b/tests/LocalizedListFormatterTest.php @@ -24,7 +24,7 @@ public function test_format(array $list, string $type, string $locale_code, stri $this->assertSame($expected, $lp($list, $type)); } - public function provide_test_format(): array + public static function provide_test_format(): array { $sd = LocalizedListFormatter::TYPE_STANDARD; $st = LocalizedListFormatter::TYPE_UNIT_SHORT; diff --git a/tests/LocalizedLocaleTest.php b/tests/LocalizedLocaleTest.php index 7e48c7b..15128cc 100644 --- a/tests/LocalizedLocaleTest.php +++ b/tests/LocalizedLocaleTest.php @@ -26,7 +26,7 @@ public function test_get_name(string $locale_code, string $code, string $expecte $this->assertEquals($expected, $localized->name); } - public function provide_test_get_name(): array + public static function provide_test_get_name(): array { return [ diff --git a/tests/LocalizedNumberFormatterTest.php b/tests/LocalizedNumberFormatterTest.php index de07e37..d8ba574 100644 --- a/tests/LocalizedNumberFormatterTest.php +++ b/tests/LocalizedNumberFormatterTest.php @@ -28,7 +28,7 @@ public function test_format(string $locale_code, $number, ?string $pattern, stri $this->assertSame($expected, $localized->format($number, $pattern)); } - public function provide_test_format(): array + public static function provide_test_format(): array { return [ diff --git a/tests/LocalizedTerritoryTest.php b/tests/LocalizedTerritoryTest.php index 56a2283..f625265 100644 --- a/tests/LocalizedTerritoryTest.php +++ b/tests/LocalizedTerritoryTest.php @@ -26,7 +26,7 @@ public function test_get_name(string $locale_code, string $territory_code, strin $this->assertEquals($expected, $localized->name); } - public function provide_test_get_name(): array + public static function provide_test_get_name(): array { return [ diff --git a/tests/NumberFormatterTest.php b/tests/NumberFormatterTest.php index d5cd512..c6d4ac7 100644 --- a/tests/NumberFormatterTest.php +++ b/tests/NumberFormatterTest.php @@ -27,7 +27,7 @@ public function test_format(string $locale_code, $number, string $pattern, strin $this->assertSame($expected, $formatter->format($number, $pattern, $symbols)); } - public function provide_test_format(): array + public static function provide_test_format(): array { return [ diff --git a/tests/NumberPatternTest.php b/tests/NumberPatternTest.php index 2cb31ab..7df41a2 100644 --- a/tests/NumberPatternTest.php +++ b/tests/NumberPatternTest.php @@ -30,7 +30,7 @@ public function test_properties(string $pattern, array $properties): void } } - public function provide_test_properties(): array + public static function provide_test_properties(): array { $default = [ @@ -126,7 +126,7 @@ public function test_format_integer_with_decimal(string $pattern, int $integer, $this->assertSame($expected, $instance->format_integer_with_decimal($integer, $decimal, '/')); } - public function provide_test_format_integer_with_decimal(): array + public static function provide_test_format_integer_with_decimal(): array { return [ diff --git a/tests/NumberTest.php b/tests/NumberTest.php index f8d963e..bcb67cd 100644 --- a/tests/NumberTest.php +++ b/tests/NumberTest.php @@ -38,7 +38,7 @@ public function test_round_to($number, int $precision, $expected): void $this->assertEquals($expected, Number::round_to($number, $precision)); } - public function provide_test_round_to(): array + public static function provide_test_round_to(): array { return [ @@ -64,7 +64,7 @@ public function test_parse($number, int $precision, array $expected): void $this->assertSame($expected, Number::parse($number, $precision)); } - public function provide_test_parse(): array + public static function provide_test_parse(): array { return [ diff --git a/tests/NumbersTest.php b/tests/NumbersTest.php index 5f855ad..e3bd361 100644 --- a/tests/NumbersTest.php +++ b/tests/NumbersTest.php @@ -31,7 +31,7 @@ public function test_shortcuts(string $locale_code, string $property, string $of /** * @phpstan-ignore-next-line */ - public function provide_test_shortcuts(): array + public static function provide_test_shortcuts(): array { return [ @@ -59,7 +59,7 @@ public function test_symbols(string $locale_code, Symbols $expected): void /** * @phpstan-ignore-next-line */ - public function provide_symbols(): array + public static function provide_symbols(): array { return [ @@ -132,7 +132,7 @@ public function test_decimal_width_shortcuts(string $locale_code, string $proper /** * @phpstan-ignore-next-line */ - public function provide_test_decimal_width_shortcuts(): array + public static function provide_test_decimal_width_shortcuts(): array { return [ @@ -156,7 +156,7 @@ public function test_get_decimal_format(string $locale_code, string $expected): /** * @phpstan-ignore-next-line */ - public function provide_test_get_decimal_format(): array + public static function provide_test_get_decimal_format(): array { return [ diff --git a/tests/Plurals/OperandsTest.php b/tests/Plurals/OperandsTest.php index f8c8ccc..041e599 100644 --- a/tests/Plurals/OperandsTest.php +++ b/tests/Plurals/OperandsTest.php @@ -37,7 +37,7 @@ public function test_cases(string $number, array $expected): void /** * @see https://www.unicode.org/reports/tr35/tr35-66/tr35-numbers.html#table-plural-operand-examples */ - public function provide_test_cases(): array + public static function provide_test_cases(): array { return [ diff --git a/tests/Plurals/RelationTest.php b/tests/Plurals/RelationTest.php index 354914c..258eb43 100644 --- a/tests/Plurals/RelationTest.php +++ b/tests/Plurals/RelationTest.php @@ -30,7 +30,7 @@ public function test_cases(string $relation, $number, bool $expected): void $this->assertSame($expected, Relation::from($relation)->evaluate($operands)); } - public function provide_test_cases(): array + public static function provide_test_cases(): array { return [ diff --git a/tests/Plurals/RuleTest.php b/tests/Plurals/RuleTest.php index 578c01c..bacb5f0 100644 --- a/tests/Plurals/RuleTest.php +++ b/tests/Plurals/RuleTest.php @@ -31,7 +31,7 @@ public function test_cases(string $rule, $number, bool $expected): void /** * @see https://github.com/unicode-org/cldr-json/blob/41.0.0/cldr-json/cldr-core/supplemental/plurals.json */ - public function provide_test_cases(): array + public static function provide_test_cases(): array { $r1 = "n = 0 or n != 1 and n % 100 = 1..19"; $r2 = "n % 10 = 2..4 and n % 100 != 12..14"; diff --git a/tests/Plurals/SamplesTest.php b/tests/Plurals/SamplesTest.php index 1cd5259..28d6c12 100644 --- a/tests/Plurals/SamplesTest.php +++ b/tests/Plurals/SamplesTest.php @@ -35,7 +35,7 @@ public function test_samples(string $rules, array $expected): void $this->assertSame($expected, iterator_to_array($samples)); } - public function provide_test_samples(): array + public static function provide_test_samples(): array { return [ diff --git a/tests/PluralsTest.php b/tests/PluralsTest.php index 7786832..18a9a09 100644 --- a/tests/PluralsTest.php +++ b/tests/PluralsTest.php @@ -46,7 +46,7 @@ public function test_samples_for(string $locale, array $expected_keys): void $this->assertContainsOnlyInstancesOf(Samples::class, $samples); } - public function provide_test_samples_for(): array + public static function provide_test_samples_for(): array { return [ @@ -97,7 +97,7 @@ public function test_rules_for(string $locale, array $expected_keys): void $this->assertSame($expected_keys, $rules); } - public function provide_test_rules_for(): array + public static function provide_test_rules_for(): array { return [ @@ -141,7 +141,7 @@ public function test_rule_for($number, string $locale, string $expected): void $this->assertSame($expected, $this->plurals->rule_for($number, $locale)); } - public function provide_test_rule_for(): array + public static function provide_test_rule_for(): array { return [ @@ -181,7 +181,7 @@ public function test_rule_with_samples(string $locale): void } } - public function provide_test_rule_with_samples(): array + public static function provide_test_rule_with_samples(): array { return [ diff --git a/tests/RepositoryPropertyTraitTest.php b/tests/RepositoryPropertyTraitTest.php deleted file mode 100644 index 8b550a0..0000000 --- a/tests/RepositoryPropertyTraitTest.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ICanBoogie\CLDR; - -use ICanBoogie\CLDR\RepositoryPropertyTraitTest\A; -use PHPUnit\Framework\TestCase; - -final class RepositoryPropertyTraitTest extends TestCase -{ - public function test_get_repository(): void - { - $repository = get_repository(); - $a = new A($repository); - $this->assertSame($repository, $a->repository); - } -} diff --git a/tests/RepositoryPropertyTraitTest/A.php b/tests/RepositoryPropertyTraitTest/A.php deleted file mode 100644 index f29d0e4..0000000 --- a/tests/RepositoryPropertyTraitTest/A.php +++ /dev/null @@ -1,18 +0,0 @@ -repository = $repository; - } -} diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php index d99c2ec..a3951e0 100644 --- a/tests/RepositoryTest.php +++ b/tests/RepositoryTest.php @@ -12,6 +12,7 @@ namespace ICanBoogie\CLDR; use ICanBoogie\CLDR\Locale\ListPattern; +use PHPUnit\Framework\Assert; use PHPUnit\Framework\TestCase; final class RepositoryTest extends TestCase @@ -37,7 +38,7 @@ public function test_properties_instanceof(string $property, string $expected): $this->assertSame($instance, $repository->$property); } - public function provide_test_properties_instanceof(): array + public static function provide_test_properties_instanceof(): array { return [ @@ -93,16 +94,16 @@ public function test_properties(string $property, callable $assert): void $assert($this->repository->$property); } - public function provide_test_properties(): array + public static function provide_test_properties(): array { return [ [ 'available_locales', function($value) { - $this->assertContains('fr', $value); - $this->assertContains('en', $value); - $this->assertNotContains('fr-FR', $value); - $this->assertNotContains('en-US', $value); + Assert::assertContains('fr', $value); + Assert::assertContains('en', $value); + Assert::assertNotContains('fr-FR', $value); + Assert::assertNotContains('en-US', $value); } ] @@ -117,7 +118,7 @@ public function test_is_locale_available(string $locale, bool $expected): void $this->assertSame($expected, get_repository()->is_locale_available($locale)); } - public function provide_test_is_locale_available(): array + public static function provide_test_is_locale_available(): array { return [ diff --git a/tests/Supplemental/CurrencyDataTest.php b/tests/Supplemental/CurrencyDataTest.php index 4c7854f..d14e218 100644 --- a/tests/Supplemental/CurrencyDataTest.php +++ b/tests/Supplemental/CurrencyDataTest.php @@ -53,7 +53,7 @@ public function test_fraction_for(string $currency_code, array $expected): void /** * @phpstan-ignore-next-line */ - public function provide_fraction_for(): array + public static function provide_fraction_for(): array { return [ diff --git a/tests/Supplemental/FractionTest.php b/tests/Supplemental/FractionTest.php index 304d5ad..985578b 100644 --- a/tests/Supplemental/FractionTest.php +++ b/tests/Supplemental/FractionTest.php @@ -33,7 +33,7 @@ public function test_from(array $data, int $digits, int $rounding, int $cash_dig /** * @phpstan-ignore-next-line */ - public function provide_from(): array + public static function provide_from(): array { return [ diff --git a/tests/SupplementalTest.php b/tests/SupplementalTest.php index 3895307..4d749f3 100644 --- a/tests/SupplementalTest.php +++ b/tests/SupplementalTest.php @@ -41,7 +41,7 @@ public function test_sections(string $section, string $key): void /** * @phpstan-ignore-next-line */ - public function provide_test_sections(): array + public static function provide_test_sections(): array { return [ @@ -90,7 +90,7 @@ public function test_properties(string $property, string $expected): void /** * @phpstan-ignore-next-line */ - public function provide_properties(): array + public static function provide_properties(): array { return [ diff --git a/tests/TerritoryNotDefinedTest.php b/tests/TerritoryNotDefinedTest.php index f7cd9ff..46b22ac 100644 --- a/tests/TerritoryNotDefinedTest.php +++ b/tests/TerritoryNotDefinedTest.php @@ -28,7 +28,7 @@ public function test_instance(string $territory_code, ?string $message, string $ $this->assertSame($previous, $sut->getPrevious()); } - public function provide_instance(): array + public static function provide_instance(): array { $territory_code = 'FR'; $previous = new Exception; diff --git a/tests/TerritoryTest.php b/tests/TerritoryTest.php index 17522ca..ee9e9b5 100644 --- a/tests/TerritoryTest.php +++ b/tests/TerritoryTest.php @@ -44,7 +44,7 @@ public function test_get_currency(string $expected, string $territory_code): voi $this->assertEquals($expected, $territory->currency); } - public function provide_test_get_currency(): array + public static function provide_test_get_currency(): array { return [ @@ -66,7 +66,7 @@ public function test_currency_at(string $expected, string $territory_code, $date $this->assertEquals($expected, $territory->currency_at($date)); } - public function provide_test_currency_at(): array + public static function provide_test_currency_at(): array { return [ @@ -91,7 +91,7 @@ public function test_get_language(string $expected, string $territory_code): voi $this->assertSame($expected, $territory->language); } - public function provide_test_get_language(): array + public static function provide_test_get_language(): array { return [ @@ -117,7 +117,7 @@ public function test_name_as(string $expected, string $territory_code, string $l $this->assertEquals($expected, $territory->name_as($locale_code)); } - public function provide_test_name_as(): array + public static function provide_test_name_as(): array { return [ @@ -138,7 +138,7 @@ public function test_get_name_as(string $expected, string $territory_code, strin $this->assertEquals($expected, $territory->{ 'name_as_' . $locale_code }); } - public function provide_test_get_name_as(): array + public static function provide_test_get_name_as(): array { return [ @@ -162,7 +162,7 @@ public function test_get_property(string $expected, string $territory_code, stri /** * @see https://github.com/unicode-org/cldr-json/blob/41.0.0/cldr-json/cldr-core/supplemental/weekData.json */ - public function provide_test_get_property(): array + public static function provide_test_get_property(): array { return [ diff --git a/tests/TimeFormatterTest.php b/tests/TimeFormatterTest.php index 8613f87..746782a 100644 --- a/tests/TimeFormatterTest.php +++ b/tests/TimeFormatterTest.php @@ -36,7 +36,7 @@ public function test_format(string $locale, string $datetime, string $pattern, s $this->assertEquals($expected, self::$formatters[$locale]->format($datetime, $pattern)); } - public function provide_test_format(): array + public static function provide_test_format(): array { return [ diff --git a/tests/Units/NumberPerUnitTest.php b/tests/Units/NumberPerUnitTest.php index a72dbea..d728430 100644 --- a/tests/Units/NumberPerUnitTest.php +++ b/tests/Units/NumberPerUnitTest.php @@ -51,7 +51,7 @@ public function test_cases(string $locale, $number, string $number_unit, string $this->assertSame($expected, $stu->{ 'as_' . $length }); } - public function provide_test_cases(): array + public static function provide_test_cases(): array { return [ diff --git a/tests/Units/NumberWithUnitTest.php b/tests/Units/NumberWithUnitTest.php index e2348c0..2c42abb 100644 --- a/tests/Units/NumberWithUnitTest.php +++ b/tests/Units/NumberWithUnitTest.php @@ -51,7 +51,7 @@ public function test_cases(string $locale, string $unit, $number, string $length $this->assertSame($expected, $stu->{ 'as_' . $length }); } - public function provide_test_cases(): array + public static function provide_test_cases(): array { return [ @@ -88,7 +88,7 @@ public function test_per( ); } - public function provide_per(): array + public static function provide_per(): array { return [ diff --git a/tests/Units/SequenceTest.php b/tests/Units/SequenceTest.php index 31728c3..430f377 100644 --- a/tests/Units/SequenceTest.php +++ b/tests/Units/SequenceTest.php @@ -25,7 +25,7 @@ public function test_format(): void $length = Units::LENGTH_NARROW; $units = $this->getMockBuilder(Units::class) - ->setMethods([ 'assert_is_unit', 'format_sequence' ]) + ->onlyMethods([ 'assert_is_unit', 'format_sequence' ]) ->disableOriginalConstructor() ->getMock(); $units @@ -55,7 +55,7 @@ public function test_to_string(): void $length = Units::DEFAULT_LENGTH; $units = $this->getMockBuilder(Units::class) - ->setMethods([ 'assert_is_unit', 'format_sequence' ]) + ->onlyMethods([ 'assert_is_unit', 'format_sequence' ]) ->disableOriginalConstructor() ->getMock(); $units diff --git a/tests/Units/UnitTest.php b/tests/Units/UnitTest.php index 44f46c9..3b989c9 100644 --- a/tests/Units/UnitTest.php +++ b/tests/Units/UnitTest.php @@ -52,7 +52,7 @@ public function test_properties(string $unit, string $property, string $length, /** * @phpstan-ignore-next-line */ - public function provide_test_properties(): array + public static function provide_test_properties(): array { return [ diff --git a/tests/UnitsTest.php b/tests/UnitsTest.php index 7d06ca0..9675cc2 100644 --- a/tests/UnitsTest.php +++ b/tests/UnitsTest.php @@ -41,7 +41,7 @@ public function test_cases(string $locale, string $unit, $number, string $length /** * @phpstan-ignore-next-line */ - public function provide_test_cases(): array + public static function provide_test_cases(): array { return [ @@ -79,7 +79,7 @@ public function test_format_compound( /** * @phpstan-ignore-next-line */ - public function provide_test_format_compound(): array + public static function provide_test_format_compound(): array { return [ @@ -108,7 +108,7 @@ public function test_format_sequence(string $locale, callable $sequence, string /** * @phpstan-ignore-next-line */ - public function provide_test_format_sequence(): array + public static function provide_test_format_sequence(): array { $s1 = Spaces::NARROW_NO_BREAK_SPACE; $s2 = Spaces::NO_BREAK_SPACE; @@ -225,7 +225,7 @@ public function test_name_for(string $unit, string $length, string $expected_nam /** * @phpstan-ignore-next-line */ - public function provide_name_for(): array + public static function provide_name_for(): array { return [