3v4l.org

run code in 300+ PHP versions simultaneously
<?php class RetryCatch { protected static $_currentRetries = 0; /** * Resets the retries counter * @return void */ public static function resetRetries() { static::$_currentRetries = 0; } /** * Emulates a try-catch-retry block for each element of a provided array. * At least two callback functions must be provided. The first one is applied * to each element of the array. If it throws an Exception, the second callback * is invoked and it tries to "fix" the error. This handler is called a maximum * of $maxRetries times, before throwing another Exception itself. * That's why the handleException callback must call resetRetries() in order * to reset the counter back to 0 retries. * * Obligations of the iterateCallback: * - It must accept the current element as a first argument * * Obligations of the handleExceptionCallback: * - It must accept the Exception object as a first argument * - It must accept the remaining array of objects as a second argument * - It must call RetryCatch::resetRetries() upon successful error handling * * EXAMPLE USAGE: * $arr = array( * 1, false, 2, 3 * ); * RetryCatch::retry($arr, 5, 'SomeClass::iterateMethod', 'SomeClass::handleException'); * * OR with the optional parameters, allowing the use of objects and/or * arbitrary callback parameterrs: * $foo = new Foo(); * RetryCatch::retry($arr, 5, 'bar', 'handle', array(1, 2, 3), array(1, 2, 3), $foo, $foo); * * @internal call resetRetries() if the handleCallback runs successfully * @param array $array * @param int $maxRetries * @param string $iterateCallback * @param string $handleCallback * @param array $iterateParams * @param array $handleParams * @param object $iterateCallbackObject * @param object $handleCallbackObject * @throws RetryCatchException */ public static function retry($array, $maxRetries, $iterateCallback, $handleCallback, $iterateParams = null, $handleParams = null, $iterateCallbackObject = null, $handleCallbackObject = null) { while (!empty($array)) { reset($array); $key = key($array); try { if (!is_object($iterateCallbackObject)) { if (is_array($iterateParams) && !empty($iterateParams)) { $useIterateParams = $iterateParams; array_unshift($useIterateParams, $array[$key]); call_user_func_array($iterateCallback, $useIterateParams); } else { call_user_func($iterateCallback, ($array[$key])); } } else { if (is_array($iterateParams) && !empty($iterateParams)) { $useIterateParams = $iterateParams; array_unshift($useIterateParams, $array[$key]); call_user_func_array(array($iterateCallbackObject, $iterateCallback), $useIterateParams); } else { $iterateCallbackObject->$iterateCallback($array[$key]); } } unset($array[$key]); } catch (Exception $e) { static::$_currentRetries++; if (static::$_currentRetries <= $maxRetries) { if (!is_object($handleCallbackObject)) { if (is_array($handleParams) && !empty($handleParams)) { $useHandleParams = $handleParams; array_unshift($useHandleParams, $e, $array); call_user_func_array($handleCallback, $useHandleParams); } else { call_user_func($handleCallback, $e, $array); } } else { if (is_array($handleParams) && !empty($handleParams)) { $useHandleParams = $handleParams; array_unshift($useHandleParams, $e, $array); call_user_func_array(array($handleCallbackObject, $handleCallback), $useHandleParams); } else { $handleCallbackObject->$handleCallback($e, $array); } } } else { throw new RetryCatchException('RetryCatch::maxRetries reached'); } } } } } class RetryCatchException extends Exception {}
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IBC2g
function name:  (null)
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  103     0  E > > RETURN                                                   1

Class RetryCatch:
Function resetretries:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IBC2g
function name:  resetRetries
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   ASSIGN_STATIC_PROP                                       '_currentRetries'
          1        OP_DATA                                                  0
   11     2      > RETURN                                                   null

End of function resetretries

Function retry:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 125
Branch analysis from position: 125
2 jumps found. (Code = 44) Position 1 = 128, Position 2 = 9
Branch analysis from position: 128
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 41
Branch analysis from position: 19
2 jumps found. (Code = 46) Position 1 = 21, Position 2 = 24
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 36
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 125
Branch analysis from position: 125
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
Branch analysis from position: 24
Branch analysis from position: 41
2 jumps found. (Code = 46) Position 1 = 43, Position 2 = 46
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 60
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 125
Branch analysis from position: 125
Branch analysis from position: 46
Found catch point at position: 67
Branch analysis from position: 67
2 jumps found. (Code = 107) Position 1 = 68, Position 2 = -2
Branch analysis from position: 68
2 jumps found. (Code = 43) Position 1 = 72, Position 2 = 121
Branch analysis from position: 72
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 97
Branch analysis from position: 75
2 jumps found. (Code = 46) Position 1 = 77, Position 2 = 80
Branch analysis from position: 77
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 92
Branch analysis from position: 81
1 jumps found. (Code = 42) Position 1 = 96
Branch analysis from position: 96
1 jumps found. (Code = 42) Position 1 = 120
Branch analysis from position: 120
1 jumps found. (Code = 42) Position 1 = 125
Branch analysis from position: 125
Branch analysis from position: 92
1 jumps found. (Code = 42) Position 1 = 120
Branch analysis from position: 120
Branch analysis from position: 80
Branch analysis from position: 97
2 jumps found. (Code = 46) Position 1 = 99, Position 2 = 102
Branch analysis from position: 99
2 jumps found. (Code = 43) Position 1 = 103, Position 2 = 116
Branch analysis from position: 103
1 jumps found. (Code = 42) Position 1 = 120
Branch analysis from position: 120
Branch analysis from position: 116
1 jumps found. (Code = 42) Position 1 = 125
Branch analysis from position: 125
Branch analysis from position: 102
Branch analysis from position: 121
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/IBC2g
function name:  retry
number of ops:  129
compiled vars:  !0 = $array, !1 = $maxRetries, !2 = $iterateCallback, !3 = $handleCallback, !4 = $iterateParams, !5 = $handleParams, !6 = $iterateCallbackObject, !7 = $handleCallbackObject, !8 = $key, !9 = $useIterateParams, !10 = $e, !11 = $useHandleParams
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
          4        RECV_INIT                                        !4      null
          5        RECV_INIT                                        !5      null
          6        RECV_INIT                                        !6      null
          7        RECV_INIT                                        !7      null
   53     8      > JMP                                                      ->125
   54     9    >   INIT_FCALL                                               'reset'
         10        SEND_REF                                                 !0
         11        DO_ICALL                                                 
   55    12        INIT_FCALL                                               'key'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $13     
         15        ASSIGN                                                   !8, $13
   57    16        TYPE_CHECK                                  256  ~15     !6
         17        BOOL_NOT                                         ~16     ~15
         18      > JMPZ                                                     ~16, ->41
   58    19    >   TYPE_CHECK                                  128  ~17     !4
         20      > JMPZ_EX                                          ~17     ~17, ->24
         21    >   ISSET_ISEMPTY_CV                                 ~18     !4
         22        BOOL_NOT                                         ~19     ~18
         23        BOOL                                             ~17     ~19
         24    > > JMPZ                                                     ~17, ->36
   59    25    >   ASSIGN                                                   !9, !4
   60    26        INIT_FCALL                                               'array_unshift'
         27        SEND_REF                                                 !9
         28        FETCH_DIM_R                                      ~21     !0, !8
         29        SEND_VAL                                                 ~21
         30        DO_ICALL                                                 
   61    31        INIT_USER_CALL                                0          'call_user_func_array', !2
         32        SEND_ARRAY                                               !9
         33        CHECK_UNDEF_ARGS                                         
         34        DO_FCALL                                      0          
         35      > JMP                                                      ->40
   63    36    >   INIT_USER_CALL                                1          'call_user_func', !2
         37        FETCH_DIM_R                                      ~24     !0, !8
         38        SEND_USER                                                ~24
         39        DO_FCALL                                      0          
         40    > > JMP                                                      ->65
   66    41    >   TYPE_CHECK                                  128  ~26     !4
         42      > JMPZ_EX                                          ~26     ~26, ->46
         43    >   ISSET_ISEMPTY_CV                                 ~27     !4
         44        BOOL_NOT                                         ~28     ~27
         45        BOOL                                             ~26     ~28
         46    > > JMPZ                                                     ~26, ->60
   67    47    >   ASSIGN                                                   !9, !4
   68    48        INIT_FCALL                                               'array_unshift'
         49        SEND_REF                                                 !9
         50        FETCH_DIM_R                                      ~30     !0, !8
         51        SEND_VAL                                                 ~30
         52        DO_ICALL                                                 
   69    53        INIT_ARRAY                                       ~32     !6
         54        ADD_ARRAY_ELEMENT                                ~32     !2
         55        INIT_USER_CALL                                0          'call_user_func_array', ~32
         56        SEND_ARRAY                                               !9
         57        CHECK_UNDEF_ARGS                                         
         58        DO_FCALL                                      0          
         59      > JMP                                                      ->65
   71    60    >   INIT_METHOD_CALL                                         !6, !2
         61        CHECK_FUNC_ARG                                           
         62        FETCH_DIM_FUNC_ARG                               $34     !0, !8
         63        SEND_FUNC_ARG                                            $34
         64        DO_FCALL                                      0          
   74    65    >   UNSET_DIM                                                !0, !8
         66      > JMP                                                      ->125
   75    67  E > > CATCH                                       last         'Exception'
   76    68    >   PRE_INC_STATIC_PROP                                      '_currentRetries'
   77    69        FETCH_STATIC_PROP_R          unknown             ~37     '_currentRetries'
         70        IS_SMALLER_OR_EQUAL                                      ~37, !1
         71      > JMPZ                                                     ~38, ->121
   78    72    >   TYPE_CHECK                                  256  ~39     !7
         73        BOOL_NOT                                         ~40     ~39
         74      > JMPZ                                                     ~40, ->97
   79    75    >   TYPE_CHECK                                  128  ~41     !5
         76      > JMPZ_EX                                          ~41     ~41, ->80
         77    >   ISSET_ISEMPTY_CV                                 ~42     !5
         78        BOOL_NOT                                         ~43     ~42
         79        BOOL                                             ~41     ~43
         80    > > JMPZ                                                     ~41, ->92
   80    81    >   ASSIGN                                                   !11, !5
   81    82        INIT_FCALL                                               'array_unshift'
         83        SEND_REF                                                 !11
         84        SEND_VAR                                                 !10
         85        SEND_VAR                                                 !0
         86        DO_ICALL                                                 
   82    87        INIT_USER_CALL                                0          'call_user_func_array', !3
         88        SEND_ARRAY                                               !11
         89        CHECK_UNDEF_ARGS                                         
         90        DO_FCALL                                      0          
         91      > JMP                                                      ->96
   84    92    >   INIT_USER_CALL                                2          'call_user_func', !3
         93        SEND_USER                                                !10
         94        SEND_USER                                                !0
         95        DO_FCALL                                      0          
         96    > > JMP                                                      ->120
   87    97    >   TYPE_CHECK                                  128  ~48     !5
         98      > JMPZ_EX                                          ~48     ~48, ->102
         99    >   ISSET_ISEMPTY_CV                                 ~49     !5
        100        BOOL_NOT                                         ~50     ~49
        101        BOOL                                             ~48     ~50
        102    > > JMPZ                                                     ~48, ->116
   88   103    >   ASSIGN                                                   !11, !5
   89   104        INIT_FCALL                                               'array_unshift'
        105        SEND_REF                                                 !11
        106        SEND_VAR                                                 !10
        107        SEND_VAR                                                 !0
        108        DO_ICALL                                                 
   90   109        INIT_ARRAY                                       ~53     !7
        110        ADD_ARRAY_ELEMENT                                ~53     !3
        111        INIT_USER_CALL                                0          'call_user_func_array', ~53
        112        SEND_ARRAY                                               !11
        113        CHECK_UNDEF_ARGS                                         
        114        DO_FCALL                                      0          
        115      > JMP                                                      ->120
   92   116    >   INIT_METHOD_CALL                                         !7, !3
        117        SEND_VAR_EX                                              !10
        118        SEND_VAR_EX                                              !0
        119        DO_FCALL                                      0          
        120    > > JMP                                                      ->125
   96   121    >   NEW                                              $56     'RetryCatchException'
        122        SEND_VAL_EX                                              'RetryCatch%3A%3AmaxRetries+reached'
        123        DO_FCALL                                      0          
        124      > THROW                                         0          $56
   53   125    >   ISSET_ISEMPTY_CV                                 ~58     !0
        126        BOOL_NOT                                         ~59     ~58
        127      > JMPNZ                                                    ~59, ->9
  100   128    > > RETURN                                                   null

End of function retry

End of class RetryCatch.

Class RetryCatchException: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.79 ms | 1408 KiB | 19 Q