Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the EnumType class to align its behavior with other type classes by making marshal() return null for invalid values instead of throwing exceptions. The toDatabase() method logic is also simplified and reorganized for better clarity.
Key Changes
- Modified
marshal()to returnnullfor invalid values instead of throwingInvalidArgumentException, catchingValueErrorandTypeErrorexceptions - Refactored
toDatabase()to simplify type checking and validation logic, with improved handling of string-to-int conversion for integer-backed enums - Removed PHPStan baseline entry for a false positive that no longer applies after the code cleanup
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Database/Type/EnumType.php | Refactored marshal() and toDatabase() methods to handle invalid values consistently, added TypeError and ValueError imports for exception handling |
| tests/TestCase/Database/Type/EnumTypeTest.php | Updated tests to verify marshal() returns null for invalid values, added test for type validation in toDatabase(), removed obsolete tests that expected exceptions from marshal() |
| phpstan-baseline.neon | Removed baseline entry for "Result of && is always false" in EnumType that is no longer applicable after the refactoring |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
c275234 to
014d538
Compare
Trying to marshal an invalid value now results in null. This is inline with how other types classes handle invalid values.
014d538 to
df25168
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Is this maybe something we want to harden/enforce in future cake versions? Because sure, normally we'd expect users to have validation present to make sure valid values are being passed down the entity, but having Just looked up a few cases:
In general we always seem to coerse an empty string to null which is fine i guess, but any other "value" being passed down to a database type should still be valid imho. |
So what to you want to happen instead, if an invalid value cannot be marshalled to an enum instance? |
|
I'd throw an InvalidArgumentException based exception just like we do in BoolType, DecimalType, IntegerType. If it should be try-catched upwards is up for debate, but in general I'd like to remove that kind of "soft fallback" for invalid values on a database type layer. If I'd quote you: What is the usecase for having this soft fallback to a |
The use case is to not have the app blow up for invalid user provided data. You mentioned expecting users to have validation rules but a validation failure doesn't prevent marshalling, the marshaller will try try to set the value for the entity field/property. So throwing an exception would mean user will just can a general error page/response instead of the page/response with validation errors. |
|
Makes sense. |
|
Well thats what I meant by
All I am referencing here is the fact, that we already do throw exceptions in certain database types if the given value can't be processed for that specific type. I know validation is only being called, if you do If an entity is built via request data and its not using If an entity is manually built via e.g. a CLI command sync and saved without going through validation its also the programmers choice of doing so, therefore it should also throw an exception if something goes wrong and not default to This feels like a hidden escape gateway for bugs to happen without anyone knowing why. And if we need to have a separate exception for that which can be caught in between to do that, then why not add it. |
Wouldn't those bugs often be caught by application validation? Developers have the ability to use I think it is useful to align the behavior of the EnumType with other types which by and large return null on invalid data. |
|
We could, or users could, add options to always throw if they want that behavior. If we can fail to marshal, you can fail to validate. Just depends where you want that last stand to be. |
Yes, thats exactly my point. Validation would catch that, but validation only happens when you do I think I am going to prepare a separate issue/PR explaining more in detail (with tests) what I am trying to achieve here. For now I am fine with this as well to align the EnumType with the others. |
There are application rules too if you want to safeguard against invalid values set using |
Trying to marshal an invalid value now results in null. This is inline with how other types classes handle invalid values.