3v4l.org

run code in 500+ PHP versions simultaneously
<?php class MyEntity { const STATE_NEW = 'new'; const STATE_IN_PROGRESS = 'in_progress'; const STATE_COMPLETE = 'complete'; const CONFIG_STATES = array( self::STATE_NEW, self::STATE_IN_PROGRESS, self::STATE_COMPLETE ); /** * @var \DateTime|null * @ORM\Column(type="datetime", nullable=true) */ private $dateStarted; /** * @var string|null * @ORM\Column(type="string", nullable=true) */ private $state = self::STATE_NEW; /** * @var string|null $state */ public function setState($state = null) { $this->validateState($state); $this->updateStateDate($state); $this->state = $state; return $this; } public function getState() { return $this->state; } public function getDateStarted() { return $this->dateStarted; } /** * used to update the started date when the state changes to in_progress * @var string $state */ protected function updateStateDate($state) { if ($this->state !== $state && $state === self::STATE_IN_PROGRESS && $this->dateStarted === null) { $this->dateStarted = new \DateTime(); } return $this; } protected function validateState($state) { if (!in_array($state, self::CONFIG_STATES, true)) { throw new \InvalidArgumentException( sprintf('Unknown state "%s". Expected one of: "%s"', $state, implode('", "', self::CONFIG_STATES) ) ); } if ($this->state === self::STATE_NEW && $state !== self::STATE_IN_PROGRESS) { throw new \InvalidArgumentException( sprintf('Invalid state "%s" specified, Expected: %s', $state, self::STATE_IN_PROGRESS ) ); } return $this; } } $entity = new \MyEntity; var_dump($entity); echo \PHP_EOL . '----' . \PHP_EOL; echo 'set State as in progress and set the initial date'. \PHP_EOL; $entity->setState(MyEntity::STATE_IN_PROGRESS); var_dump($entity); echo \PHP_EOL . '----' . \PHP_EOL; echo 'set State as complete and leave date unchanged'. \PHP_EOL; $entity->setState(MyEntity::STATE_COMPLETE); var_dump($entity); echo \PHP_EOL . '----' . \PHP_EOL; echo 'change the state back to in progress and leave the date unchanged'. \PHP_EOL; $entity->setState(MyEntity::STATE_IN_PROGRESS); var_dump($entity); echo \PHP_EOL . '----' . \PHP_EOL; echo 'use an invalid state string'. \PHP_EOL; $entity->setState('Wrong State');
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s9PJu
function name:  (null)
number of ops:  36
compiled vars:  !0 = $entity
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   85     0  E >   NEW                                                  $1      'MyEntity'
          1        DO_FCALL                                          0          
          2        ASSIGN                                                       !0, $1
   86     3        INIT_FCALL                                                   'var_dump'
          4        SEND_VAR                                                     !0
          5        DO_ICALL                                                     
   88     6        ECHO                                                         '%0A----%0A'
   89     7        ECHO                                                         'set+State+as+in+progress+and+set+the+initial+date%0A'
   90     8        INIT_METHOD_CALL                                             !0, 'setState'
          9        SEND_VAL_EX                                                  'in_progress'
         10        DO_FCALL                                          0          
   91    11        INIT_FCALL                                                   'var_dump'
         12        SEND_VAR                                                     !0
         13        DO_ICALL                                                     
   93    14        ECHO                                                         '%0A----%0A'
   94    15        ECHO                                                         'set+State+as+complete+and+leave+date+unchanged%0A'
   95    16        INIT_METHOD_CALL                                             !0, 'setState'
         17        SEND_VAL_EX                                                  'complete'
         18        DO_FCALL                                          0          
   96    19        INIT_FCALL                                                   'var_dump'
         20        SEND_VAR                                                     !0
         21        DO_ICALL                                                     
   98    22        ECHO                                                         '%0A----%0A'
   99    23        ECHO                                                         'change+the+state+back+to+in+progress+and+leave+the+date+unchanged%0A'
  100    24        INIT_METHOD_CALL                                             !0, 'setState'
         25        SEND_VAL_EX                                                  'in_progress'
         26        DO_FCALL                                          0          
  101    27        INIT_FCALL                                                   'var_dump'
         28        SEND_VAR                                                     !0
         29        DO_ICALL                                                     
  103    30        ECHO                                                         '%0A----%0A'
  104    31        ECHO                                                         'use+an+invalid+state+string%0A'
  105    32        INIT_METHOD_CALL                                             !0, 'setState'
         33        SEND_VAL_EX                                                  'Wrong+State'
         34        DO_FCALL                                          0          
         35      > RETURN                                                       1

Class MyEntity:
Function setstate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s9PJu
function name:  setState
number of ops:  12
compiled vars:  !0 = $state
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   30     0  E >   RECV_INIT                                            !0      null
   32     1        INIT_METHOD_CALL                                             'validateState'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0          
   33     4        INIT_METHOD_CALL                                             'updateStateDate'
          5        SEND_VAR_EX                                                  !0
          6        DO_FCALL                                          0          
   34     7        ASSIGN_OBJ                                                   'state'
          8        OP_DATA                                                      !0
   36     9        FETCH_THIS                                           ~4      
         10      > RETURN                                                       ~4
   37    11*     > RETURN                                                       null

End of function setstate

Function getstate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s9PJu
function name:  getState
number of ops:  3
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   41     0  E >   FETCH_OBJ_R                                          ~0      'state'
          1      > RETURN                                                       ~0
   42     2*     > RETURN                                                       null

End of function getstate

Function getdatestarted:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s9PJu
function name:  getDateStarted
number of ops:  3
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   46     0  E >   FETCH_OBJ_R                                          ~0      'dateStarted'
          1      > RETURN                                                       ~0
   47     2*     > RETURN                                                       null

End of function getdatestarted

Function updatestatedate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
Branch analysis from position: 10
Branch analysis from position: 6
filename:       /in/s9PJu
function name:  updateStateDate
number of ops:  18
compiled vars:  !0 = $state
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   53     0  E >   RECV                                                 !0      
   55     1        FETCH_OBJ_R                                          ~1      'state'
          2        IS_NOT_IDENTICAL                                     ~2      !0, ~1
          3      > JMPZ_EX                                              ~2      ~2, ->6
          4    >   IS_IDENTICAL                                         ~3      !0, 'in_progress'
          5        BOOL                                                 ~2      ~3
          6    > > JMPZ_EX                                              ~2      ~2, ->10
          7    >   FETCH_OBJ_R                                          ~4      'dateStarted'
          8        TYPE_CHECK                                        2  ~5      ~4
          9        BOOL                                                 ~2      ~5
         10    > > JMPZ                                                         ~2, ->15
   56    11    >   NEW                                                  $7      'DateTime'
         12        DO_FCALL                                          0          
         13        ASSIGN_OBJ                                                   'dateStarted'
         14        OP_DATA                                                      $7
   59    15    >   FETCH_THIS                                           ~9      
         16      > RETURN                                                       ~9
   60    17*     > RETURN                                                       null

End of function updatestatedate

Function validatestate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 15
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 46) Position 1 = 18, Position 2 = 20
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 29
Branch analysis from position: 21
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/s9PJu
function name:  validateState
number of ops:  32
compiled vars:  !0 = $state
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   62     0  E >   RECV                                                 !0      
   64     1        FRAMELESS_ICALL_3                in_array            ~1      !0, <array>
          2        OP_DATA                                                      <true>
          3        BOOL_NOT                                             ~2      ~1
          4      > JMPZ                                                         ~2, ->15
   65     5    >   NEW                                                  $3      'InvalidArgumentException'
   68     6        FRAMELESS_ICALL_2                implode             ~4      '%22%2C+%22', <array>
          7        ROPE_INIT                                         5  ~6      'Unknown+state+%22'
          8        ROPE_ADD                                          1  ~6      ~6, !0
          9        ROPE_ADD                                          2  ~6      ~6, '%22.+Expected+one+of%3A+%22'
         10        ROPE_ADD                                          3  ~6      ~6, ~4
         11        ROPE_END                                          4  ~5      ~6, '%22'
         12        SEND_VAL_EX                                                  ~5
   65    13        DO_FCALL                                          0          
   68    14      > THROW                                             0          $3
   72    15    >   FETCH_OBJ_R                                          ~10     'state'
         16        IS_IDENTICAL                                         ~11     ~10, 'new'
         17      > JMPZ_EX                                              ~11     ~11, ->20
         18    >   IS_NOT_IDENTICAL                                     ~12     !0, 'in_progress'
         19        BOOL                                                 ~11     ~12
         20    > > JMPZ                                                         ~11, ->29
   73    21    >   NEW                                                  $13     'InvalidArgumentException'
   76    22        ROPE_INIT                                         4  ~15     'Invalid+state+%22'
         23        ROPE_ADD                                          1  ~15     ~15, !0
         24        ROPE_ADD                                          2  ~15     ~15, '%22+specified%2C+Expected%3A+'
         25        ROPE_END                                          3  ~14     ~15, 'in_progress'
         26        SEND_VAL_EX                                                  ~14
   73    27        DO_FCALL                                          0          
   76    28      > THROW                                             0          $13
   81    29    >   FETCH_THIS                                           ~18     
         30      > RETURN                                                       ~18
   82    31*     > RETURN                                                       null

End of function validatestate

End of class MyEntity.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
166.19 ms | 2453 KiB | 14 Q