From de06d40441d8cf3f5c45097e138a174446c772d4 Mon Sep 17 00:00:00 2001 From: henzeb Date: Tue, 28 Feb 2023 21:05:48 +0100 Subject: [PATCH] version 2.0.0 --- CHANGELOG.md | 10 + README.md | 3 +- composer.json | 3 +- docs/makers.md | 63 -- phpstan-baseline.neon | 660 ------------------ src/Concerns/Bitmasks.php | 3 + src/Concerns/Makers.php | 48 -- src/Concerns/Mappers.php | 74 -- src/Concerns/Reporters.php | 16 - src/Concerns/State.php | 27 +- src/Concerns/Subset.php | 11 +- src/Enums/LogLevel.php | 3 + src/Helpers/Bitmasks/EnumBitmasks.php | 7 +- src/Helpers/Concerns/EnumImplements.php | 78 --- src/Helpers/EnumImplements.php | 123 +++- src/Helpers/EnumReporter.php | 7 + src/Helpers/Mappers/EnumArrayMapper.php | 7 +- src/Helpers/Mappers/EnumMapper.php | 38 +- src/Helpers/Subset/EnumSubsetMethods.php | 37 +- src/Laravel/Middleware/SubstituteEnums.php | 4 +- src/Laravel/Reporters/LaravelLogReporter.php | 11 +- src/Laravel/Rules/EnumBitmask.php | 9 +- src/Laravel/Rules/EnumTransition.php | 2 +- src/Laravel/Rules/IsEnum.php | 6 +- tests/Fixtures/ExtractBackedEnum.php | 3 - tests/Fixtures/IntBackedEnum.php | 6 +- tests/Fixtures/StringBackedGetEnum.php | 3 +- .../UnitEnums/Getters/GetUnitEnum.php | 3 +- tests/Unit/Concerns/MakersTest.php | 180 ----- tests/Unit/Concerns/MappersMakersTest.php | 319 --------- tests/Unit/Concerns/ReportersMakersTest.php | 198 ------ tests/Unit/Helpers/EnumImplementsTest.php | 28 + tests/Unit/Helpers/EnumSubsetMethodsTest.php | 4 +- 33 files changed, 277 insertions(+), 1717 deletions(-) delete mode 100644 docs/makers.md delete mode 100644 src/Concerns/Makers.php delete mode 100644 src/Helpers/Concerns/EnumImplements.php delete mode 100644 tests/Unit/Concerns/MakersTest.php delete mode 100644 tests/Unit/Concerns/MappersMakersTest.php delete mode 100644 tests/Unit/Concerns/ReportersMakersTest.php create mode 100644 tests/Unit/Helpers/EnumImplementsTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index fe59ba0..3ceac28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ All notable changes to `Enumhancer` will be documented in this file +## 2.0.0 - 2023-02-28 + +- Now supports Laravel 10 +- dropped support for laravel 8 + +### Upgrade notes + +- Makers (make, tryMake etc.) are removed in favor + of [Getters](docs/getters.md) + ## 1.23.0 - 2023-02-03 - added [PHPStan](docs/phpstan.md) support diff --git a/README.md b/README.md index 3cf1626..ac95d13 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,6 @@ implemented the methods of `Getters`, `Extractor` and `Reporters`. - [From](docs/from.md) - [Getters](docs/getters.md) - [Labels](docs/labels.md) -- ~~[Makers](docs/makers.md)~~ (deprecated) - [Macros](docs/macros.md) - [Mappers](docs/mappers.md) - [Properties](docs/properties.md) @@ -99,7 +98,7 @@ implemented the methods of `Getters`, `Extractor` and `Reporters`. When you are installing this package into a laravel project, Enumhancer will automatically set macro's for the `validation rules` and sets the global -`Reporter` for the `makeOrReport` methods, so that it will use Laravel's +`Reporter` for the `getOrReport` methods, so that it will use Laravel's `Log` facade. If you don't want that to happen, you can tell Laravel not to discover the diff --git a/composer.json b/composer.json index 3c7e980..933022d 100644 --- a/composer.json +++ b/composer.json @@ -73,9 +73,8 @@ "composer/composer": "^2.5", "henzeb/enumhancer-ide-helper": "main-dev", "mockery/mockery": "^1.5", - "nette/php-generator": "^4.0", "nunomaduro/larastan": "^2.3", - "orchestra/testbench": "v6.24.1|^v7.18", + "orchestra/testbench": "^v7.18|^8.0", "phpstan/phpstan": "^1.9", "phpunit/phpunit": "^9.5.27" }, diff --git a/docs/makers.md b/docs/makers.md deleted file mode 100644 index 084dcc0..0000000 --- a/docs/makers.md +++ /dev/null @@ -1,63 +0,0 @@ -# Makers - -DEPRECATED. PLEASE USE [Getters](getters.md) instead! - -Enums have out of the box the methods `from` and `tryFrom`. One drawback is that -you cannot use them to create `enum objects` with the name of the `enum`. - -This allows you to do so. - -Note: the name `make` is chosen because of Spatie, who started using them and -never really moved away from them. So this is also backwards compatible with -[Spatie's PHP Enum](https://github.com/spatie/enum) - -## Usage - -```php -use Henzeb\Enumhancer\Concerns\Makers; - -enum YourEnum: string { - use Makers; - - case ENUM = 'your_enum'; - case ENUM2 = 'your_other_enum'; -} -``` - -### Examples - -```php -/** make */ -YourEnum::make('ENUM'); // returns YourEnum::ENUM -YourEnum::make('0'); // returns YourEnum::ENUM -YourEnum::make(0); // returns YourEnum::ENUM -YourEnum::make('ENUM2'); // returns YourEnum::ENUM2 -YourEnum::make('1'); // returns YourEnum::ENUM2 -YourEnum::make(1); // returns YourEnum::ENUM2 -YourEnum::make('your_enum'); // returns YourEnum::ENUM -YourEnum::make('your_other_enum'); // returns YourEnum::ENUM2 -YourEnum::make('ENUM3'); // throws exception - -/** tryMake */ -YourEnum::tryMake('ENUM'); // returns YourEnum::ENUM -YourEnum::tryMake('0'); // returns YourEnum::ENUM -YourEnum::tryMake(1); // returns YourEnum::ENUM -YourEnum::tryMake('ENUM2'); // returns YourEnum::ENUM2 -YourEnum::tryMake('your_enum'); // returns YourEnum::ENUM -YourEnum::tryMake('your_other_enum'); // returns YourEnum::ENUM2 -YourEnum::tryMake('ENUM3'); // returns null -YourEnum::tryMake(3); // returns null - -/** makeArray */ - -YourEnum::makeArray( - ['ENUM', 'your_other_enum'] -); // returns [YourEnum::ENUM, YourEnum::ENUM2] -YourEnum::makeArray(['ENUM', 'unknown']); // throws exception -/** tryMakeArray */ - -YourEnum::tryMakeArray( - ['ENUM', 'your_other_enum'] -); // returns [YourEnum::ENUM, YourEnum::ENUM2] -YourEnum::tryMakeArray(['ENUM', 'unknown']); // returns [YourEnum::ENUM] -``` diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f1daa6a..7498602 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -15,511 +15,21 @@ parameters: count: 1 path: src/Concerns/Getters.php - - - message: "#^Trait Henzeb\\\\Enumhancer\\\\Concerns\\\\Makers is used zero times and is not analysed\\.$#" - count: 1 - path: src/Concerns/Makers.php - - message: "#^Trait Henzeb\\\\Enumhancer\\\\Concerns\\\\Reporters is used zero times and is not analysed\\.$#" count: 1 path: src/Concerns/Reporters.php - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Contracts\\\\EnumSubset\\:\\:cases\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/EnumSubset.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Contracts\\\\EnumSubset\\:\\:names\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/EnumSubset.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Contracts\\\\EnumSubset\\:\\:values\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/EnumSubset.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Contracts\\\\Mapper\\:\\:__call\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/Mapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Contracts\\\\Mapper\\:\\:__callStatic\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/Mapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Contracts\\\\Mapper\\:\\:flipMappable\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/Mapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Contracts\\\\Mapper\\:\\:getMap\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/Mapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Contracts\\\\Mapper\\:\\:getMapWithPrefix\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/Mapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Contracts\\\\Mapper\\:\\:keysMethod\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/Mapper.php - - - - message: "#^PHPDoc tag @var for variable \\$mappable has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/Mapper.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Contracts\\\\Mapper\\:\\:\\$flipped type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contracts/Mapper.php - - message: "#^Unsafe usage of new static\\(\\)\\.$#" count: 1 path: src/Contracts/Mapper.php - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:__call\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 3 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:__callStatic\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 3 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:allowedTransitions\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:bits\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:customTransitions\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:dropdown\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:extract\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:extract\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:get\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:getArray\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:getArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:getArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:getOrReport\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:getOrReportArray\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:getOrReportArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:getOrReportArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:labels\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:makeArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:makeArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:makeOrReportArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:makeOrReportArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:mapper\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:setLabels\\(\\) has parameter \\$labels with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:setLabelsOnce\\(\\) has parameter \\$labels with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:setMapper\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:setMapperOnce\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:setTransitions\\(\\) has parameter \\$transitions with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:setTransitionsOnce\\(\\) has parameter \\$transitions with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:tryArray\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:tryArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:tryArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:tryGet\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:tryMakeArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Enums\\\\LogLevel\\:\\:tryMakeArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Enums/LogLevel.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Bitmasks\\\\Bitmask\\:\\:cases\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Bitmasks/Bitmask.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Bitmasks\\\\EnumBitmasks\\:\\:getCaseBits\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Bitmasks/EnumBitmasks.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\Bitmasks\\\\EnumBitmasks\\:\\:\\$isValid has no type specified\\.$#" - count: 1 - path: src/Helpers/Bitmasks/EnumBitmasks.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumCompare\\:\\:isValidCall\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumCompare.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:extract\\(\\) has parameter \\$mappers with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:extract\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:get\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:getArray\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:getArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:getArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:getOrReport\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:getOrReportArray\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:getOrReportArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:getOrReportArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:makeArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:makeArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:makeOrReportArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:makeOrReportArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:mapper\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:tryArray\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:tryArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:tryArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:tryGet\\(\\) has parameter \\$mapper with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:tryMakeArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumExtractor\\:\\:tryMakeArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumExtractor.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumGetters\\:\\:cases\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumGetters.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumGetters\\:\\:findCase\\(\\) has parameter \\$constants with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumGetters.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumGetters\\:\\:getArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/EnumGetters.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumGetters\\:\\:getArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumGetters.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumGetters\\:\\:tryArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/EnumGetters.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumGetters\\:\\:tryArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumGetters.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumImplements\\:\\:available\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumImplements.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumImplements\\:\\:classUsesTrait\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumImplements.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumImplements\\:\\:getTraitsFrom\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumImplements.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumImplements\\:\\:\\$available type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumImplements.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumImplements\\:\\:\\$passivelyImplements type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumImplements.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumLabels\\:\\:getConfiguredLabels\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumLabels.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumLabels\\:\\:getLabels\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumLabels.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumMacros\\:\\:call\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumMacros.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumMacros\\:\\:callStatic\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumMacros.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumMacros\\:\\:getMacros\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumMacros.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumMacros\\:\\:getMethodsFromMixin\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumMacros.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumMacros\\:\\:\\$globalMacros type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumMacros.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumMacros\\:\\:\\$macros type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumMacros.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumMagicCalls\\:\\:call\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumMagicCalls.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumMagicCalls\\:\\:static\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumMagicCalls.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumProperties\\:\\:\\$global type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumProperties.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumProperties\\:\\:\\$once type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumProperties.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumProperties\\:\\:\\$properties type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumProperties.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumProperties\\:\\:\\$reserved type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumProperties.php - - message: "#^Access to an uninitialized readonly property Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumProxy\\:\\:\\$name\\.$#" count: 1 path: src/Helpers/EnumProxy.php - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumReporter\\:\\:getOrReportArray\\(\\) has parameter \\$keys with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/EnumReporter.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumReporter\\:\\:getOrReportArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumReporter.php - - message: "#^Call to method to\\(\\) on an unknown class Henzeb\\\\Enumhancer\\\\Concerns\\\\State\\.$#" count: 1 @@ -530,146 +40,11 @@ parameters: count: 1 path: src/Helpers/EnumState.php - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:allowedTransitions\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:call\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:castTransitions\\(\\) has parameter \\$transitions with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:castTransitions\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:filterAllowedTransitions\\(\\) has parameter \\$hooks with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:filterAllowedTransitions\\(\\) has parameter \\$transitions with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:filterAllowedTransitions\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:getTransitions\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:transitions\\(\\) has parameter \\$userTransitions with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\EnumState\\:\\:transitions\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/EnumState.php - - message: "#^PHPDoc tag @var for variable \\$currentState has invalid type Henzeb\\\\Enumhancer\\\\Concerns\\\\State\\.$#" count: 1 path: src/Helpers/EnumState.php - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumArrayMapper\\:\\:__construct\\(\\) has parameter \\$mappable with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumArrayMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:getConfiguredMapper\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:getConstantMappers\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:getMappers\\(\\) has parameter \\$mappers with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:getMappers\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:map\\(\\) has parameter \\$mappers with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:mapArray\\(\\) has parameter \\$mappers with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:mapArray\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:mapArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:sanitizeMapperArray\\(\\) has parameter \\$mappers with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:sanitizeMapperArray\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Mappers\\\\EnumMapper\\:\\:wrapInMapper\\(\\) has parameter \\$map with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Mappers/EnumMapper.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Subset\\\\EnumSubsetMethods\\:\\:cases\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Subset/EnumSubsetMethods.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Subset\\\\EnumSubsetMethods\\:\\:dropdown\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Subset/EnumSubsetMethods.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Subset\\\\EnumSubsetMethods\\:\\:names\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Subset/EnumSubsetMethods.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Helpers\\\\Subset\\\\EnumSubsetMethods\\:\\:values\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Subset/EnumSubsetMethods.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Helpers\\\\Subset\\\\EnumSubsetMethods\\:\\:\\$enums type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Helpers/Subset/EnumSubsetMethods.php - - message: "#^Trait Henzeb\\\\Enumhancer\\\\Laravel\\\\Concerns\\\\CastsBasicEnumerations is used zero times and is not analysed\\.$#" count: 1 @@ -680,47 +55,12 @@ parameters: count: 1 path: src/Laravel/Concerns/CastsStatefulEnumerations.php - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Laravel\\\\Middleware\\\\SubstituteEnums\\:\\:getParameters\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Laravel/Middleware/SubstituteEnums.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Laravel\\\\Reporters\\\\LaravelLogReporter\\:\\:getChannels\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Laravel/Reporters/LaravelLogReporter.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Laravel\\\\Reporters\\\\LaravelLogReporter\\:\\:\\$channels type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Laravel/Reporters/LaravelLogReporter.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Laravel\\\\Rules\\\\EnumBitmask\\:\\:message\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Laravel/Rules/EnumBitmask.php - - message: "#^Call to method isTransitionAllowed\\(\\) on an unknown class Henzeb\\\\Enumhancer\\\\Concerns\\\\State\\.$#" count: 1 path: src/Laravel/Rules/EnumTransition.php - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Laravel\\\\Rules\\\\EnumTransition\\:\\:message\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Laravel/Rules/EnumTransition.php - - message: "#^PHPDoc tag @var for variable \\$currentState has invalid type Henzeb\\\\Enumhancer\\\\Concerns\\\\State\\.$#" count: 1 path: src/Laravel/Rules/EnumTransition.php - - - - message: "#^Method Henzeb\\\\Enumhancer\\\\Laravel\\\\Rules\\\\IsEnum\\:\\:__construct\\(\\) has parameter \\$mappers with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Laravel/Rules/IsEnum.php - - - - message: "#^Property Henzeb\\\\Enumhancer\\\\Laravel\\\\Rules\\\\IsEnum\\:\\:\\$mappers type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Laravel/Rules/IsEnum.php diff --git a/src/Concerns/Bitmasks.php b/src/Concerns/Bitmasks.php index f38abb7..23a8b7e 100644 --- a/src/Concerns/Bitmasks.php +++ b/src/Concerns/Bitmasks.php @@ -12,6 +12,9 @@ public function bit(): int return EnumBitmasks::getBit($this); } + /** + * @return array + */ public static function bits(): array { return EnumBitmasks::getCaseBits(self::class); diff --git a/src/Concerns/Makers.php b/src/Concerns/Makers.php deleted file mode 100644 index 5abfee8..0000000 --- a/src/Concerns/Makers.php +++ /dev/null @@ -1,48 +0,0 @@ -transitionTo($state, $hook); } - public function tryTo(self|string|int $state, TransitionHook $hook = null): self + public function tryTo(self|string|int $state, TransitionHook $hook = null): static { if ($this->isTransitionAllowed($state, $hook)) { return $this->transitionTo($state, $hook); @@ -62,7 +70,7 @@ public function isTransitionAllowed(self|string|int $state, TransitionHook $hook /** * @param TransitionHook|null $hook - * @return array + * @return static[] */ public function allowedTransitions(TransitionHook $hook = null): array { @@ -73,13 +81,16 @@ public function allowedTransitions(TransitionHook $hook = null): array } /** - * @return self[] + * @return static[] */ public static function transitions(): array { return EnumState::transitions(self::class, self::customTransitions()); } + /** + * @return static[] + */ protected static function customTransitions(): array { return EnumProperties::get(self::class, EnumProperties::reservedWord('state')) ?? []; diff --git a/src/Concerns/Subset.php b/src/Concerns/Subset.php index a42bddd..4898a27 100644 --- a/src/Concerns/Subset.php +++ b/src/Concerns/Subset.php @@ -2,12 +2,15 @@ namespace Henzeb\Enumhancer\Concerns; -use Henzeb\Enumhancer\Contracts\EnumSubset; use Henzeb\Enumhancer\Helpers\Subset\EnumSubsetMethods; use UnitEnum; trait Subset { + /** + * @param static[] $enums + * @return EnumSubsetMethods + */ public static function without(self ...$enums): EnumSubsetMethods { return new EnumSubsetMethods( @@ -21,7 +24,11 @@ function (UnitEnum $case) use ($enums) { ); } - public static function of(self ...$enums): EnumSubset + /** + * @param static[] $enums + * @return EnumSubsetMethods + */ + public static function of(self ...$enums): EnumSubsetMethods { return new EnumSubsetMethods( self::class, diff --git a/src/Enums/LogLevel.php b/src/Enums/LogLevel.php index 3904039..9712963 100644 --- a/src/Enums/LogLevel.php +++ b/src/Enums/LogLevel.php @@ -6,6 +6,9 @@ use Henzeb\Enumhancer\Concerns\Enhancers; use Henzeb\Enumhancer\Concerns\Macros; +/** + * @uses Enhancers + */ enum LogLevel { use Enhancers, Configure, Macros; diff --git a/src/Helpers/Bitmasks/EnumBitmasks.php b/src/Helpers/Bitmasks/EnumBitmasks.php index efa0480..3e3c05b 100644 --- a/src/Helpers/Bitmasks/EnumBitmasks.php +++ b/src/Helpers/Bitmasks/EnumBitmasks.php @@ -11,6 +11,7 @@ use Henzeb\Enumhancer\Helpers\EnumValue; use ReflectionClass; use UnitEnum; +use function is_object; use const E_USER_ERROR; final class EnumBitmasks @@ -119,7 +120,7 @@ public static function getBits(string|UnitEnum $class, Bitmask|UnitEnum|string|i private static function castToBits(Bitmask|UnitEnum|string|int $value, string|UnitEnum $class): int { - $class = \is_object($class) ? $class::class : $class; + $class = is_object($class) ? $class::class : $class; if ($value instanceof Bitmask) { self::forOrFail($class, $value); @@ -142,6 +143,10 @@ private static function castToBits(Bitmask|UnitEnum|string|int $value, string|Un self::throwMismatch($class, gettype($value)); } + /** + * @param string $class + * @return array + */ public static function getCaseBits(string $class): array { /** diff --git a/src/Helpers/Concerns/EnumImplements.php b/src/Helpers/Concerns/EnumImplements.php deleted file mode 100644 index 3ec7c22..0000000 --- a/src/Helpers/Concerns/EnumImplements.php +++ /dev/null @@ -1,78 +0,0 @@ - $class] as $class) { - $results += self::getTraitsFrom($class); - } - - return array_unique($results); - } - - private static function getTraitsFrom(string $class): array - { - $traits = class_uses($class) ?: []; - - foreach ($traits as $class) { - $traits += self::getTraitsFrom($class); - } - - return $traits; - } -} diff --git a/src/Helpers/EnumImplements.php b/src/Helpers/EnumImplements.php index 4fa9ce9..d5f3652 100644 --- a/src/Helpers/EnumImplements.php +++ b/src/Helpers/EnumImplements.php @@ -2,63 +2,124 @@ namespace Henzeb\Enumhancer\Helpers; -use Henzeb\Enumhancer\Concerns\Bitmasks; -use Henzeb\Enumhancer\Concerns\Comparison; +use Henzeb\Enumhancer\Concerns\Configure; use Henzeb\Enumhancer\Concerns\Constructor; -use Henzeb\Enumhancer\Concerns\Defaults; -use Henzeb\Enumhancer\Concerns\Labels; +use Henzeb\Enumhancer\Concerns\Enhancers; +use Henzeb\Enumhancer\Concerns\Extractor; use Henzeb\Enumhancer\Concerns\Macros; -use Henzeb\Enumhancer\Concerns\Mappers; -use Henzeb\Enumhancer\Concerns\State; -use Henzeb\Enumhancer\Concerns\Value; -use Henzeb\Enumhancer\Helpers\Concerns\EnumImplements as EnumImplementsBase; +use Henzeb\Enumhancer\Concerns\Reporters; +/** + * @method static bool bitmasks(string $enum) + * @method static bool comparison(string $enum) + * @method static bool defaults(string $enum) + * @method static bool dropdown(string $enum) + * @method static bool from(string $enum) + * @method static bool labels(string $enum) + * @method static bool mappers(string $enum) + * @method static bool properties(string $enum) + * @method static bool state(string $enum) + * @method static bool subset(string $enum) + * @method static bool value(string $enum) + * @method static bool configure(string $enum) + * @method static bool constructor(string $enum) + * @method static bool extractor(string $enum) + * @method static bool macros(string $enum) + * @method static bool reporters(string $enum) + * @method static bool configureDefaults(string $enum) + * @method static bool configureLabels(string $enum) + * @method static bool configureMapper(string $enum) + * @method static bool configureState(string $enum) + */ final class EnumImplements { - use EnumImplementsBase; + private static array $passivelyImplements = [ + Configure::class, + Constructor::class, + Extractor::class, + Macros::class, + Reporters::class, + ]; - public static function mappers(string $class): bool - { - return self::implements($class, Mappers::class); - } + private static ?array $available = null; - public static function defaults(string $class): bool + public static function available(): array { - return self::implements($class, Defaults::class); + return self::$available ?? self::buildAvailable(); } - public static function value(string $class): bool + private static function buildAvailable(): array { - return self::implements($class, Value::class); - } + $availablePassive = array_map(self::getTraitsFrom(...), self::$passivelyImplements); - public static function state(string $class): bool - { - return self::implements($class, State::class); + $available = array_values(array_merge( + array_values(self::getTraitsFrom(Enhancers::class)), + self::$passivelyImplements, + ... $availablePassive + )); + + $available = array_map( + fn($class) => [strtolower(basename(str_replace('\\', '/', $class))) => $class], + $available + ); + + return self::$available = array_merge(...$available); } - public static function labels(string $class): bool + public static function enumhancer(string $class): bool { - return self::implements($class, Labels::class); + if (self::implements($class, Enhancers::class)) { + return true; + } + + foreach (self::available() as $trait) { + if (self::implements($class, $trait)) { + return true; + } + } + return false; } - public static function macros(string $class): bool + public static function __callStatic(string $name, array $arguments): bool { - return self::implements($class, Macros::class); + $implements = self::available()[strtolower($name)] ?? null; + + if ($implements && $arguments[0] && is_string($arguments[0])) { + return self::implements($arguments[0], $implements); + } + + trigger_error( + sprintf('Call to undefined method %s::%s()', self::class, $name), + E_USER_ERROR + ); } - public static function constructor(string $class): bool + public static function implements(string $class, string $implements): bool { - return self::implements($class, Constructor::class); + EnumCheck::check($class); + + return in_array($implements, self::classUsesTrait($class)); } - public static function comparison(string $class): bool + private static function classUsesTrait(string $class): array { - return self::implements($class, Comparison::class); + $results = []; + + foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) { + $results += self::getTraitsFrom($class); + } + + return array_unique($results); } - public static function bitmasks(string $class): bool + private static function getTraitsFrom(string $class): array { - return self::implements($class, Bitmasks::class); + $traits = class_uses($class) ?: []; + + foreach ($traits as $class) { + $traits += self::getTraitsFrom($class); + } + + return $traits; } } diff --git a/src/Helpers/EnumReporter.php b/src/Helpers/EnumReporter.php index 519d901..e17d2ce 100644 --- a/src/Helpers/EnumReporter.php +++ b/src/Helpers/EnumReporter.php @@ -79,6 +79,13 @@ public static function getOrReport( return $enum; } + /** + * @param string $class + * @param iterable $keys + * @param BackedEnum|null $context + * @param Reporter|null $reporter + * @return UnitEnum[] + */ public static function getOrReportArray( string $class, iterable $keys, diff --git a/src/Helpers/Mappers/EnumArrayMapper.php b/src/Helpers/Mappers/EnumArrayMapper.php index c62256f..2d58c9b 100644 --- a/src/Helpers/Mappers/EnumArrayMapper.php +++ b/src/Helpers/Mappers/EnumArrayMapper.php @@ -3,13 +3,18 @@ namespace Henzeb\Enumhancer\Helpers\Mappers; use Henzeb\Enumhancer\Contracts\Mapper; +use UnitEnum; final class EnumArrayMapper extends Mapper { - public function __construct(private array $mappable) + /** + * @param array> $mappable + */ + public function __construct(private readonly array $mappable) { } + protected function mappable(): array { return $this->mappable; diff --git a/src/Helpers/Mappers/EnumMapper.php b/src/Helpers/Mappers/EnumMapper.php index fe21eba..48db9cc 100644 --- a/src/Helpers/Mappers/EnumMapper.php +++ b/src/Helpers/Mappers/EnumMapper.php @@ -7,6 +7,7 @@ use Henzeb\Enumhancer\Helpers\EnumCheck; use Henzeb\Enumhancer\Helpers\EnumProperties; use ReflectionClass; +use ReflectionException; use RuntimeException; use UnitEnum; use ValueError; @@ -19,10 +20,16 @@ */ final class EnumMapper { + /** + * @param string $enum + * @param string|int|UnitEnum|null $value + * @param array|Mapper|string|null ...$mappers + * @return string|null + */ public static function map( string $enum, - string|int|UnitEnum|null $value, - Mapper|array|string|null ...$mappers + UnitEnum|int|string|null $value, + array|string|Mapper|null ...$mappers ): ?string { EnumCheck::check($enum); @@ -37,6 +44,12 @@ public static function map( return $value instanceof UnitEnum ? $value->name : $value; } + /** + * @param string $enum + * @param iterable $values + * @param Mapper|string|array>|null ...$mappers + * @return string[] + */ public static function mapArray(string $enum, iterable $values, Mapper|string|array|null ...$mappers): array { $mapped = []; @@ -48,6 +61,9 @@ public static function mapArray(string $enum, iterable $values, Mapper|string|ar return $mapped; } + /** + * @return Mapper[] + */ private static function sanitizeMapperArray(array $mappers): array { return array_map( @@ -76,6 +92,11 @@ function (Mapper|array|string $mapper) { ); } + /** + * @param string $enum + * @return Mapper[] + * @throws ReflectionException + */ private static function getConstantMappers(string $enum): array { /** @@ -141,11 +162,19 @@ protected static function instantiateMapper(string $class, bool $flip = false): return $class::newInstance(); } + /** + * @param array> $map + * @return Mapper + */ protected static function wrapInMapper(array $map): Mapper { return EnumArrayMapper::newInstance($map); } + /** + * @param string $enum + * @return array> + */ private static function getConfiguredMapper(string $enum): array { return EnumProperties::get( @@ -156,8 +185,9 @@ private static function getConfiguredMapper(string $enum): array /** * @param string $enum - * @param array $mappers - * @return array + * @param Mapper|string|array|null ...$mappers + * @return Mapper + * @throws ReflectionException */ public static function getMappers(string $enum, Mapper|string|array|null ...$mappers): array { diff --git a/src/Helpers/Subset/EnumSubsetMethods.php b/src/Helpers/Subset/EnumSubsetMethods.php index d076df6..88a9b58 100644 --- a/src/Helpers/Subset/EnumSubsetMethods.php +++ b/src/Helpers/Subset/EnumSubsetMethods.php @@ -2,21 +2,28 @@ namespace Henzeb\Enumhancer\Helpers\Subset; -use BackedEnum; use Closure; -use Henzeb\Enumhancer\Contracts\EnumSubset; use Henzeb\Enumhancer\Helpers\EnumCheck; use Henzeb\Enumhancer\Helpers\EnumGetters; -use Henzeb\Enumhancer\Helpers\EnumImplements; use Henzeb\Enumhancer\Helpers\EnumLabels; use Henzeb\Enumhancer\Helpers\EnumValue; use UnitEnum; -class EnumSubsetMethods implements EnumSubset +/** + * @template T of UnitEnum + */ +class EnumSubsetMethods { + /** + * @var T[] + */ private array $enums; - public function __construct(private readonly string $enumType, UnitEnum|BackedEnum ...$enums) + /** + * @param class-string $enumType + * @param T ...$enums + */ + public function __construct(private readonly string $enumType, UnitEnum ...$enums) { EnumCheck::matches($enumType, ...$enums); @@ -65,21 +72,32 @@ private function asEnumObject(mixed $value): ?UnitEnum return $value; } + + /** + * @return string[] + */ public function names(): array { return array_map(fn(UnitEnum $enum) => $enum->name, $this->enums); } - public function values(): array + /** + * @return string[]|int[] + */ + public function values(bool $keepEnumCase = null): array { return array_map( - function (mixed $enum) { - return EnumValue::value($enum, !EnumImplements::value($enum::class)); + function (mixed $enum) use ($keepEnumCase) { + return EnumValue::value($enum, $keepEnumCase); }, $this->enums ); } + /** + * @param bool|null $keepEnumCase + * @return array + */ public function dropdown(bool $keepEnumCase = null): array { return array_replace( @@ -98,6 +116,9 @@ function (UnitEnum $case) use ($keepEnumCase) { ); } + /** + * @return T[] + */ public function cases(): array { return $this->enums; diff --git a/src/Laravel/Middleware/SubstituteEnums.php b/src/Laravel/Middleware/SubstituteEnums.php index 686bf02..0470357 100644 --- a/src/Laravel/Middleware/SubstituteEnums.php +++ b/src/Laravel/Middleware/SubstituteEnums.php @@ -10,6 +10,7 @@ use Illuminate\Http\Request; use Illuminate\Routing\Route; use ReflectionEnum; +use ReflectionException; use ReflectionParameter; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use UnitEnum; @@ -59,7 +60,8 @@ protected function processParameter(ReflectionEnum $parameter, Request $request, /** * @param Route|null $route - * @return array + * @return array + * @throws ReflectionException */ private function getParameters(Route|null $route): array { diff --git a/src/Laravel/Reporters/LaravelLogReporter.php b/src/Laravel/Reporters/LaravelLogReporter.php index 55accb6..96f39eb 100644 --- a/src/Laravel/Reporters/LaravelLogReporter.php +++ b/src/Laravel/Reporters/LaravelLogReporter.php @@ -3,14 +3,18 @@ namespace Henzeb\Enumhancer\Laravel\Reporters; use BackedEnum; -use Illuminate\Support\Facades\Log; -use Henzeb\Enumhancer\Enums\LogLevel; use Henzeb\Enumhancer\Contracts\Reporter; +use Henzeb\Enumhancer\Enums\LogLevel; +use Illuminate\Support\Facades\Log; use function class_basename; class LaravelLogReporter implements Reporter { private readonly ?LogLevel $level; + + /** + * @var string[] + */ private array $channels; public function __construct( @@ -26,6 +30,9 @@ private function getLevel(): string return (string)($this->level ?? LogLevel::default() ?? LogLevel::Notice)->value(); } + /** + * @return string[] + */ private function getChannels(): array { if (empty($this->channels)) { diff --git a/src/Laravel/Rules/EnumBitmask.php b/src/Laravel/Rules/EnumBitmask.php index a63d5c2..8a64f2b 100644 --- a/src/Laravel/Rules/EnumBitmask.php +++ b/src/Laravel/Rules/EnumBitmask.php @@ -12,8 +12,10 @@ class EnumBitmask implements Rule { private mixed $value = null; - public function __construct(private readonly string $type, private readonly bool $singleBit = false) - { + public function __construct( + private readonly string $type, + private readonly bool $singleBit = false + ) { EnumCheck::check($type); if (!EnumImplements::bitmasks($type)) { @@ -38,6 +40,9 @@ public function passes($attribute, $value): bool return EnumBitmasks::isValidBitmask($this->type, $value); } + /** + * @return string|string[] + */ public function message(): string|array { $message = trans( diff --git a/src/Laravel/Rules/EnumTransition.php b/src/Laravel/Rules/EnumTransition.php index e162a25..2eb3c03 100644 --- a/src/Laravel/Rules/EnumTransition.php +++ b/src/Laravel/Rules/EnumTransition.php @@ -45,7 +45,7 @@ public function passes($attribute, $value) /** * Get the validation error message. * - * @return array + * @return string[] */ public function message() { diff --git a/src/Laravel/Rules/IsEnum.php b/src/Laravel/Rules/IsEnum.php index 3c11077..1c08a83 100644 --- a/src/Laravel/Rules/IsEnum.php +++ b/src/Laravel/Rules/IsEnum.php @@ -14,13 +14,13 @@ class IsEnum implements Rule /** - * @var array|array[]|Mapper[]|null[]|string[] + * @var array|array|Mapper[]|null[]|string[] */ private array $mappers; /** * @param string $type - * @param Mapper|string|array|null ...$mappers + * @param array|array|Mapper[]|null[]|string[] $mappers */ public function __construct(private readonly string $type, Mapper|string|array|null ...$mappers) { @@ -39,7 +39,7 @@ public function passes($attribute, $value): bool } /** - * @return string|array + * @return string|string[] */ public function message(): string|array { diff --git a/tests/Fixtures/ExtractBackedEnum.php b/tests/Fixtures/ExtractBackedEnum.php index fbf4d77..50c1376 100644 --- a/tests/Fixtures/ExtractBackedEnum.php +++ b/tests/Fixtures/ExtractBackedEnum.php @@ -2,9 +2,6 @@ namespace Henzeb\Enumhancer\Tests\Fixtures; -use Henzeb\Enumhancer\Contracts\Mapper; -use Henzeb\Enumhancer\Concerns\Enhancers; -use Henzeb\Enumhancer\Concerns\Constructor; use Henzeb\Enumhancer\Concerns\Extractor; enum ExtractBackedEnum: string diff --git a/tests/Fixtures/IntBackedEnum.php b/tests/Fixtures/IntBackedEnum.php index e44e167..29e4bd1 100644 --- a/tests/Fixtures/IntBackedEnum.php +++ b/tests/Fixtures/IntBackedEnum.php @@ -5,12 +5,10 @@ use Henzeb\Enumhancer\Concerns\Bitmasks; -use Henzeb\Enumhancer\Concerns\Configure; +use Henzeb\Enumhancer\Concerns\Comparison; use Henzeb\Enumhancer\Concerns\ConfigureDefaults; use Henzeb\Enumhancer\Concerns\Defaults; use Henzeb\Enumhancer\Concerns\Getters; -use Henzeb\Enumhancer\Concerns\Makers; -use Henzeb\Enumhancer\Concerns\Comparison; /** * @method isTest() bool @@ -22,7 +20,7 @@ */ enum IntBackedEnum: int { - use Makers, Getters, Comparison, Bitmasks, ConfigureDefaults, Defaults; + use Getters, Comparison, Bitmasks, ConfigureDefaults, Defaults; case TEST = 0; case TEST_2 = 1; diff --git a/tests/Fixtures/StringBackedGetEnum.php b/tests/Fixtures/StringBackedGetEnum.php index 2207e9b..92786e9 100644 --- a/tests/Fixtures/StringBackedGetEnum.php +++ b/tests/Fixtures/StringBackedGetEnum.php @@ -6,12 +6,11 @@ use Henzeb\Enumhancer\Concerns\Getters; use Henzeb\Enumhancer\Concerns\State; -use Henzeb\Enumhancer\Concerns\Makers; enum StringBackedGetEnum: string { - use Makers, Getters, State; + use Getters, State; case TEST = 'TEST'; diff --git a/tests/Fixtures/UnitEnums/Getters/GetUnitEnum.php b/tests/Fixtures/UnitEnums/Getters/GetUnitEnum.php index 34ad0a5..5cefa72 100644 --- a/tests/Fixtures/UnitEnums/Getters/GetUnitEnum.php +++ b/tests/Fixtures/UnitEnums/Getters/GetUnitEnum.php @@ -4,11 +4,10 @@ use Henzeb\Enumhancer\Concerns\From; use Henzeb\Enumhancer\Concerns\Getters; -use Henzeb\Enumhancer\Concerns\Makers; enum GetUnitEnum { - use Makers, Getters, From; + use Getters, From; case Zero; case One; diff --git a/tests/Unit/Concerns/MakersTest.php b/tests/Unit/Concerns/MakersTest.php deleted file mode 100644 index 9d29c91..0000000 --- a/tests/Unit/Concerns/MakersTest.php +++ /dev/null @@ -1,180 +0,0 @@ -expectException(ValueError::class); - StringBackedGetEnum::make(null); - } - - public function testExpectValueErrorWhenMakeUnknownValue() - { - $this->expectException(ValueError::class); - StringBackedGetEnum::make('RANDOM_UNKNOWN_VALUE'); - } - - public function testMake() - { - $this->assertEquals( - StringBackedGetEnum::TEST, - StringBackedGetEnum::make('TEST') - ); - } - - public function testMakeStrToUpper() - { - $this->assertEquals( - StringBackedGetEnum::TEST, - StringBackedGetEnum::make('test') - ); - } - - public function testMakeByValue() - { - $this->assertEquals( - StringBackedGetEnum::TEST1, - StringBackedGetEnum::make('Different') - ); - } - - public function testMakeByValueStrToUpper() - { - $this->assertEquals( - StringBackedGetEnum::TEST_STRING_TO_UPPER, - StringBackedGetEnum::make('stringtoupper') - ); - } - - public function testMakeByValueOnIntbackedEnum() - { - $this->assertEquals( - IntBackedEnum::TEST, - IntBackedEnum::make(0) - ); - } - - public function testTryMakeReturnNullWhenDoesNotExist() - { - $this->assertNull( - StringBackedGetEnum::tryMake('DOES NOT EXISTS') - ); - } - - public function testTryMakeByName() - { - $this->assertEquals( - StringBackedGetEnum::TEST, - StringBackedGetEnum::tryMake('TEST') - ); - } - - public function testTryMakeByValue() - { - $this->assertEquals( - StringBackedGetEnum::TEST1, - StringBackedGetEnum::tryMake('different') - ); - } - - public function testTryMakeByValueOnIntbackedEnum() - { - $this->assertEquals( - IntBackedEnum::TEST, - IntBackedEnum::tryMake(0) - ); - } - - public function testMakeArray() - { - $this->assertEquals( - [ - StringBackedGetEnum::TEST, - StringBackedGetEnum::TEST1, - StringBackedGetEnum::TEST_STRING_TO_UPPER - ], - StringBackedGetEnum::makeArray(['TEST', 'different', 'stringtoupper']) - ); - } - - public function testMakeArrayWithGenerator() - { - $this->assertEquals( - [ - StringBackedGetEnum::TEST, - StringBackedGetEnum::TEST1, - StringBackedGetEnum::TEST_STRING_TO_UPPER - ], - StringBackedGetEnum::makeArray( - (function (): Generator { - yield 'TEST'; - yield 'different'; - yield 'stringtoupper'; - })() - ) - ); - } - - public function testMakeArrayFails() - { - $this->expectException(ValueError::class); - - StringBackedGetEnum::makeArray(['DOES_NOT_EXIST']); - } - - public function testTryMakeArray() - { - $this->assertEquals( - [ - StringBackedGetEnum::TEST, - StringBackedGetEnum::TEST1, - StringBackedGetEnum::TEST_STRING_TO_UPPER - ], - StringBackedGetEnum::tryMakeArray(['TEST', 'different', 'stringtoupper', 'DOES_NOT_EXIST']) - ); - } - - public function testTryMakeArrayWithGenerator() - { - $this->assertEquals( - [ - StringBackedGetEnum::TEST, - StringBackedGetEnum::TEST1, - StringBackedGetEnum::TEST_STRING_TO_UPPER - ], - StringBackedGetEnum::tryMakeArray( - (function (): Generator { - yield 'TEST'; - yield 'different'; - yield 'stringtoupper'; - yield 'DOES_NOT_EXIST'; - })() - ) - ); - } - - public function testMakeStringBackedEnumWithInteger() - { - $this->assertEquals( - StringBackedGetEnum::TEST1, StringBackedGetEnum::make(1) - ); - } - - public function testMakeUnitEnumWithInteger() - { - $this->assertEquals( - GetUnitEnum::Zero, GetUnitEnum::make(0) - ); - } -} diff --git a/tests/Unit/Concerns/MappersMakersTest.php b/tests/Unit/Concerns/MappersMakersTest.php deleted file mode 100644 index ea990c6..0000000 --- a/tests/Unit/Concerns/MappersMakersTest.php +++ /dev/null @@ -1,319 +0,0 @@ - EnhancedBackedEnum::ENUM - ]; - } - }; - } - - public function testMakeShouldWorkWithoutMapper() - { - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::make('ENUM') - ); - } - - public function testMakeShouldErrorWithoutMapper() - { - $this->expectException(ValueError::class); - EnhancedBackedEnum::make('mappedEnum'); - } - - public function testMakeShouldMap() - { - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::make('mappedEnum', $this->getMapper()) - ); - } - - public function testMakeShouldMapWithStringMap() - { - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::make('mappedEnum', $this->getMapper()::class) - ); - } - - public function testMakeShouldThrowExceptionForNonMap() - { - $this->expectException(RuntimeException::class); - EnhancedBackedEnum::make('mappedEnum', $this::class); - } - - public function testMakeShouldNotMapWhenNull() - { - $this->expectException(ValueError::class); - EnhancedBackedEnum::make(null, $this->getMapper()); - } - - public function testMakeShouldMapWithoutMapperGiven() - { - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::make('anotherMappedEnum') - ); - } - - public function testMakeShouldErrorWithMap() - { - $this->expectException(ValueError::class); - EnhancedBackedEnum::make('not existing', $this->getMapper()); - } - - public function testTryMakeShouldWorkWithoutMapper() - { - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::tryGet('ENUM') - ); - } - - public function testTryGetShouldReturnNullWithoutMapper() - { - $this->assertNull(EnhancedBackedEnum::tryGet('mappedEnum')); - } - - public function testTryGetShouldNotMapWhenNull() - { - - $this->assertNull( - EnhancedBackedEnum::tryGet(null, $this->getMapper()) - ); - } - - public function testTryGetShouldMap() - { - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::tryGet('mappedEnum', $this->getMapper()) - ); - } - - public function testTryGetShouldMapWithoutMapperGiven() - { - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::tryGet('anotherMappedEnum') - ); - } - - - public function testTryGetShouldReturnNullWithMap() - { - $this->assertNull(EnhancedBackedEnum::tryGet('not existing', $this->getMapper())); - } - - - public function testGetArrayShouldNotMapWhenNull() - { - $this->expectException(ValueError::class); - EnhancedBackedEnum::makeArray([null], $this->getMapper()); - } - - public function testGetArrayShouldWorkWithoutMapper() - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::makeArray(['ENUM']) - ); - } - - public function testGetArrayShouldThrowErrorWorkWithoutMapper() - { - $this->expectException(ValueError::class); - EnhancedBackedEnum::makeArray(['Does Not exist']); - } - - public function testGetArrayShouldWorkWitMapper() - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::tryMakeArray(['mappedEnum'], $this->getMapper()) - ); - } - - - public function testGetArrayShouldMapWithoutMapperGiven() - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::GetArray(['anotherMappedEnum']) - ); - } - - public function testGetArrayShouldThrowErrorWitMapper() - { - $this->expectException(ValueError::class); - EnhancedBackedEnum::makeArray(['ENUM', 'doesNotExist'], $this->getMapper()); - } - - public function testTryGetArrayShouldWorkWithoutMapper() - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::tryMakeArray(['ENUM', 'DoesNotExist']) - ); - } - - public function testTryGetArrayShouldNotMapWhenNull() - { - $this->assertEquals([], EnhancedBackedEnum::tryMakeArray([null], $this->getMapper())); - } - - public function testTryGetArrayShouldWorkWitMapper() - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::tryMakeArray(['mappedEnum', 'DoesNotExist'], $this->getMapper()) - ); - } - - public function testtryArrayShouldMapWithoutMapperGiven() - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::tryMakeArray(['anotherMappedEnum']) - ); - } - - public function testShouldUseMapperWhenConstructorIsUsed() - { - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::anotherMappedEnum() - ); - } - - public function testShouldExtractWithDefaultMappedKey() - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::extract('This text contains anotherMappedEnum for you') - ); - } - - public function testShouldExtractWithMappedKey() - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::extract('This text contains mappedEnum for you', $this->getMapper()) - ); - } - - public function testShouldExtractWithMappedKeyAndDefaultMappedKey() - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM, EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::extract( - 'This text contains mappedEnum and anotherMappedEnum for you', - $this->getMapper() - ) - ); - } - - public function testShouldAcceptEnumsAsValue(): void - { - //EnhancedUnitEnum::Mapped->name => EnhancedBackedEnum::ANOTHER_ENUM - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::tryMake(EnhancedBackedEnum::ENUM) - ); - - $this->assertEquals( - EnhancedBackedEnum::ANOTHER_ENUM, - EnhancedBackedEnum::tryMake(EnhancedUnitEnum::Mapped) - ); - - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::tryMake(EnhancedUnitEnum::ENUM) - ); - - $this->assertNull( - EnhancedBackedEnum::tryMake(EnhancedUnitEnum::Unique) - ); - - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::make(EnhancedBackedEnum::ENUM) - ); - - $this->assertEquals( - EnhancedBackedEnum::ANOTHER_ENUM, - EnhancedBackedEnum::make(EnhancedUnitEnum::Mapped) - ); - - $this->assertEquals( - EnhancedBackedEnum::ENUM, - EnhancedBackedEnum::make(EnhancedUnitEnum::ENUM) - ); - - $this->expectException(ValueError::class); - EnhancedBackedEnum::make(EnhancedUnitEnum::Unique); - } - - public function testShouldAcceptEnumsAsValueArrays(): void - { - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::tryMakeArray([EnhancedBackedEnum::ENUM]) - ); - - $this->assertEquals( - [EnhancedBackedEnum::ANOTHER_ENUM], - EnhancedBackedEnum::tryMakeArray([EnhancedUnitEnum::Mapped]) - ); - - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::tryMakeArray([EnhancedUnitEnum::ENUM]) - ); - - $this->assertEquals( - [], - EnhancedBackedEnum::tryMakeArray([EnhancedUnitEnum::Unique]) - ); - - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::tryMakeArray([EnhancedBackedEnum::ENUM]) - ); - - $this->assertEquals( - [EnhancedBackedEnum::ENUM], - EnhancedBackedEnum::makeArray([EnhancedUnitEnum::ENUM]) - ); - - $this->assertEquals( - [EnhancedBackedEnum::ANOTHER_ENUM], - EnhancedBackedEnum::makeArray([EnhancedUnitEnum::Mapped]) - ); - - $this->expectException(ValueError::class); - EnhancedBackedEnum::makeArray([EnhancedUnitEnum::Unique]); - } -} diff --git a/tests/Unit/Concerns/ReportersMakersTest.php b/tests/Unit/Concerns/ReportersMakersTest.php deleted file mode 100644 index f4f9809..0000000 --- a/tests/Unit/Concerns/ReportersMakersTest.php +++ /dev/null @@ -1,198 +0,0 @@ - [ReporterTestEnum::class], - 'with-mappers-enhancment' => [EnhancedBackedEnum::class] - ]; - } - - - /** - * @param string|Enhancers $enum - * @return void - * - * @dataProvider providesEnumsToTestWith - */ - public function testExistingEnum(string $enum) - { - $this->assertEquals( - $enum::ENUM, - $enum::makeOrReport('ENUM') - ); - } - - /** - * @param string|Enhancers $enum - * @return void - * - * @dataProvider providesEnumsToTestWith - */ - public function testNoreporting(string $enum) - { - $this->assertNull($enum::makeOrReport('NOT EXIST')); - } - - /** - * @param string|Enhancers $enum - * @return void - * - * @dataProvider providesEnumsToTestWith - */ - public function testDoesReport(string $enum) - { - $reporter = Mockery::mock(Reporter::class) - ->shouldReceive( - 'report' - )->once()->getMock(); - - EnumReporter::set( - $reporter - ); - - $this->assertNull($enum::makeOrReport('NOT EXIST')); - } - - /** - * @param string|Enhancers $enum - * @return void - * - * @dataProvider providesEnumsToTestWith - */ - public function testOverridesGlobalReporterWithNull(string $enum) - { - $globalReporter = Mockery::mock(Reporter::class) - ->shouldReceive( - 'report' - )->never()->getMock(); - - EnumReporter::set( - $globalReporter - ); - - $this->assertNull(NotReportingEnum::makeOrReport('NOT EXIST')); - } - - public function testOverridesGlobalReporterWithOwnReporter() - { - $globalReporter = Mockery::mock(Reporter::class) - ->shouldReceive( - 'report' - )->never()->getMock(); - - $customReporter = Mockery::mock(Reporter::class) - ->shouldReceive( - 'report' - )->once()->getMock(); - - EnumReporter::set( - $globalReporter - ); - - CustomReportingEnum::property('reporter', $customReporter); - - $this->assertNull(CustomReportingEnum::makeOrReport('NOT EXIST')); - } - - /** - * @param string|Enhancers $enum - * @return void - * - * @dataProvider providesEnumsToTestWith - */ - public function testExistingEnums(string $enum) - { - $this->assertEquals( - [$enum::ENUM, $enum::ANOTHER_ENUM], - $enum::makeOrReportArray(['ENUM', 'ANOTHER_ENUM']) - ); - } - - /** - * @param string|Enhancers $enum - * @return void - * - * @dataProvider providesEnumsToTestWith - */ - public function testNonExistingEnums(string $enum) - { - $globalReporter = Mockery::mock(Reporter::class) - ->shouldReceive( - 'report' - )->once()->getMock(); - - EnumReporter::set($globalReporter); - - $this->assertEquals( - [$enum::ENUM], - $enum::makeOrReportArray(['ENUM', 'DOESNOTEXIST']) - ); - } - - /** - * @param string|Enhancers $enum - * @return void - * - * @dataProvider providesEnumsToTestWith - */ - public function testReportWithContext(string $enum) - { - $reporter = new class implements Reporter { - - public function report(string $enum, ?string $key, ?BackedEnum $context): void - { - enum_exists($context::class); - } - }; - - EnumReporter::set($reporter); - - $this->assertNull($enum::makeOrReport('DOESNOTEXIST', EnhancedBackedEnum::ANOTHER_ENUM)); - - } - - /** - * @param string|Enhancers $enum - * @return void - * - * @dataProvider providesEnumsToTestWith - */ - public function testMakeOrReportArrayWithContext(string $enum) - { - $reporter = new class implements Reporter { - - public function report(string $enum, ?string $key, ?BackedEnum $context): void - { - enum_exists($context::class); - } - }; - - EnumReporter::set($reporter); - - $this->assertEquals([], $enum::makeOrReportArray(['DOESNOTEXIST'], EnhancedBackedEnum::ANOTHER_ENUM)); - } - - protected function tearDown(): void - { - EnumReporter::set(null); - parent::tearDown(); // TODO: Change the autogenerated stub - } -} diff --git a/tests/Unit/Helpers/EnumImplementsTest.php b/tests/Unit/Helpers/EnumImplementsTest.php new file mode 100644 index 0000000..5a804e7 --- /dev/null +++ b/tests/Unit/Helpers/EnumImplementsTest.php @@ -0,0 +1,28 @@ + throw new Exception($message)); + + try { + EnumImplements::doesNotExist(SimpleEnum::class); + restore_error_handler(); + } catch (Throwable $e) { + restore_error_handler(); + $this->assertEquals( + 'Call to undefined method Henzeb\Enumhancer\Helpers\EnumImplements::doesNotExist()', + $e->getMessage() + ); + } + } +} diff --git a/tests/Unit/Helpers/EnumSubsetMethodsTest.php b/tests/Unit/Helpers/EnumSubsetMethodsTest.php index 4fe20af..1e80a40 100644 --- a/tests/Unit/Helpers/EnumSubsetMethodsTest.php +++ b/tests/Unit/Helpers/EnumSubsetMethodsTest.php @@ -3,6 +3,7 @@ namespace Henzeb\Enumhancer\Tests\Unit\Helpers; use Henzeb\Enumhancer\Concerns\Dropdown; +use Henzeb\Enumhancer\Helpers\EnumValue; use Henzeb\Enumhancer\Helpers\Subset\EnumSubsetMethods; use Henzeb\Enumhancer\Tests\Fixtures\EnhancedUnitEnum; use Henzeb\Enumhancer\Tests\Fixtures\IntBackedEnum; @@ -166,7 +167,6 @@ private function getNames(array $cases): array private function getValues(array $cases): array { - return array_map(fn($enum) => $enum->value ?? (method_exists($enum, - 'value') ? $enum->value() : null) ?? $enum->name, $cases); + return array_map(fn($enum) => EnumValue::value($enum), $cases); } }