3v4l.org

run code in 500+ PHP versions simultaneously
<?php class FakeCollection { public function __construct(private array $items) {} public function sum(string $key): float { $total = 0; foreach ($this->items as $item) { $total += $item[$key]; } return $total; } } function collect(array $items): FakeCollection { return new FakeCollection($items); } class FakePayment { public array $credits = []; public function __construct(public float $amount, public float $refunded) {} public function credits() { return new class { public function exists(): bool { return false; } }; } } function checkTotalRefundableAmount($payment, array $request_invoices, array $input): bool { // Calculate total refund amount requested $total_refund_requested = 0; if (count($request_invoices) > 0) { $total_refund_requested = collect($request_invoices)->sum('amount'); } elseif (array_key_exists('amount', $this->input)) { $total_refund_requested = $this->input['amount']; } if ($total_refund_requested <= 0) { return true; // No refund requested } // Calculate maximum refundable from cash portion $max_cash_refund = $payment->amount - $payment->refunded; // Calculate maximum refundable from credits $max_credit_refund = 0; if ($payment->credits()->exists()) { foreach ($payment->credits as $credit) { $max_credit_refund += ($credit->pivot->amount - $credit->pivot->refunded); } } $max_total_refundable = $max_cash_refund + $max_credit_refund; if ($total_refund_requested > $max_total_refundable) { return false; } return true; } $payment = new FakePayment(amount: 30.20, refunded: 0.0); $invoices = [['invoice_id' => 'INV_1', 'amount' => 11.85], ['invoice_id' => 'INV_2', 'amount' => 18.35]]; var_dump(checkTotalRefundableAmount($payment, $invoices, ['id' => 'PAYMENT_ID'])); // bool(false) — should be true
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Tl3AY
function name:  (null)
number of ops:  16
compiled vars:  !0 = $payment, !1 = $invoices
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   52     0  E >   NEW                                                  $2      'FakePayment'
          1        SEND_VAL_EX                                                  30.2, 'amount'
          2        SEND_VAL_EX                                                  0, 'refunded'
          3        CHECK_UNDEF_ARGS                                             
          4        DO_FCALL                                          1          
          5        ASSIGN                                                       !0, $2
   53     6        ASSIGN                                                       !1, <array>
   55     7        INIT_FCALL                                                   'var_dump'
          8        INIT_FCALL                                                   'checktotalrefundableamount'
          9        SEND_VAR                                                     !0
         10        SEND_VAR                                                     !1
         11        SEND_VAL                                                     <array>
         12        DO_FCALL                                          0  $6      
         13        SEND_VAR                                                     $6
         14        DO_ICALL                                                     
         15      > RETURN                                                       1

Function collect:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Tl3AY
function name:  collect
number of ops:  8
compiled vars:  !0 = $items
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   RECV                                                 !0      
          1        NEW                                                  $1      'FakeCollection'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0          
          4        VERIFY_RETURN_TYPE                                           $1
          5      > RETURN                                                       $1
          6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN                                                       null

End of function collect

Function checktotalrefundableamount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 26
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 47
Branch analysis from position: 36
2 jumps found. (Code = 77) Position 1 = 38, Position 2 = 46
Branch analysis from position: 38
2 jumps found. (Code = 78) Position 1 = 39, Position 2 = 46
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 52
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
Branch analysis from position: 47
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 23
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 26
Branch analysis from position: 25
Branch analysis from position: 26
Branch analysis from position: 23
filename:       /in/Tl3AY
function name:  checkTotalRefundableAmount
number of ops:  55
compiled vars:  !0 = $payment, !1 = $request_invoices, !2 = $input, !3 = $total_refund_requested, !4 = $max_cash_refund, !5 = $max_credit_refund, !6 = $credit, !7 = $max_total_refundable
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   18     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
   20     3        ASSIGN                                                       !3, 0
   22     4        COUNT                                                ~9      !1
          5        IS_SMALLER                                                   0, ~9
          6      > JMPZ                                                         ~10, ->15
   23     7    >   INIT_FCALL                                                   'collect'
          8        SEND_VAR                                                     !1
          9        DO_FCALL                                          0  $11     
         10        INIT_METHOD_CALL                                             $11, 'sum'
         11        SEND_VAL_EX                                                  'amount'
         12        DO_FCALL                                          0  $12     
         13        ASSIGN                                                       !3, $12
   22    14      > JMP                                                          ->23
   24    15    >   FETCH_THIS                                           $14     
         16        FETCH_OBJ_R                                          ~15     $14, 'input'
         17        ARRAY_KEY_EXISTS                                             'amount', ~15
         18      > JMPZ                                                         ~16, ->23
   25    19    >   FETCH_THIS                                           $17     
         20        FETCH_OBJ_R                                          ~18     $17, 'input'
         21        FETCH_DIM_R                                          ~19     ~18, 'amount'
         22        ASSIGN                                                       !3, ~19
   28    23    >   IS_SMALLER_OR_EQUAL                                          !3, 0
         24      > JMPZ                                                         ~21, ->26
   29    25    > > RETURN                                                       <true>
   33    26    >   FETCH_OBJ_R                                          ~22     !0, 'amount'
         27        FETCH_OBJ_R                                          ~23     !0, 'refunded'
         28        SUB                                                  ~24     ~22, ~23
         29        ASSIGN                                                       !4, ~24
   36    30        ASSIGN                                                       !5, 0
   37    31        INIT_METHOD_CALL                                             !0, 'credits'
         32        DO_FCALL                                          0  $27     
         33        INIT_METHOD_CALL                                             $27, 'exists'
         34        DO_FCALL                                          0  $28     
         35      > JMPZ                                                         $28, ->47
   38    36    >   FETCH_OBJ_R                                          ~29     !0, 'credits'
         37      > FE_RESET_R                                           $30     ~29, ->46
         38    > > FE_FETCH_R                                                   $30, !6, ->46
   39    39    >   FETCH_OBJ_R                                          ~31     !6, 'pivot'
         40        FETCH_OBJ_R                                          ~32     ~31, 'amount'
         41        FETCH_OBJ_R                                          ~33     !6, 'pivot'
         42        FETCH_OBJ_R                                          ~34     ~33, 'refunded'
         43        SUB                                                  ~35     ~32, ~34
         44        ASSIGN_OP                                         1          !5, ~35
   38    45      > JMP                                                          ->38
         46    >   FE_FREE                                                      $30
   43    47    >   ADD                                                  ~37     !4, !5
         48        ASSIGN                                                       !7, ~37
   45    49        IS_SMALLER                                                   !7, !3
         50      > JMPZ                                                         ~39, ->52
   46    51    > > RETURN                                                       <false>
   49    52    > > RETURN                                                       <true>
   50    53*       VERIFY_RETURN_TYPE                                           
         54*     > RETURN                                                       null

End of function checktotalrefundableamount

Class FakeCollection:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Tl3AY
function name:  __construct
number of ops:  4
compiled vars:  !0 = $items
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        ASSIGN_OBJ                                                   'items'
          2        OP_DATA                                                      !0
          3      > RETURN                                                       null

End of function __construct

Function sum:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/Tl3AY
function name:  sum
number of ops:  13
compiled vars:  !0 = $key, !1 = $total, !2 = $item
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   RECV                                                 !0      
    5     1        ASSIGN                                                       !1, 0
    6     2        FETCH_OBJ_R                                          ~4      'items'
          3      > FE_RESET_R                                           $5      ~4, ->8
          4    > > FE_FETCH_R                                                   $5, !2, ->8
          5    >   FETCH_DIM_R                                          ~6      !2, !0
          6        ASSIGN_OP                                         1          !1, ~6
          7      > JMP                                                          ->4
          8    >   FE_FREE                                                      $5
    7     9        VERIFY_RETURN_TYPE                                           !1
         10      > RETURN                                                       !1
    8    11*       VERIFY_RETURN_TYPE                                           
         12*     > RETURN                                                       null

End of function sum

End of class FakeCollection.

Class class@anonymous:
Function exists:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Tl3AY
function name:  exists
number of ops:  3
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   15     0  E > > RETURN                                                       <false>
          1*       VERIFY_RETURN_TYPE                                           
          2*     > RETURN                                                       null

End of function exists

End of class class@anonymous.

Class FakePayment:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Tl3AY
function name:  __construct
number of ops:  7
compiled vars:  !0 = $amount, !1 = $refunded
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        ASSIGN_OBJ                                                   'amount'
          3        OP_DATA                                                      !0
          4        ASSIGN_OBJ                                                   'refunded'
          5        OP_DATA                                                      !1
          6      > RETURN                                                       null

End of function __construct

Function credits:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Tl3AY
function name:  credits
number of ops:  5
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   15     0  E >   DECLARE_ANON_CLASS                                   <unknown> 
          1        NEW                                                  $1      $0
          2        DO_FCALL                                          0          
          3      > RETURN                                                       $1
          4*     > RETURN                                                       null

End of function credits

End of class FakePayment.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
161.8 ms | 804 KiB | 11 Q