3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface ErrorProvider extends IteratorAggregate { public function add(string $error) : ErrorProvider; public function clear() : void; public function hasErrors() : bool; } interface ValidationSubject { public function setErrorProvider(ErrorProvider $errorProvider) : void; } class ConcreteErrorProvider implements ErrorProvider { private $errorList = []; public function add(string $errorMessage) : ErrorProvider { $this->errorList[] = $errorMessage; return $this; } public function clear() : void { $this->errorList = []; } public function hasErrors() : bool { return (boolean)$this->errorList; } public function __toString() : string { return implode("\n" , $this->errorList); } public function getIterator() : \Iterator { return new ArrayIterator($this->errorList); } } trait ValidationSubjectTrait { private $errorProvider; public function setErrorProvider(ErrorProvider $errorProvider) : void { $this->errorProvider = $errorProvider; } protected function setErrorMessage(string $errorMessage) : void { //Caso não haja um ErrorProvider, será lançada uma exception como de costume if (!$this->errorProvider instanceof ErrorProvider) { throw new \RuntimeException($errorMessage); } $this->errorProvider->add($errorMessage); } } class Person implements ValidationSubject { use ValidationSubjectTrait; /** método apenas para testes **/ public function setName($name) : void { $this->setErrorMessage("O nome não foi informado"); } /** método apenas para testes **/ public function setLastName($lastName) : void { $this->setErrorMessage("O sobrenome não foi informado"); } } try { $person = new Person(); $person->setName('Gabriel'); $person->setLastName('Heming'); } catch(Exception $exception) { echo $exception->getMessage(); } echo "\n --------------------- \n"; $errorProvider = new ConcreteErrorProvider(); $person = new Person(); $person->setErrorProvider($errorProvider); $person->setName('Gabriel'); $person->setLastName('Heming'); if ($errorProvider->hasErrors()) { echo $errorProvider; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 37
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
Found catch point at position: 13
Branch analysis from position: 13
2 jumps found. (Code = 107) Position 1 = 14, Position 2 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 37
Branch analysis from position: 36
Branch analysis from position: 37
filename:       /in/aDUGW
function name:  (null)
number of ops:  38
compiled vars:  !0 = $person, !1 = $exception, !2 = $errorProvider
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   DECLARE_CLASS                                            'errorprovider'
   16     1        DECLARE_CLASS                                            'concreteerrorprovider'
   69     2        DECLARE_CLASS                                            'person'
   88     3        NEW                                              $3      'Person'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $3
   89     6        INIT_METHOD_CALL                                         !0, 'setName'
          7        SEND_VAL_EX                                              'Gabriel'
          8        DO_FCALL                                      0          
   90     9        INIT_METHOD_CALL                                         !0, 'setLastName'
         10        SEND_VAL_EX                                              'Heming'
         11        DO_FCALL                                      0          
         12      > JMP                                                      ->17
   92    13  E > > CATCH                                       last         'Exception'
   94    14    >   INIT_METHOD_CALL                                         !1, 'getMessage'
         15        DO_FCALL                                      0  $8      
         16        ECHO                                                     $8
   97    17    >   ECHO                                                     '%0A+---------------------+%0A'
   99    18        NEW                                              $9      'ConcreteErrorProvider'
         19        DO_FCALL                                      0          
         20        ASSIGN                                                   !2, $9
  101    21        NEW                                              $12     'Person'
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !0, $12
  102    24        INIT_METHOD_CALL                                         !0, 'setErrorProvider'
         25        SEND_VAR_EX                                              !2
         26        DO_FCALL                                      0          
  103    27        INIT_METHOD_CALL                                         !0, 'setName'
         28        SEND_VAL_EX                                              'Gabriel'
         29        DO_FCALL                                      0          
  104    30        INIT_METHOD_CALL                                         !0, 'setLastName'
         31        SEND_VAL_EX                                              'Heming'
         32        DO_FCALL                                      0          
  106    33        INIT_METHOD_CALL                                         !2, 'hasErrors'
         34        DO_FCALL                                      0  $18     
         35      > JMPZ                                                     $18, ->37
  108    36    >   ECHO                                                     !2
  109    37    > > RETURN                                                   1

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

End of function add

Function clear:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  clear
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E > > RETURN                                                   null

End of function clear

Function haserrors:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  hasErrors
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   VERIFY_RETURN_TYPE                                       
          1      > RETURN                                                   null

End of function haserrors

End of class ErrorProvider.

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

End of function seterrorprovider

End of class ValidationSubject.

Class ConcreteErrorProvider:
Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  add
number of ops:  9
compiled vars:  !0 = $errorMessage
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   22     1        FETCH_OBJ_W                                      $1      'errorList'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
   24     4        FETCH_THIS                                       ~3      
          5        VERIFY_RETURN_TYPE                                       ~3
          6      > RETURN                                                   ~3
   25     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function add

Function clear:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  clear
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   ASSIGN_OBJ                                               'errorList'
          1        OP_DATA                                                  <array>
   30     2      > RETURN                                                   null

End of function clear

Function haserrors:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  hasErrors
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   FETCH_OBJ_R                                      ~0      'errorList'
          1        BOOL                                             ~1      ~0
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
   35     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function haserrors

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  __toString
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   INIT_FCALL                                               'implode'
          1        SEND_VAL                                                 '%0A'
          2        FETCH_OBJ_R                                      ~0      'errorList'
          3        SEND_VAL                                                 ~0
          4        DO_ICALL                                         $1      
          5        VERIFY_RETURN_TYPE                                       $1
          6      > RETURN                                                   $1
   40     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function __tostring

Function getiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  getIterator
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   NEW                                              $0      'ArrayIterator'
          1        CHECK_FUNC_ARG                                           
          2        FETCH_OBJ_FUNC_ARG                               $1      'errorList'
          3        SEND_FUNC_ARG                                            $1
          4        DO_FCALL                                      0          
          5        VERIFY_RETURN_TYPE                                       $0
          6      > RETURN                                                   $0
   45     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function getiterator

End of class ConcreteErrorProvider.

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

End of function seterrorprovider

Function seterrormessage:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  setErrorMessage
number of ops:  14
compiled vars:  !0 = $errorMessage
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
   60     1        FETCH_OBJ_R                                      ~1      'errorProvider'
          2        INSTANCEOF                                       ~2      ~1, 'ErrorProvider'
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->9
   62     5    >   NEW                                              $4      'RuntimeException'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8      > THROW                                         0          $4
   65     9    >   FETCH_OBJ_R                                      ~6      'errorProvider'
         10        INIT_METHOD_CALL                                         ~6, 'add'
         11        SEND_VAR_EX                                              !0
         12        DO_FCALL                                      0          
   66    13      > RETURN                                                   null

End of function seterrormessage

End of class ValidationSubjectTrait.

Class Person:
Function setname:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  setName
number of ops:  5
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
   76     1        INIT_METHOD_CALL                                         'setErrorMessage'
          2        SEND_VAL_EX                                              'O+nome+n%C3%A3o+foi+informado'
          3        DO_FCALL                                      0          
   77     4      > RETURN                                                   null

End of function setname

Function setlastname:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aDUGW
function name:  setLastName
number of ops:  5
compiled vars:  !0 = $lastName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
   82     1        INIT_METHOD_CALL                                         'setErrorMessage'
          2        SEND_VAL_EX                                              'O+sobrenome+n%C3%A3o+foi+informado'
          3        DO_FCALL                                      0          
   83     4      > RETURN                                                   null

End of function setlastname

End of class Person.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.35 ms | 1413 KiB | 15 Q