3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ArrayAnalyzer { /** * The rules to test. * * @var array */ protected $rules; public function __construct(ArrayAnalyzerRule ...$rules) { $this->rules = $rules; } /** * Analyzes an array with each of the rules, and returns the report. * * @param array $array * @return array */ public function analyze(array $array) { $report = []; foreach ($this->rules as $key => $rule) { if ($rule->fulfilledBy($array)) { $report[$key] = $rule->getValue($array); } } return $report; } } interface ArrayAnalyzerRule { /** * Checks whether the rule is fulfilled by the passed array. * * @param array $array * @return boolean */ public function fulfilledBy(array $array); /** * Gets the rule output value. * * @param array $array * @return array */ public function getValue(array $array); } class AllIntegersRule implements ArrayAnalyzerRule { /** * {@inheritDoc} */ public function fulfilledBy(array $array) { return array_reduce($array, function ($carry, $item) { return $carry && is_int($item); }, true); } /** * {@inheritDoc} */ public function getValue(array $array) { return true; } } class SameLengthRule implements ArrayAnalyzerRule { public function fulfilledBy(array $array) { return count(array_unique(array_map(function ($item) { return mb_strlen($item); }, $array))) === 1; } /** * {@inheritDoc} */ public function getValue(array $array) { return mb_strlen($array[0]); } } $arrayAnalyzer = new ArrayAnalyzer([ 'isInteger' => new AllIntegersRule, 'length' => new SameLengthRule, ]); var_dump($arrayAnalyzer->analyze([1, 2, 3])); var_dump($arrayAnalyzer->analyze([1, 2, 'D'])); var_dump($arrayAnalyzer->analyze([1, 2, '3D']));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YHs7h
function name:  (null)
number of ops:  31
compiled vars:  !0 = $arrayAnalyzer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   DECLARE_CLASS                                            'allintegersrule'
   77     1        DECLARE_CLASS                                            'samelengthrule'
   95     2        NEW                                              $1      'ArrayAnalyzer'
   96     3        NEW                                              $2      'AllIntegersRule'
          4        DO_FCALL                                      0          
          5        INIT_ARRAY                                       ~4      $2, 'isInteger'
   97     6        NEW                                              $5      'SameLengthRule'
          7        DO_FCALL                                      0          
          8        ADD_ARRAY_ELEMENT                                ~4      $5, 'length'
          9        SEND_VAL_EX                                              ~4
         10        DO_FCALL                                      0          
   95    11        ASSIGN                                                   !0, $1
  100    12        INIT_FCALL                                               'var_dump'
         13        INIT_METHOD_CALL                                         !0, 'analyze'
         14        SEND_VAL_EX                                              <array>
         15        DO_FCALL                                      0  $9      
         16        SEND_VAR                                                 $9
         17        DO_ICALL                                                 
  101    18        INIT_FCALL                                               'var_dump'
         19        INIT_METHOD_CALL                                         !0, 'analyze'
         20        SEND_VAL_EX                                              <array>
         21        DO_FCALL                                      0  $11     
         22        SEND_VAR                                                 $11
         23        DO_ICALL                                                 
  102    24        INIT_FCALL                                               'var_dump'
         25        INIT_METHOD_CALL                                         !0, 'analyze'
         26        SEND_VAL_EX                                              <array>
         27        DO_FCALL                                      0  $13     
         28        SEND_VAR                                                 $13
         29        DO_ICALL                                                 
         30      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FYHs7h%3A63%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/YHs7h
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $carry, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   64     2      > JMPZ_EX                                          ~2      !0, ->5
          3    >   TYPE_CHECK                                   16  ~3      !1
          4        BOOL                                             ~2      ~3
          5    > > RETURN                                                   ~2
   65     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYHs7h%3A63%240

Function %00%7Bclosure%7D%2Fin%2FYHs7h%3A81%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YHs7h
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
   82     1        INIT_FCALL                                               'mb_strlen'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
   83     5*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYHs7h%3A81%242

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

End of function __construct

Function analyze:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 16
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 15
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/YHs7h
function name:  analyze
number of ops:  19
compiled vars:  !0 = $array, !1 = $report, !2 = $rule, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   25     1        ASSIGN                                                   !1, <array>
   27     2        FETCH_OBJ_R                                      ~5      'rules'
          3      > FE_RESET_R                                       $6      ~5, ->16
          4    > > FE_FETCH_R                                       ~7      $6, !2, ->16
          5    >   ASSIGN                                                   !3, ~7
   28     6        INIT_METHOD_CALL                                         !2, 'fulfilledBy'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $9      
          9      > JMPZ                                                     $9, ->15
   29    10    >   INIT_METHOD_CALL                                         !2, 'getValue'
         11        SEND_VAR_EX                                              !0
         12        DO_FCALL                                      0  $11     
         13        ASSIGN_DIM                                               !1, !3
         14        OP_DATA                                                  $11
   27    15    > > JMP                                                      ->4
         16    >   FE_FREE                                                  $6
   33    17      > RETURN                                                   !1
   34    18*     > RETURN                                                   null

End of function analyze

End of class ArrayAnalyzer.

Class ArrayAnalyzerRule:
Function fulfilledby:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YHs7h
function name:  fulfilledBy
number of ops:  2
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function fulfilledby

Function getvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YHs7h
function name:  getValue
number of ops:  2
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function getvalue

End of class ArrayAnalyzerRule.

Class AllIntegersRule:
Function fulfilledby:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YHs7h
function name:  fulfilledBy
number of ops:  9
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   63     1        INIT_FCALL                                               'array_reduce'
          2        SEND_VAR                                                 !0
          3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYHs7h%3A63%240'
   65     4        SEND_VAL                                                 ~1
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $2      
          7      > RETURN                                                   $2
   66     8*     > RETURN                                                   null

End of function fulfilledby

Function getvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YHs7h
function name:  getValue
number of ops:  3
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
   73     1      > RETURN                                                   <true>
   74     2*     > RETURN                                                   null

End of function getvalue

End of class AllIntegersRule.

Class SameLengthRule:
Function fulfilledby:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YHs7h
function name:  fulfilledBy
number of ops:  13
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   RECV                                             !0      
   81     1        INIT_FCALL                                               'array_unique'
          2        INIT_FCALL                                               'array_map'
          3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYHs7h%3A81%242'
   83     4        SEND_VAL                                                 ~1
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                         $3      
          9        COUNT                                            ~4      $3
         10        IS_IDENTICAL                                     ~5      ~4, 1
         11      > RETURN                                                   ~5
   84    12*     > RETURN                                                   null

End of function fulfilledby

Function getvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YHs7h
function name:  getValue
number of ops:  7
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   RECV                                             !0      
   91     1        INIT_FCALL                                               'mb_strlen'
          2        FETCH_DIM_R                                      ~1      !0, 0
          3        SEND_VAL                                                 ~1
          4        DO_ICALL                                         $2      
          5      > RETURN                                                   $2
   92     6*     > RETURN                                                   null

End of function getvalue

End of class SameLengthRule.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.7 ms | 1408 KiB | 23 Q