3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ValidationError { private array $errors = []; public function addError(string $fieldName, string $error) { $this->errors[$fieldName][] = $error; } public function haveErrors() : bool { return 0 !== count($this->errors); } public function printErrors() : void { print_r($this->errors); } } class UserForm { public string $name; public int $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } } class User { private string $name; private int $age; private function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } public static function createFromForm(UserForm $form) : User|ValidationError { $result = static::validate([ 'name' => $form->name, 'age' => $form->age, ]); if ($result) { return $result; } return new User($form->name, $form->age); } private static function validate(array $data) : ?ValidationError { $validationError = new ValidationError; if (20 < mb_strlen($data['name'])) { $validationError->addError('name', 'Too long. 20 chars only.'); } if (false !== mb_strpos($data['name'], 'хуй')) { $validationError->addError('name', 'Sam ti hui.'); } if (0 === $data['age']) { $validationError->addError('age', 'Too young.'); } if ($validationError->haveErrors()) { return $validationError; } return null; } } $correctForm = new UserForm('Test', 11); $result = User::createFromForm($correctForm); if ($result instanceof ValidationError) { $result->printErrors(); } $incorrectForm = new UserForm('Ты хуй', 0); $result = User::createFromForm($incorrectForm); if ($result instanceof ValidationError) { $result->printErrors(); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
Branch analysis from position: 13
filename:       /in/s9Eb9
function name:  (null)
number of ops:  27
compiled vars:  !0 = $correctForm, !1 = $result, !2 = $incorrectForm
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   NEW                                              $3      'UserForm'
          1        SEND_VAL_EX                                              'Test'
          2        SEND_VAL_EX                                              11
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $3
   85     5        INIT_STATIC_METHOD_CALL                                  'User', 'createFromForm'
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !1, $6
   86     9        INSTANCEOF                                               !1, 'ValidationError'
         10      > JMPZ                                                     ~8, ->13
   87    11    >   INIT_METHOD_CALL                                         !1, 'printErrors'
         12        DO_FCALL                                      0          
   90    13    >   NEW                                              $10     'UserForm'
         14        SEND_VAL_EX                                              '%D0%A2%D1%8B+%D1%85%D1%83%D0%B9'
         15        SEND_VAL_EX                                              0
         16        DO_FCALL                                      0          
         17        ASSIGN                                                   !2, $10
   91    18        INIT_STATIC_METHOD_CALL                                  'User', 'createFromForm'
         19        SEND_VAR                                                 !2
         20        DO_FCALL                                      0  $13     
         21        ASSIGN                                                   !1, $13
   92    22        INSTANCEOF                                               !1, 'ValidationError'
         23      > JMPZ                                                     ~15, ->26
   93    24    >   INIT_METHOD_CALL                                         !1, 'printErrors'
         25        DO_FCALL                                      0          
   94    26    > > RETURN                                                   1

Class ValidationError:
Function adderror:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s9Eb9
function name:  addError
number of ops:  7
compiled vars:  !0 = $fieldName, !1 = $error
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2        FETCH_OBJ_W                                      $2      'errors'
          3        FETCH_DIM_W                                      $3      $2, !0
          4        ASSIGN_DIM                                               $3
          5        OP_DATA                                                  !1
   10     6      > RETURN                                                   null

End of function adderror

Function haveerrors:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s9Eb9
function name:  haveErrors
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   FETCH_OBJ_R                                      ~0      'errors'
          1        COUNT                                            ~1      ~0
          2        IS_NOT_IDENTICAL                                 ~2      ~1, 0
          3        VERIFY_RETURN_TYPE                                       ~2
          4      > RETURN                                                   ~2
   15     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function haveerrors

Function printerrors:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s9Eb9
function name:  printErrors
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   INIT_FCALL                                               'print_r'
          1        FETCH_OBJ_R                                      ~0      'errors'
          2        SEND_VAL                                                 ~0
          3        DO_ICALL                                                 
   20     4      > RETURN                                                   null

End of function printerrors

End of class ValidationError.

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

End of function __construct

End of class UserForm.

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

End of function __construct

Function createfromform:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s9Eb9
function name:  createFromForm
number of ops:  24
compiled vars:  !0 = $form, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   48     1        INIT_STATIC_METHOD_CALL                                  'validate'
   49     2        FETCH_OBJ_R                                      ~2      !0, 'name'
          3        INIT_ARRAY                                       ~3      ~2, 'name'
   50     4        FETCH_OBJ_R                                      ~4      !0, 'age'
          5        ADD_ARRAY_ELEMENT                                ~3      ~4, 'age'
          6        SEND_VAL_EX                                              ~3
          7        DO_FCALL                                      0  $5      
   48     8        ASSIGN                                                   !1, $5
   53     9      > JMPZ                                                     !1, ->12
   54    10    >   VERIFY_RETURN_TYPE                                       !1
         11      > RETURN                                                   !1
   57    12    >   NEW                                              $7      'User'
         13        CHECK_FUNC_ARG                                           
         14        FETCH_OBJ_FUNC_ARG                               $8      !0, 'name'
         15        SEND_FUNC_ARG                                            $8
         16        CHECK_FUNC_ARG                                           
         17        FETCH_OBJ_FUNC_ARG                               $9      !0, 'age'
         18        SEND_FUNC_ARG                                            $9
         19        DO_FCALL                                      0          
         20        VERIFY_RETURN_TYPE                                       $7
         21      > RETURN                                                   $7
   58    22*       VERIFY_RETURN_TYPE                                       
         23*     > RETURN                                                   null

End of function createfromform

Function validate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 25
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 32
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
Branch analysis from position: 25
Branch analysis from position: 14
filename:       /in/s9Eb9
function name:  validate
number of ops:  40
compiled vars:  !0 = $data, !1 = $validationError
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   62     1        NEW                                              $2      'ValidationError'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $2
   64     4        INIT_FCALL                                               'mb_strlen'
          5        FETCH_DIM_R                                      ~5      !0, 'name'
          6        SEND_VAL                                                 ~5
          7        DO_ICALL                                         $6      
          8        IS_SMALLER                                               20, $6
          9      > JMPZ                                                     ~7, ->14
   65    10    >   INIT_METHOD_CALL                                         !1, 'addError'
         11        SEND_VAL_EX                                              'name'
         12        SEND_VAL_EX                                              'Too+long.+20+chars+only.'
         13        DO_FCALL                                      0          
   68    14    >   INIT_FCALL                                               'mb_strpos'
         15        FETCH_DIM_R                                      ~9      !0, 'name'
         16        SEND_VAL                                                 ~9
         17        SEND_VAL                                                 '%D1%85%D1%83%D0%B9'
         18        DO_ICALL                                         $10     
         19        TYPE_CHECK                                  1018          $10
         20      > JMPZ                                                     ~11, ->25
   69    21    >   INIT_METHOD_CALL                                         !1, 'addError'
         22        SEND_VAL_EX                                              'name'
         23        SEND_VAL_EX                                              'Sam+ti+hui.'
         24        DO_FCALL                                      0          
   72    25    >   FETCH_DIM_R                                      ~13     !0, 'age'
         26        IS_IDENTICAL                                             ~13, 0
         27      > JMPZ                                                     ~14, ->32
   73    28    >   INIT_METHOD_CALL                                         !1, 'addError'
         29        SEND_VAL_EX                                              'age'
         30        SEND_VAL_EX                                              'Too+young.'
         31        DO_FCALL                                      0          
   76    32    >   INIT_METHOD_CALL                                         !1, 'haveErrors'
         33        DO_FCALL                                      0  $16     
         34      > JMPZ                                                     $16, ->37
   77    35    >   VERIFY_RETURN_TYPE                                       !1
         36      > RETURN                                                   !1
   80    37    > > RETURN                                                   null
   81    38*       VERIFY_RETURN_TYPE                                       
         39*     > RETURN                                                   null

End of function validate

End of class User.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.21 ms | 1413 KiB | 19 Q