Comments (8)
I also prefer TEnumValue
in this case.
from enum.
Hey,
At first: I'm not against this!
But even if value is an integer the construct still accepts integer and string. As it's instantiatable by the value and methodname.
Which is also always required for StatusEnum::DRAFT()
.
So it would be more correct to adjust the type of read-only property $value
. 🤔
from enum.
Hi Gummibeer,
I see what you're saying (in regards to the instantiation). Although not quite following with that last line.
But in regards to how it will affect the DX,
If the following were replaced with templates:
- https://github.com/spatie/enum/blob/master/src/Enum.php#L38
- https://github.com/spatie/enum/blob/master/src/Enum.php#L52
- https://github.com/spatie/enum/blob/master/src/Enum.php#L256
Then when calling StatusEnum::DRAFT()->value
you can remove the ambiguity that it may be an int or a string.
This enforcement (albeit only caught by the IDE and static analysis) can greatly help in a type-safe environment, where a downstream, maybe relying on it being backed by ints.
Could also prevent issues where you're storing ints in your database, and a newcomer comes to the team and attempts to add a string to the value set.
Granted, these issues should also be fixed in tests & code reviews. just feels any opportunity to catch this sort of issue would be awesome!
from enum.
I'm not really into templates - but so far I got this would allow a user to override @template EnumValue
with int
only instead of the packages default int|string
and this would adjust everywhere in the corresponding class!?
I totally like type-safety and if I got it right: go ahead!
If I haven't got it: please correct me. 🙈
For the __construct()
we could use EnumValue|string
? 🤔 This would make it clear that we allow whatever your value is and the string
on top.
from enum.
I'm not really into templates - but so far I got this would allow a user to override
@template EnumValue
That's correct, I believe it's often described as PHP's answer to Generics!
I'm also I planning on contributing to Laravel to get an uptake in this: laravel/framework#34110
Hadn't considered using EnumValue|string
for the constructor, but this is a great idea!
PR Inbound!
from enum.
One thing, if it's possible I would prefer a namespaced template to prevent conflicts.
If not can we go with SpatieEnumValue
?
from enum.
Worth noting: https://psalm.dev/docs/annotating_code/templated_annotations/
The names of your templated types (e.g. TKey, TValue don't matter outside the scope of the class or function in which they're declared.
(So, I was originally gonna be lazy and use T
) I believe it's the same behaviour with PHPStan
Happy to use SpatieEnumValue
, but may be better to use either TEnumValue
, TSpatieEnumValue
to maintain the convention of using T to dictate it's a template?
I have no preference as such, but personally think TEnumValue
looks better!
Thoughts?
from enum.
Also, (following some reading) looks like it'll need a slight update to spatie/laravel-enum
for the inheritance (happy to sort myself also)!
See:
- https://psalm.dev/r/1ede5efb5d
- https://psalm.dev/docs/annotating_code/templated_annotations/#template-inheritance
from enum.
Related Issues (20)
- Enum serialized as empty array when using spatie/laravel-event-sourcing and spatie/data-transfer-object HOT 3
- Doc link is broken HOT 2
- Strong typed enums problems HOT 2
- Unit test overridden values HOT 3
- Enums with Laravel Octane HOT 2
- initialize Enum with values coming from DB? HOT 2
- PHPUnit assertions trait changed to class HOT 6
- Too few arguments to function Spatie\Enum\Enum::__construct(), 0 passed HOT 3
- OPcache strip comments problem HOT 2
- Missing documentation for ->isXy() HOT 1
- Deprecate values HOT 1
- Localised labels aren't update after locale is changed HOT 2
- can php7.2 (or even 7.3) be supported? HOT 2
- Extend already extended HOT 2
- Creating enum using method call from within enum throws UnknownEnumMethod HOT 6
- Future of this package after PHP 8.1 HOT 7
- Support standard strict comparison HOT 5
- PHP8.1 Compatibility: check properties HOT 1
- Fix "Creating an enum from a value" example in documentation HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from enum.