Fix factory when validator has no constructor#1605
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1605 +/- ##
=========================================
Coverage 97.46% 97.47%
- Complexity 991 993 +2
=========================================
Files 211 211
Lines 2292 2298 +6
=========================================
+ Hits 2234 2240 +6
Misses 58 58 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug in the NamespacedValidatorFactory that prevented validators without constructors (like v::version() and v::attributes()) from being instantiated through the builder chain API. The fix checks if a class has a constructor before attempting to pass arguments to it.
Changes:
- Added logic to handle validators without constructors in
NamespacedValidatorFactory - Created a test validator
NoConstructorto support testing this scenario - Added a unit test to verify the factory can create validators that have no constructor
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| library/NamespacedValidatorFactory.php | Added check for null constructor before calling newInstanceArgs() to support validators without constructors |
| tests/library/Validators/NoConstructor.php | Created test validator extending Simple with no constructor to support testing |
| tests/unit/NamespacedRuleFactoryTest.php | Added test case and import to verify validators without constructors can be created via factory |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
This is really curious. How come the tests didn't pick this up? |
|
@henriquemoody Now I see the problem better. I was doing Perhaps we should throw " EDIT: It's kind of a pain to manage mandatory/optional arguments though, and we shouldn't be checking that kind of stuff during runtime (ReflectionArgument and so on are slow). |
|
@alganet I think maybe we just need better exception handling. Right now there are too many things in the same try block. We could improve that without managing arguments. An exception saying that a validator is not valid is one thing, but in this case we should say we were not able to create a validator with the given arguments. Since you are at it already, perhaps you could put the creation of the validator in another try block and handle that exception with a more descriptive message |
|
@henriquemoody makes sense, good idea! |
9d1c9ae to
b0875c6
Compare
This change introduces a less misleading exception when trying to construct an instance and failing due to mismatched arguments coming from ReflectionExceptions.
bd274a0 to
9abe993
Compare
This was affecting both v::version and v::attributes, making them impossible to use except for the concrete interface.
This change allows these validators to be built using the chain.