3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TranslationError extends \Exception { public function __construct(string $message){ parent::__construct($message); } } class TranslationUtils { /** * List of strings that are required to be in the translation if they appear in the english one */ private const MANDATORY_MATCHES = ['/…/', '/%(\\d+\$)?(b|B|h|H|s|S|c|C|d|i|x|X|e|E|f|g|G|a|A|t|T|n)/']; public static function checkTranslation(string $original, string $translation){ //CHECK MANDATORY STRINGS foreach(static::MANDATORY_MATCHES as $pattern){ $matches = []; if(preg_match_all($pattern, $original, $matches) >= 1){ foreach($matches[0] as $match){ if(strpos($translation, $match) === FALSE){ throw new TranslationError("Missing '" . $match . "' from translation!"); } } } } } public static function isTranslationOk(string $original, string $translation) : bool { try { TranslationUtils::checkTranslation("Loading…", "adsda..."); return true; } catch(TranslationError $ex){ var_dump($ex->getMessage()); return false; } } } var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda...")); var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda…")); var_dump(TranslationUtils::isTranslationOk("Ciao %s", "Ciao %s")); var_dump(TranslationUtils::isTranslationOk("NOPE %d", "YES %s")); var_dump(TranslationUtils::isTranslationOk("Allo %\$1s", "Hello %\$1s %\$2s")); var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda...")); var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda...")); var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda..."));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/urMVf
function name:  (null)
number of ops:  57
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_STATIC_METHOD_CALL                                  'TranslationUtils', 'isTranslationOk'
          2        SEND_VAL                                                 'Loading%E2%80%A6'
          3        SEND_VAL                                                 'adsda...'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   46     7        INIT_FCALL                                               'var_dump'
          8        INIT_STATIC_METHOD_CALL                                  'TranslationUtils', 'isTranslationOk'
          9        SEND_VAL                                                 'Loading%E2%80%A6'
         10        SEND_VAL                                                 'adsda%E2%80%A6'
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
   47    14        INIT_FCALL                                               'var_dump'
         15        INIT_STATIC_METHOD_CALL                                  'TranslationUtils', 'isTranslationOk'
         16        SEND_VAL                                                 'Ciao+%25s'
         17        SEND_VAL                                                 'Ciao+%25s'
         18        DO_FCALL                                      0  $4      
         19        SEND_VAR                                                 $4
         20        DO_ICALL                                                 
   48    21        INIT_FCALL                                               'var_dump'
         22        INIT_STATIC_METHOD_CALL                                  'TranslationUtils', 'isTranslationOk'
         23        SEND_VAL                                                 'NOPE+%25d'
         24        SEND_VAL                                                 'YES+%25s'
         25        DO_FCALL                                      0  $6      
         26        SEND_VAR                                                 $6
         27        DO_ICALL                                                 
   49    28        INIT_FCALL                                               'var_dump'
         29        INIT_STATIC_METHOD_CALL                                  'TranslationUtils', 'isTranslationOk'
         30        SEND_VAL                                                 'Allo+%25%241s'
         31        SEND_VAL                                                 'Hello+%25%241s+%25%242s'
         32        DO_FCALL                                      0  $8      
         33        SEND_VAR                                                 $8
         34        DO_ICALL                                                 
   50    35        INIT_FCALL                                               'var_dump'
         36        INIT_STATIC_METHOD_CALL                                  'TranslationUtils', 'isTranslationOk'
         37        SEND_VAL                                                 'Loading%E2%80%A6'
         38        SEND_VAL                                                 'adsda...'
         39        DO_FCALL                                      0  $10     
         40        SEND_VAR                                                 $10
         41        DO_ICALL                                                 
   51    42        INIT_FCALL                                               'var_dump'
         43        INIT_STATIC_METHOD_CALL                                  'TranslationUtils', 'isTranslationOk'
         44        SEND_VAL                                                 'Loading%E2%80%A6'
         45        SEND_VAL                                                 'adsda...'
         46        DO_FCALL                                      0  $12     
         47        SEND_VAR                                                 $12
         48        DO_ICALL                                                 
   52    49        INIT_FCALL                                               'var_dump'
         50        INIT_STATIC_METHOD_CALL                                  'TranslationUtils', 'isTranslationOk'
         51        SEND_VAL                                                 'Loading%E2%80%A6'
         52        SEND_VAL                                                 'adsda...'
         53        DO_FCALL                                      0  $14     
         54        SEND_VAR                                                 $14
         55        DO_ICALL                                                 
         56      > RETURN                                                   1

Class TranslationError:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/urMVf
function name:  __construct
number of ops:  5
compiled vars:  !0 = $message
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    6     1        INIT_STATIC_METHOD_CALL                                  
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
    7     4      > RETURN                                                   null

End of function __construct

End of class TranslationError.

Class TranslationUtils:
Function checktranslation:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 31
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 31
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 30
Branch analysis from position: 13
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 29
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 29
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 28
Branch analysis from position: 22
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 29
Branch analysis from position: 30
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/urMVf
function name:  checkTranslation
number of ops:  33
compiled vars:  !0 = $original, !1 = $translation, !2 = $pattern, !3 = $matches, !4 = $match
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   20     2        FETCH_CLASS_CONSTANT                             ~5      'MANDATORY_MATCHES'
          3      > FE_RESET_R                                       $6      ~5, ->31
          4    > > FE_FETCH_R                                               $6, !2, ->31
   21     5    >   ASSIGN                                                   !3, <array>
   22     6        INIT_FCALL                                               'preg_match_all'
          7        SEND_VAR                                                 !2
          8        SEND_VAR                                                 !0
          9        SEND_REF                                                 !3
         10        DO_ICALL                                         $8      
         11        IS_SMALLER_OR_EQUAL                                      1, $8
         12      > JMPZ                                                     ~9, ->30
   23    13    >   FETCH_DIM_R                                      ~10     !3, 0
         14      > FE_RESET_R                                       $11     ~10, ->29
         15    > > FE_FETCH_R                                               $11, !4, ->29
   24    16    >   INIT_FCALL                                               'strpos'
         17        SEND_VAR                                                 !1
         18        SEND_VAR                                                 !4
         19        DO_ICALL                                         $12     
         20        TYPE_CHECK                                    4          $12
         21      > JMPZ                                                     ~13, ->28
   25    22    >   NEW                                              $14     'TranslationError'
         23        CONCAT                                           ~15     'Missing+%27', !4
         24        CONCAT                                           ~16     ~15, '%27+from+translation%21'
         25        SEND_VAL_EX                                              ~16
         26        DO_FCALL                                      0          
         27      > THROW                                         0          $14
   23    28    > > JMP                                                      ->15
         29    >   FE_FREE                                                  $11
   20    30    > > JMP                                                      ->4
         31    >   FE_FREE                                                  $6
   31    32      > RETURN                                                   null

End of function checktranslation

Function istranslationok:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 8
Branch analysis from position: 8
2 jumps found. (Code = 107) Position 1 = 9, Position 2 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/urMVf
function name:  isTranslationOk
number of ops:  17
compiled vars:  !0 = $original, !1 = $translation, !2 = $ex
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   35     2        INIT_STATIC_METHOD_CALL                                  'TranslationUtils', 'checkTranslation'
          3        SEND_VAL                                                 'Loading%E2%80%A6'
          4        SEND_VAL                                                 'adsda...'
          5        DO_FCALL                                      0          
   36     6      > RETURN                                                   <true>
          7*       JMP                                                      ->15
   37     8  E > > CATCH                                       last         'TranslationError'
   38     9    >   INIT_FCALL                                               'var_dump'
         10        INIT_METHOD_CALL                                         !2, 'getMessage'
         11        DO_FCALL                                      0  $4      
         12        SEND_VAR                                                 $4
         13        DO_ICALL                                                 
   39    14      > RETURN                                                   <false>
   42    15*       VERIFY_RETURN_TYPE                                       
         16*     > RETURN                                                   null

End of function istranslationok

End of class TranslationUtils.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
184.34 ms | 1408 KiB | 19 Q