magento/magento2#40302: TypeError in Creditmemo\Validation\QuantityVa…#40429
magento/magento2#40302: TypeError in Creditmemo\Validation\QuantityVa…#40429shmVan wants to merge 3 commits intomagento:2.4-developfrom
Conversation
…aused by min() with string quantity. Fix PHP 8.4 TypeError when credit memo quantity passed as string - Cast quantity to float before passing to isValidDecimalRefundQty() - Add test cases for string quantity values from API - Ensure PHP 8.4 compatibility with strict types
|
Hi @shmVan. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
|
@magento run all tests |
|
Note this related PR: |
|
@magento run all tests |
|
@magento run all tests |
|
Hi @shmVan, Thank you for this contribution and for the clear description and test. Your change correctly addresses the credit memo quantity issue and is very much appreciated. After review, we have decided to close this pull request in favour of #40303. That PR takes a slightly broader approach (including the same validation fix plus related handling when the credit memo is built from an invoice). This is not a reflection on the quality of your work—both directions fix the reported problem; we are standardising on one implementation to avoid duplicate merges. We are grateful for the time you put into this and for helping highlight the issue. Please do keep contributing; fixes like yours make a real difference to the project. Thanks again. |
Fix PHP 8.4 TypeError when credit memo quantity passed as string
Description (*)
This PR fixes a PHP 8.4 compatibility issue in credit memo quantity validation that causes a TypeError when quantity values are passed as strings from API/JSON requests.
Problem:
When creating a credit memo via API where the quantity is provided as a string (e.g.,
"1.0"or"2.5"), PHP 8.4's strict type enforcement throws a TypeError inQuantityValidator::isValidDecimalRefundQty()because themethod signature requires a
floatparameter but receives astring.Root Cause:
isValidDecimalRefundQty()has strict type declaration:float $itemQty$item->getQty()can return string values depending on the data sourceSolution:
Added explicit
(float)type cast when passing$item->getQty()toisValidDecimalRefundQty()method on line 136. This approach:InvoiceService::setInvoiceItemQuantity())declare(strict_types=1)Testing:
Added two new test cases in
QuantityValidatorTestto verify:"1.0"for non-decimal products (integer quantities)"2.5"for decimal-allowed products (float quantities)All existing tests continue to pass.
Error Fixed:
TypeError: Magento\Sales\Model\Order\Creditmemo\Validation\QuantityValidator::isValidDecimalRefundQty():
Argument #2 ($itemQty) must be of type float, string given
Related Pull Requests
Fixed Issues (if relevant)
Manual testing scenarios (*)
/rest/V1/creditmemowith the following payload: ```json{
"entity": {
"order_id": <order_id>,
"items": [
{
"order_item_id": <item_id>,
"qty": "1.0"
}
]
}
}
Questions or comments
Contribution checklist (*)