3v4l.org

run code in 500+ PHP versions simultaneously
<?php /** * @psalm-immutable * @template-covariant TValue */ abstract class Option { /** * @template T * @param list<Option<T>> $options * @return Option<list<T>> */ public static function all(array $options): Option { return array_reduce( $options, /** * @param Option<list<T>> $carry * @param Option<T> $o * @return Option<list<T>> */ fn(Option $carry, Option $o) => $carry->flatMap( /** * @param list<T> $ts * @return Option<list<T>> */ fn(array $ts) => $o instanceof None ? new Some($ts) : $o->map( /** * @param T $t * @return list<T> */ fn($t) => array_merge($ts, [ $t ]) ) ), new Some([]), ); } /** * @template TMap * @param callable(TValue):TMap $map * @return Option<TMap> */ abstract public function map(callable $map): Option; /** * @template TMap * @param callable(TValue):Option<TMap> $map * @return Option<TMap> */ abstract public function flatMap(callable $map): Option; } /** * @psalm-immutable * @template-covariant T * @template-extends Option<T> */ final class Some extends Option { /** @var T */ private $value; /** @param T $value */ public function __construct($value) { $this->value = $value; } public function map(callable $map): Option { /** @psalm-suppress ImpureFunctionCall */ return new Some($map($this->value)); } public function flatMap(callable $map): Option { /** @psalm-suppress ImpureFunctionCall */ return $map($this->value); } } /** * @psalm-immutable * @template-extends Option<never> */ final class None extends Option { public function map(callable $map): Option { return $this; } public function flatMap(callable $map): Option { return $this; } } $opts = [new Some(1), new None(), new Some(2), new None(), new None(), new Some(3)]; var_dump(Option::all($opts));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  (null)
number of ops:  29
compiled vars:  !0 = $opts
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  101     0  E >   NEW                                                  $1      'Some'
          1        SEND_VAL_EX                                                  1
          2        DO_FCALL                                          0          
          3        INIT_ARRAY                                           ~3      $1
          4        NEW                                                  $4      'None'
          5        DO_FCALL                                          0          
          6        ADD_ARRAY_ELEMENT                                    ~3      $4
          7        NEW                                                  $6      'Some'
          8        SEND_VAL_EX                                                  2
          9        DO_FCALL                                          0          
         10        ADD_ARRAY_ELEMENT                                    ~3      $6
         11        NEW                                                  $8      'None'
         12        DO_FCALL                                          0          
         13        ADD_ARRAY_ELEMENT                                    ~3      $8
         14        NEW                                                  $10     'None'
         15        DO_FCALL                                          0          
         16        ADD_ARRAY_ELEMENT                                    ~3      $10
         17        NEW                                                  $12     'Some'
         18        SEND_VAL_EX                                                  3
         19        DO_FCALL                                          0          
         20        ADD_ARRAY_ELEMENT                                    ~3      $12
         21        ASSIGN                                                       !0, ~3
  102    22        INIT_FCALL                                                   'var_dump'
         23        INIT_STATIC_METHOD_CALL                                      'Option', 'all'
         24        SEND_VAR                                                     !0
         25        DO_FCALL                                          0  $15     
         26        SEND_VAR                                                     $15
         27        DO_ICALL                                                     
         28      > RETURN                                                       1

Class Option:
Function all:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  all
number of ops:  16
compiled vars:  !0 = $options, !1 = $ts, !2 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
   16     1        INIT_FCALL                                                   'array_reduce'
   17     2        SEND_VAR                                                     !0
   23     3        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
          4        BIND_LEXICAL                                                 ~3, !1
          5        BIND_LEXICAL                                                 ~3, !2
   35     6        SEND_VAL                                                     ~3
   36     7        NEW                                                  $4      'Some'
          8        SEND_VAL_EX                                                  <array>
          9        DO_FCALL                                          0          
         10        SEND_VAR                                                     $4
   16    11        DO_ICALL                                             $6      
   36    12        VERIFY_RETURN_TYPE                                           $6
         13      > RETURN                                                       $6
   38    14*       VERIFY_RETURN_TYPE                                           
         15*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  {closure:Option::all():23}
number of ops:  12
compiled vars:  !0 = $carry, !1 = $o, !2 = $ts, !3 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        BIND_STATIC                                                  !2
          3        BIND_STATIC                                                  !3
          4        INIT_METHOD_CALL                                             !0, 'flatMap'
   28     5        DECLARE_LAMBDA_FUNCTION                              ~4      [0]
          6        BIND_LEXICAL                                                 ~4, !1
          7        BIND_LEXICAL                                                 ~4, !3
   35     8        SEND_VAL_EX                                                  ~4
   23     9        DO_FCALL                                          0  $5      
   35    10      > RETURN                                                       $5
         11*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  {closure:{closure:Option::all():23}:28}
number of ops:  18
compiled vars:  !0 = $ts, !1 = $o, !2 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        BIND_STATIC                                                  !2
          3        INSTANCEOF                                                   !1, 'None'
          4      > JMPZ                                                         ~3, ->10
          5    >   NEW                                                  $4      'Some'
          6        SEND_VAR_EX                                                  !0
          7        DO_FCALL                                          0          
          8        QM_ASSIGN                                            ~6      $4
          9      > JMP                                                          ->16
         10    >   INIT_METHOD_CALL                                             !1, 'map'
   33    11        DECLARE_LAMBDA_FUNCTION                              ~7      [0]
         12        BIND_LEXICAL                                                 ~7, !0
   34    13        SEND_VAL_EX                                                  ~7
   28    14        DO_FCALL                                          0  $8      
   34    15        QM_ASSIGN                                            ~6      $8
         16    > > RETURN                                                       ~6
   35    17*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  {closure:{closure:{closure:Option::all():23}:28}:33}
number of ops:  9
compiled vars:  !0 = $t, !1 = $ts
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   33     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        INIT_FCALL                                                   'array_merge'
          3        SEND_VAR                                                     !1
          4        INIT_ARRAY                                           ~2      !0
          5        SEND_VAL                                                     ~2
          6        DO_ICALL                                             $3      
          7      > RETURN                                                       $3
   34     8*     > RETURN                                                       null

End of Dynamic Function 0

End of Dynamic Function 0

End of Dynamic Function 0

End of function all

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

End of function map

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

End of function flatmap

End of class Option.

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

End of function __construct

Function map:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  map
number of ops:  13
compiled vars:  !0 = $map
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   71     0  E >   RECV                                                 !0      
   74     1        NEW                                                  $1      'Some'
          2        INIT_DYNAMIC_CALL                                            !0
          3        CHECK_FUNC_ARG                                               
          4        FETCH_OBJ_FUNC_ARG                                   $2      'value'
          5        SEND_FUNC_ARG                                                $2
          6        DO_FCALL                                          0  $3      
          7        SEND_VAR_NO_REF_EX                                           $3
          8        DO_FCALL                                          0          
          9        VERIFY_RETURN_TYPE                                           $1
         10      > RETURN                                                       $1
   75    11*       VERIFY_RETURN_TYPE                                           
         12*     > RETURN                                                       null

End of function map

Function flatmap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  flatMap
number of ops:  10
compiled vars:  !0 = $map
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   77     0  E >   RECV                                                 !0      
   80     1        INIT_DYNAMIC_CALL                                            !0
          2        CHECK_FUNC_ARG                                               
          3        FETCH_OBJ_FUNC_ARG                                   $1      'value'
          4        SEND_FUNC_ARG                                                $1
          5        DO_FCALL                                          0  $2      
          6        VERIFY_RETURN_TYPE                                           $2
          7      > RETURN                                                       $2
   81     8*       VERIFY_RETURN_TYPE                                           
          9*     > RETURN                                                       null

End of function flatmap

Function all:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  all
number of ops:  16
compiled vars:  !0 = $options, !1 = $ts, !2 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
   16     1        INIT_FCALL                                                   'array_reduce'
   17     2        SEND_VAR                                                     !0
   23     3        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
          4        BIND_LEXICAL                                                 ~3, !1
          5        BIND_LEXICAL                                                 ~3, !2
   35     6        SEND_VAL                                                     ~3
   36     7        NEW                                                  $4      'Some'
          8        SEND_VAL_EX                                                  <array>
          9        DO_FCALL                                          0          
         10        SEND_VAR                                                     $4
   16    11        DO_ICALL                                             $6      
   36    12        VERIFY_RETURN_TYPE                                           $6
         13      > RETURN                                                       $6
   38    14*       VERIFY_RETURN_TYPE                                           
         15*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  {closure:Option::all():23}
number of ops:  12
compiled vars:  !0 = $carry, !1 = $o, !2 = $ts, !3 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        BIND_STATIC                                                  !2
          3        BIND_STATIC                                                  !3
          4        INIT_METHOD_CALL                                             !0, 'flatMap'
   28     5        DECLARE_LAMBDA_FUNCTION                              ~4      [0]
          6        BIND_LEXICAL                                                 ~4, !1
          7        BIND_LEXICAL                                                 ~4, !3
   35     8        SEND_VAL_EX                                                  ~4
   23     9        DO_FCALL                                          0  $5      
   35    10      > RETURN                                                       $5
         11*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  {closure:{closure:Option::all():23}:28}
number of ops:  18
compiled vars:  !0 = $ts, !1 = $o, !2 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        BIND_STATIC                                                  !2
          3        INSTANCEOF                                                   !1, 'None'
          4      > JMPZ                                                         ~3, ->10
          5    >   NEW                                                  $4      'Some'
          6        SEND_VAR_EX                                                  !0
          7        DO_FCALL                                          0          
          8        QM_ASSIGN                                            ~6      $4
          9      > JMP                                                          ->16
         10    >   INIT_METHOD_CALL                                             !1, 'map'
   33    11        DECLARE_LAMBDA_FUNCTION                              ~7      [0]
         12        BIND_LEXICAL                                                 ~7, !0
   34    13        SEND_VAL_EX                                                  ~7
   28    14        DO_FCALL                                          0  $8      
   34    15        QM_ASSIGN                                            ~6      $8
         16    > > RETURN                                                       ~6
   35    17*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  {closure:{closure:{closure:Option::all():23}:28}:33}
number of ops:  9
compiled vars:  !0 = $t, !1 = $ts
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   33     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        INIT_FCALL                                                   'array_merge'
          3        SEND_VAR                                                     !1
          4        INIT_ARRAY                                           ~2      !0
          5        SEND_VAL                                                     ~2
          6        DO_ICALL                                             $3      
          7      > RETURN                                                       $3
   34     8*     > RETURN                                                       null

End of Dynamic Function 0

End of Dynamic Function 0

End of Dynamic Function 0

End of function all

End of class Some.

Class None:
Function map:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  map
number of ops:  6
compiled vars:  !0 = $map
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   90     0  E >   RECV                                                 !0      
   92     1        FETCH_THIS                                           ~1      
          2        VERIFY_RETURN_TYPE                                           ~1
          3      > RETURN                                                       ~1
   93     4*       VERIFY_RETURN_TYPE                                           
          5*     > RETURN                                                       null

End of function map

Function flatmap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  flatMap
number of ops:  6
compiled vars:  !0 = $map
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   95     0  E >   RECV                                                 !0      
   97     1        FETCH_THIS                                           ~1      
          2        VERIFY_RETURN_TYPE                                           ~1
          3      > RETURN                                                       ~1
   98     4*       VERIFY_RETURN_TYPE                                           
          5*     > RETURN                                                       null

End of function flatmap

Function all:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  all
number of ops:  16
compiled vars:  !0 = $options, !1 = $ts, !2 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
   16     1        INIT_FCALL                                                   'array_reduce'
   17     2        SEND_VAR                                                     !0
   23     3        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
          4        BIND_LEXICAL                                                 ~3, !1
          5        BIND_LEXICAL                                                 ~3, !2
   35     6        SEND_VAL                                                     ~3
   36     7        NEW                                                  $4      'Some'
          8        SEND_VAL_EX                                                  <array>
          9        DO_FCALL                                          0          
         10        SEND_VAR                                                     $4
   16    11        DO_ICALL                                             $6      
   36    12        VERIFY_RETURN_TYPE                                           $6
         13      > RETURN                                                       $6
   38    14*       VERIFY_RETURN_TYPE                                           
         15*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  {closure:Option::all():23}
number of ops:  12
compiled vars:  !0 = $carry, !1 = $o, !2 = $ts, !3 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        BIND_STATIC                                                  !2
          3        BIND_STATIC                                                  !3
          4        INIT_METHOD_CALL                                             !0, 'flatMap'
   28     5        DECLARE_LAMBDA_FUNCTION                              ~4      [0]
          6        BIND_LEXICAL                                                 ~4, !1
          7        BIND_LEXICAL                                                 ~4, !3
   35     8        SEND_VAL_EX                                                  ~4
   23     9        DO_FCALL                                          0  $5      
   35    10      > RETURN                                                       $5
         11*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  {closure:{closure:Option::all():23}:28}
number of ops:  18
compiled vars:  !0 = $ts, !1 = $o, !2 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        BIND_STATIC                                                  !2
          3        INSTANCEOF                                                   !1, 'None'
          4      > JMPZ                                                         ~3, ->10
          5    >   NEW                                                  $4      'Some'
          6        SEND_VAR_EX                                                  !0
          7        DO_FCALL                                          0          
          8        QM_ASSIGN                                            ~6      $4
          9      > JMP                                                          ->16
         10    >   INIT_METHOD_CALL                                             !1, 'map'
   33    11        DECLARE_LAMBDA_FUNCTION                              ~7      [0]
         12        BIND_LEXICAL                                                 ~7, !0
   34    13        SEND_VAL_EX                                                  ~7
   28    14        DO_FCALL                                          0  $8      
   34    15        QM_ASSIGN                                            ~6      $8
         16    > > RETURN                                                       ~6
   35    17*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KspZP
function name:  {closure:{closure:{closure:Option::all():23}:28}:33}
number of ops:  9
compiled vars:  !0 = $t, !1 = $ts
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   33     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        INIT_FCALL                                                   'array_merge'
          3        SEND_VAR                                                     !1
          4        INIT_ARRAY                                           ~2      !0
          5        SEND_VAL                                                     ~2
          6        DO_ICALL                                             $3      
          7      > RETURN                                                       $3
   34     8*     > RETURN                                                       null

End of Dynamic Function 0

End of Dynamic Function 0

End of Dynamic Function 0

End of function all

End of class None.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
173.03 ms | 1936 KiB | 15 Q