From 92662add78eda4eea2fedf051d068d553aa96d05 Mon Sep 17 00:00:00 2001 From: Alexandru Patranescu Date: Tue, 7 Dec 2021 19:19:27 +0200 Subject: [PATCH 01/18] document how migration to native php enums will work --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.md b/README.md index 1e4d1ff..23a3b12 100644 --- a/README.md +++ b/README.md @@ -130,8 +130,56 @@ final class Action extends Enum } ``` +## Native enums and migration +Native enum arrived to PHP in version 8.1: https://www.php.net/enumerations +If your project is running PHP 8.1+ or your library have it as a minimum requirement you should use it instead of this library. + +When migrating from `myclabs/php-enum`, the effort should be small if the usage was in the recommended way: +- private constants +- final class without extending the enum class +- no static method overridden + +Changes for migration: +- Class definition should be changed from +```php +/** + * @method static Action VIEW() + * @method static Action EDIT() + */ +final class Action extends Enum +{ + private const VIEW = 'view'; + private const EDIT = 'edit'; +} +``` + to +```php +enum Action: string +{ + case VIEW = 'view'; + case EDIT = 'edit'; +} +``` +All places where the class was used as a type will continue to work. + +Usages and the change needed: + +| Operation | myclabs/php-enum | native enum | +|----------------------------------------------------------------|----------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Obtain an instance will change from | `$enumCase = Action::VIEW()` | `$enumCase = Action::VIEW` | +| Create an enum from a backed value | `$enumCase = new Action('view')` | `$enumCase = Action::from('view')` | +| Get the backed value of the enum instance | `$enumCase->getValue()` | `$enumCase->value` | +| Compare two enum instances | `$enumCase1 == $enumCase2`
or
`$enumCase1->equals($enumCase2)` | `$enumCase1 === $enumCase2` | +| Get the key/name of the enum instance | `$enumCase->getKey()` | `$enumCase->name` | +| Get a list of all the possible instances of the enum | `Action::values()` | `Action::cases()` | +| Get a map of possible instances of the enum mapped by name | `Action::values()` | `array_combine(array_map(fn($case) => $case->name, Action::cases()), Action::cases())`
or
`(new ReflectionEnum(Action::class))->getConstants()` | +| Get a list of all possible names of the enum | `Action::keys()` | `array_map(fn($case) => $case->name, Action::cases())` | +| Get a list of all possible backed values of the enum | `Action::toArray()` | `array_map(fn($case) => $case->value, Action::cases())` | +| Get a map of possible backed values of the enum mapped by name | `Action::toArray()` | `array_combine(array_map(fn($case) => $case->name, Action::cases()), array_map(fn($case) => $case->value, Action::cases()))`
or
`array_map(fn($case) => $case->value, (new ReflectionEnum(Action::class))->getConstants()))` | + ## Related projects +- [PHP 8.1+ native enum](https://www.php.net/enumerations) - [Doctrine enum mapping](https://github.com/acelaya/doctrine-enum-type) - [Symfony ParamConverter integration](https://github.com/Ex3v/MyCLabsEnumParamConverter) - [PHPStan integration](https://github.com/timeweb/phpstan-enum) From e53bae149bd75c14e2a7bcb240974915794050b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandru=20P=C4=83tr=C4=83nescu?= Date: Wed, 8 Dec 2021 08:20:44 +0200 Subject: [PATCH 02/18] fix the verb conjugation Co-authored-by: Jacob Dreesen --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 23a3b12..910275a 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ final class Action extends Enum ## Native enums and migration Native enum arrived to PHP in version 8.1: https://www.php.net/enumerations -If your project is running PHP 8.1+ or your library have it as a minimum requirement you should use it instead of this library. +If your project is running PHP 8.1+ or your library has it as a minimum requirement you should use it instead of this library. When migrating from `myclabs/php-enum`, the effort should be small if the usage was in the recommended way: - private constants From f3f9aa67d2b0204807c498314a01af7838ff4552 Mon Sep 17 00:00:00 2001 From: Alexandru Patranescu Date: Wed, 8 Dec 2021 15:32:27 +0200 Subject: [PATCH 03/18] reduce the unnecessary details --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 910275a..cd813b1 100644 --- a/README.md +++ b/README.md @@ -136,8 +136,8 @@ If your project is running PHP 8.1+ or your library has it as a minimum requirem When migrating from `myclabs/php-enum`, the effort should be small if the usage was in the recommended way: - private constants -- final class without extending the enum class -- no static method overridden +- final classes +- no method overridden Changes for migration: - Class definition should be changed from From eb6410282801fa4511855de001c2c1deb6e4bf50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sat, 16 Apr 2022 09:39:34 +0200 Subject: [PATCH 04/18] Implement Stringable interface on Enum --- composer.json | 5 ++++- src/Enum.php | 2 +- stubs/Stringable.php | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 stubs/Stringable.php diff --git a/composer.json b/composer.json index 924f924..978cb19 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,10 @@ "autoload": { "psr-4": { "MyCLabs\\Enum\\": "src/" - } + }, + "classmap": [ + "stubs/Stringable.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/src/Enum.php b/src/Enum.php index 89064eb..4c94cf6 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -19,7 +19,7 @@ * @psalm-immutable * @psalm-consistent-constructor */ -abstract class Enum implements \JsonSerializable +abstract class Enum implements \JsonSerializable, \Stringable { /** * Enum value diff --git a/stubs/Stringable.php b/stubs/Stringable.php new file mode 100644 index 0000000..4811af7 --- /dev/null +++ b/stubs/Stringable.php @@ -0,0 +1,11 @@ + Date: Sat, 14 May 2022 16:24:51 +0200 Subject: [PATCH 05/18] Added PHP 8.1 to CI configuration --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e3b6e64..25b3372 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -19,6 +19,7 @@ jobs: - "7.3" - "7.4" - "8.0" + - "8.1" dependencies: - "highest" include: From 0a08c9f24718cb765c6eadf14fb0ca146b934068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Mon, 16 May 2022 02:29:22 +0000 Subject: [PATCH 06/18] Fix Shepherd link in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d72323f..d874efb 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![GitHub Actions][GA Image]][GA Link] [![Latest Stable Version](https://poser.pugx.org/myclabs/php-enum/version.png)](https://packagist.org/packages/myclabs/php-enum) [![Total Downloads](https://poser.pugx.org/myclabs/php-enum/downloads.png)](https://packagist.org/packages/myclabs/php-enum) -[![Psalm Shepherd][Psalm Shepherd Image]][Psalm Shepherd Link] +[![Psalm Shepherd][Shepherd Image]][Shepherd Link] Maintenance for this project is [supported via Tidelift](https://tidelift.com/subscription/pkg/packagist-myclabs-php-enum?utm_source=packagist-myclabs-php-enum&utm_medium=referral&utm_campaign=readme). From 99a5bf3fa504ffd6e496748532222a04db67fedb Mon Sep 17 00:00:00 2001 From: Roman Varkuta Date: Fri, 17 Jun 2022 14:24:21 +0300 Subject: [PATCH 07/18] yii2-enum is abandoned and not supporting anymore --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index d874efb..681d55e 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,6 @@ Usages and the change needed: - [Doctrine enum mapping](https://github.com/acelaya/doctrine-enum-type) - [Symfony ParamConverter integration](https://github.com/Ex3v/MyCLabsEnumParamConverter) - [PHPStan integration](https://github.com/timeweb/phpstan-enum) -- [Yii2 enum mapping](https://github.com/KartaviK/yii2-enum) [GA Image]: https://github.com/myclabs/php-enum/workflows/CI/badge.svg From 5d1e09ef0c4bef8b9491f03c56d6ee4759e5e68f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1chym=20Tou=C5=A1ek?= Date: Fri, 28 Oct 2022 08:42:37 +0200 Subject: [PATCH 08/18] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 681d55e..948f374 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ use MyCLabs\Enum\Enum; /** * Action enum + * + * @extends Enum */ final class Action extends Enum { From 85a33645c5b5dd4edfada1381bd818485f2ccded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Tue, 13 Dec 2022 11:13:58 +0100 Subject: [PATCH 09/18] Upgrade to Psalm 5 --- composer.json | 2 +- src/Enum.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 978cb19..67612d0 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,6 @@ "require-dev": { "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "1.*", - "vimeo/psalm": "^4.6.2" + "vimeo/psalm": "^5.2" } } diff --git a/src/Enum.php b/src/Enum.php index 4c94cf6..1bd5592 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -176,6 +176,7 @@ public static function values() /** @psalm-var T $value */ foreach (static::toArray() as $key => $value) { + /** @psalm-suppress UnsafeGenericInstantiation */ $values[$key] = new static($value); } @@ -297,6 +298,7 @@ public static function __callStatic($name, $arguments) $message = "No static method or enum constant '$name' in class " . static::class; throw new \BadMethodCallException($message); } + /** @psalm-suppress UnsafeGenericInstantiation */ return self::$instances[$class][$name] = new static($array[$name]); } return clone self::$instances[$class][$name]; @@ -308,7 +310,6 @@ public static function __callStatic($name, $arguments) * * @return mixed * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @psalm-pure */ #[\ReturnTypeWillChange] public function jsonSerialize() From f9a24befc55270c323167cc8266f59b3dfd3c51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Tue, 13 Dec 2022 11:43:12 +0100 Subject: [PATCH 10/18] Support Psalm v4 and v5 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 67612d0..2513db7 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,6 @@ "require-dev": { "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "1.*", - "vimeo/psalm": "^5.2" + "vimeo/psalm": "^4.6.2 || ^5.2" } } From 9394a8eda18e9e0de7bb80575ca5b4e1fc6c192b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Tue, 13 Dec 2022 12:22:32 +0100 Subject: [PATCH 11/18] PHP 8.2 CI Support --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 25b3372..de91a2c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,6 +20,7 @@ jobs: - "7.4" - "8.0" - "8.1" + - "8.2" dependencies: - "highest" include: From 35b96941f167bd289f8c1811dc8585e73432f815 Mon Sep 17 00:00:00 2001 From: Giorgio Scalvini <139172449+g-scalvini@users.noreply.github.com> Date: Thu, 15 Feb 2024 13:56:24 +0100 Subject: [PATCH 12/18] HTTP to HTTPS for homepage link in composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2513db7..eab6263 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "library", "description": "PHP Enum implementation", "keywords": ["enum"], - "homepage": "http://github.com/myclabs/php-enum", + "homepage": "https://github.com/myclabs/php-enum", "license": "MIT", "authors": [ { From ae3fb27dbc8bebb7ad678e5d9131d84e6eb456d2 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Fri, 13 Sep 2024 19:39:16 +0200 Subject: [PATCH 13/18] Update ci.yaml --- .github/workflows/ci.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index de91a2c..a75731e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -5,8 +5,6 @@ on: push: branches: - "master" - schedule: - - cron: "42 3 * * *" jobs: phpunit: From a1c7bcfdfeb054b4f1bba0f5c2d1bec382898eaa Mon Sep 17 00:00:00 2001 From: "M. Vugteveen" Date: Mon, 13 Jan 2025 19:54:14 +0100 Subject: [PATCH 14/18] fix php8.4 deprecated --- src/PHPUnit/Comparator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPUnit/Comparator.php b/src/PHPUnit/Comparator.php index 302bf80..7c65e4e 100644 --- a/src/PHPUnit/Comparator.php +++ b/src/PHPUnit/Comparator.php @@ -43,7 +43,7 @@ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = f ); } - private function formatEnum(Enum $enum = null) + private function formatEnum(?Enum $enum = null) { if ($enum === null) { return "null"; From ada961a2824c5d709be48d6b108ae714fe2662e2 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Tue, 14 Jan 2025 09:59:51 +0100 Subject: [PATCH 15/18] Update ci.yaml --- .github/workflows/ci.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a75731e..cf8037c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -27,7 +27,7 @@ jobs: steps: - name: "Checkout" - uses: "actions/checkout@v2" + uses: "actions/checkout@v4" with: fetch-depth: 2 @@ -47,7 +47,7 @@ jobs: run: "vendor/bin/phpunit --coverage-clover=coverage.xml" - name: "Upload coverage file" - uses: "actions/upload-artifact@v2" + uses: "actions/upload-artifact@v4" with: name: "phpunit-${{ matrix.deps }}-${{ matrix.php-version }}.coverage" path: "coverage.xml" From a462e42ba36b2bff85695e616f3b6b086f2d4aeb Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Tue, 14 Jan 2025 10:00:05 +0100 Subject: [PATCH 16/18] Update static-analysis.yaml --- .github/workflows/static-analysis.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/static-analysis.yaml b/.github/workflows/static-analysis.yaml index 358b8c6..017d4a6 100644 --- a/.github/workflows/static-analysis.yaml +++ b/.github/workflows/static-analysis.yaml @@ -13,7 +13,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Psalm uses: docker://vimeo/psalm-github-actions:4.9.3 From 347013faaba789a283d46a55581958f2ba469bdb Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Tue, 14 Jan 2025 10:09:47 +0100 Subject: [PATCH 17/18] Update ci.yaml --- .github/workflows/ci.yaml | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index cf8037c..7863acf 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -35,7 +35,6 @@ jobs: uses: "shivammathur/setup-php@v2" with: php-version: "${{ matrix.php-version }}" - coverage: "pcov" ini-values: "zend.assertions=1" - name: "Install dependencies with Composer" @@ -44,10 +43,4 @@ jobs: dependency-versions: "${{ matrix.dependencies }}" - name: "Run PHPUnit" - run: "vendor/bin/phpunit --coverage-clover=coverage.xml" - - - name: "Upload coverage file" - uses: "actions/upload-artifact@v4" - with: - name: "phpunit-${{ matrix.deps }}-${{ matrix.php-version }}.coverage" - path: "coverage.xml" + run: "vendor/bin/phpunit" From eb3eefdf91307a002b9e3ce4f6d25034c94265a7 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Thu, 20 Feb 2025 10:36:09 +0100 Subject: [PATCH 18/18] Correct pugx badges This replaces the extension no the pugx badges for version and total downloads. The current png version is failing due to request redirection combined with [GitHub Camo](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/about-anonymized-urls) as mentioned in https://github.com/PUGX/badge-poser/issues/1195 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 948f374..2bf98cd 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # PHP Enum implementation inspired from SplEnum [![GitHub Actions][GA Image]][GA Link] -[![Latest Stable Version](https://poser.pugx.org/myclabs/php-enum/version.png)](https://packagist.org/packages/myclabs/php-enum) -[![Total Downloads](https://poser.pugx.org/myclabs/php-enum/downloads.png)](https://packagist.org/packages/myclabs/php-enum) +[![Latest Stable Version](https://poser.pugx.org/myclabs/php-enum/version.svg)](https://packagist.org/packages/myclabs/php-enum) +[![Total Downloads](https://poser.pugx.org/myclabs/php-enum/downloads.svg)](https://packagist.org/packages/myclabs/php-enum) [![Psalm Shepherd][Shepherd Image]][Shepherd Link] Maintenance for this project is [supported via Tidelift](https://tidelift.com/subscription/pkg/packagist-myclabs-php-enum?utm_source=packagist-myclabs-php-enum&utm_medium=referral&utm_campaign=readme).