Skip to content

Commit c6d22b4

Browse files
layoutdclaude
andcommitted
Fix code quality issues in refund calculations
- Add division-by-zero guards before all $original_qty divisions - Change parameter checks from !empty() to isset() to support explicit 0 values - Remove unused variable $removed_item in refund amount calculation These changes improve robustness and prevent potential PHP warnings. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9894e7e commit c6d22b4

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

includes/Generator/Order.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static function generate( $save = true, $assoc_args = array() ) {
9898
$include_coupon = ! empty( $assoc_args['coupons'] );
9999

100100
// Handle --coupon-ratio parameter
101-
if ( ! empty( $assoc_args['coupon-ratio'] ) ) {
101+
if ( isset( $assoc_args['coupon-ratio'] ) ) {
102102
$coupon_ratio = floatval( $assoc_args['coupon-ratio'] );
103103

104104
// Validate ratio is between 0.0 and 1.0
@@ -146,7 +146,7 @@ public static function generate( $save = true, $assoc_args = array() ) {
146146
$order->save();
147147

148148
// Handle --refund-ratio parameter for completed orders
149-
if ( ! empty( $assoc_args['refund-ratio'] ) && 'completed' === $status ) {
149+
if ( isset( $assoc_args['refund-ratio'] ) && 'completed' === $status ) {
150150
$refund_ratio = floatval( $assoc_args['refund-ratio'] );
151151

152152
// Validate ratio is between 0.0 and 1.0
@@ -412,8 +412,8 @@ protected static function create_refund( $order, $force_partial = false ) {
412412
$refunded_qty = isset( $refunded_qty_by_item[ $item_id ] ) ? $refunded_qty_by_item[ $item_id ] : 0;
413413
$remaining_qty = $original_qty - $refunded_qty;
414414

415-
// Skip if nothing left to refund
416-
if ( $remaining_qty <= 0 ) {
415+
// Skip if nothing left to refund or invalid quantity
416+
if ( $remaining_qty <= 0 || $original_qty <= 0 ) {
417417
continue;
418418
}
419419

@@ -465,8 +465,8 @@ protected static function create_refund( $order, $force_partial = false ) {
465465
$refunded_qty = isset( $refunded_qty_by_item[ $item_id ] ) ? $refunded_qty_by_item[ $item_id ] : 0;
466466
$remaining_qty = $original_qty - $refunded_qty;
467467

468-
// Skip if nothing left to refund
469-
if ( $remaining_qty <= 0 ) {
468+
// Skip if nothing left to refund or invalid quantity
469+
if ( $remaining_qty <= 0 || $original_qty <= 0 ) {
470470
continue;
471471
}
472472

@@ -499,8 +499,8 @@ protected static function create_refund( $order, $force_partial = false ) {
499499
$refunded_qty = isset( $refunded_qty_by_item[ $item_id ] ) ? $refunded_qty_by_item[ $item_id ] : 0;
500500
$remaining_qty = $original_qty - $refunded_qty;
501501

502-
// Skip if nothing left to refund or if only 1 remaining
503-
if ( $remaining_qty <= 1 ) {
502+
// Skip if nothing left to refund, if only 1 remaining, or invalid quantity
503+
if ( $remaining_qty <= 1 || $original_qty <= 0 ) {
504504
continue;
505505
}
506506

@@ -541,8 +541,8 @@ protected static function create_refund( $order, $force_partial = false ) {
541541
$refunded_qty = isset( $refunded_qty_by_item[ $item_id ] ) ? $refunded_qty_by_item[ $item_id ] : 0;
542542
$remaining_qty = $original_qty - $refunded_qty;
543543

544-
// Skip if nothing left to refund
545-
if ( $remaining_qty <= 0 ) {
544+
// Skip if nothing left to refund or invalid quantity
545+
if ( $remaining_qty <= 0 || $original_qty <= 0 ) {
546546
continue;
547547
}
548548

@@ -615,7 +615,6 @@ protected static function create_refund( $order, $force_partial = false ) {
615615
while ( $refund_amount >= $max_partial_refund && count( $line_items ) > 1 ) {
616616
// Remove a random item from the refund
617617
$item_id_to_remove = array_rand( $line_items );
618-
$removed_item = $line_items[ $item_id_to_remove ];
619618
unset( $line_items[ $item_id_to_remove ] );
620619

621620
// Recalculate refund amount and counts

0 commit comments

Comments
 (0)