-
Notifications
You must be signed in to change notification settings - Fork 72
refactor: payment method definition capability duplication #11148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: payment method definition capability duplication #11148
Conversation
Test the buildOption 1. Jetpack Beta
Option 2. Jurassic Ninja - available for logged-in A12s🚀 Launch a JN site with this branch 🚀 ℹ️ Install this Tampermonkey script to get more options. Build info:
Note: the build is updated when a new commit is pushed to this PR. |
|
Size Change: 0 B Total Size: 876 kB ℹ️ View Unchanged
|
| */ | ||
| public static function is_bnpl( array $capabilities ): bool { | ||
| return in_array( PaymentMethodCapability::BUY_NOW_PAY_LATER, $capabilities, true ); | ||
| public static function is_bnpl( string $payment_method_definition ): bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of passing the capabilities array, I decided to pass a PaymentMethodDefinitionInterface interface instead - I felt like it was more explicit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors the payment method definition system to eliminate duplicate boilerplate code by removing capability-derived methods from the PaymentMethodDefinitionInterface. Instead of requiring each payment method definition to implement four identical wrapper methods (is_bnpl(), is_reusable(), accepts_only_domestic_payments(), allows_manual_capture()), the code now uses PaymentMethodUtils helper methods that accept the definition class name and call get_capabilities() directly.
- Removed 5 methods from
PaymentMethodDefinitionInterface(4 capability-derived + 1 unusedis_enabled_by_default()) - Updated
PaymentMethodUtilsmethods to accept definition class names instead of capability arrays - Removed ~40 lines of boilerplate from each of 9 payment method definitions (~360 lines total)
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
includes/payment-methods/Configs/Interfaces/PaymentMethodDefinitionInterface.php |
Removed capability-derived methods and unused is_enabled_by_default() from interface |
includes/payment-methods/Configs/Utils/PaymentMethodUtils.php |
Updated method signatures to accept class names and call get_capabilities() internally |
includes/payment-methods/class-upe-payment-method.php |
Updated to call PaymentMethodUtils static methods instead of definition methods |
includes/payment-methods/Configs/Definitions/*Definition.php (9 files) |
Removed duplicate capability-checking and is_enabled_by_default() method implementations |
tests/unit/payment-methods/Configs/test-class-payment-method-utils.php |
Updated tests to use anonymous classes with new method signatures |
tests/unit/payment-methods/Configs/mocks/class-mock-payment-method-definition*.php (2 files) |
Removed deleted methods from mock implementations |
changelog/refactor-remove-payment-method-definition-duplicate-methods |
Added changelog entry for this refactoring |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hey @dmvrtx 👋 I added you directly as a reviewed as a follow-up from the earlier conversation around the |
dmvrtx
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach, and I like what you did with tests too. Although, I think there is a chance to use a data provider to simplify it further. Pre-approving, as this is a subjective suggestion.
| /** | ||
| * Test that is_bnpl() correctly identifies BNPL methods. | ||
| */ | ||
| public function test_is_bnpl() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like a practical candidate to use a data provider instead of multiple cases within one test method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to prefer more explicit tests, even tho they could be repetitive, because when something breaks they're often easier to iterate over and reason about, IMO :) however, this scenario doesn't seem too bad - if something is broken, it'll probably be for a little mistake, since the test scope isn't too big.
I refactored it in 2088c45 . I needed to reset the registry after each test, tho. There was some test pollution.
Fixes WOOPMNT-5515
Fixes #
Changes proposed in this Pull Request
Discussed here: #11138 (comment)
Refactoring the
PaymentMethodDefinitionInterface(and related code) to reduce boilerplate across payment method definitions. In this PR, I:PaymentMethodUtilshelpers, insteadis_enabled_by_default()methodThe
PaymentMethodDefinitionInterfacerequired four methods that were all derived fromget_capabilities():is_bnpl()is_reusable()accepts_only_domestic_payments()allows_manual_capture()Every payment method definition (9 definitions) had identical boilerplate implementations:
To maintain the explicitness, I kept the
get_capabilities()method in the interface. I just removed the derived convenience methods.The original design requirements wanted to avoid composition or traits - I kept these requirements.
I considered to also remove the
is_bnpl()/is_reusable()/etc. checks from thePaymentMethodUtils.However, I felt that it was becoming too verbose and didn't offer good autocomplete suggestions.
Testing instructions
is_bnpl()categorizes the payment methods correctly)npm run changelogto add a changelog file, choosepatchto leave it empty if the change is not significant. You can add multiple changelog files in one PR by running this command a few times.Post merge