standards icon indicating copy to clipboard operation
standards copied to clipboard

Add ::names ::values ::toArray helper to BackedEnum

Open lakuapik opened this issue 3 years ago • 1 comments

This PR add static function names(), values() and toArray in the BackendEnum class.

use PrinsFrank\Standards\BackedEnum;
use PrinsFrank\Standards\Language\ISO639_1_Alpha_2;

$array = BackedEnum::toArray(ISO639_1_Alpha_2::class);
$values = BackedEnum::values(ISO639_1_Alpha_2::class);
$names = BackedEnum::names(ISO639_1_Alpha_2::class);

This maybe useful and maybe not. Feel free to accept or drop this PR. Cheers :clinking_glasses:


Apart from this, @PrinsFrank what do you think instead of using it with BackedEnum class, use directly in the enum class it self. Like:

use PrinsFrank\Standards\Language\ISO639_1_Alpha_2;

$array = ISO639_1_Alpha_2::toArray();
$values = ISO639_1_Alpha_2::values();
$names = ISO639_1_Alpha_2::names();

// and maybe add toJson too
$json = ISO639_1_Alpha_2::toJson();

lakuapik avatar Jul 24 '22 03:07 lakuapik

Codecov Report

Merging #33 (f4eb3c1) into main (b81e968) will not change coverage. The diff coverage is 100.00%.

@@             Coverage Diff             @@
##                main       #33   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity        48        52    +4     
===========================================
  Files             13        13           
  Lines             97       106    +9     
===========================================
+ Hits              97       106    +9     
Impacted Files Coverage Δ
src/BackedEnum.php 100.00% <100.00%> (ø)

:mega: Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

codecov-commenter avatar Jul 24 '22 03:07 codecov-commenter

@lakuapik Thanks for your contribution! As enums cannot extend other classes and adding traits to implement generic logic across enums is not preferrable, I think your current solution is the best one.

Until now, the BackenEnum class in this package was mainly for internal use, so adding these methods that are not used internally means that any breaking changes to them mean a major semver release. I do think they add value for package users, so I will merge this when this branch is passing.

PrinsFrank avatar Aug 14 '22 16:08 PrinsFrank

@PrinsFrank i've ajusted the codestyle.


Anyway, why adding trait to Enum is not preferable to you, im curious, may i know your reason?

lakuapik avatar Aug 18 '22 14:08 lakuapik

@lakuapik I don't know why, but your pipelines were not starting. I've started them manually and fixed the issues. Apparently, even though the PHPStan documentation says that value-of<> for Backed enums is supported, it isn't. Or at least not in combination with generics.

Now as for your question why adding traits to enums is not preferrable: There are a bunch of issues with traits in PHP, so I try to avoid them in general. I think I've only found 1 valid use case for traits in the past 4 years. An excellent write up can be found here: https://codereview.stackexchange.com/questions/74077/trait-accessing-variables-of-classes-using-it

PrinsFrank avatar Aug 18 '22 18:08 PrinsFrank