3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Callcenter_Flagcustomer_Model_Observer { /** * Possibly flag an address. * * Matching an existing flagged address is an inexact science. We want * to catch most trivial circumventions but we don't want to incorrectly * flag good orders - the latter we care about MUCH more than the former. * * At the end of the day, I decided to do a basic normalized levenshtein * comparison, and if there are more than 3 total mistakes between the * flagged address and the incoming address, we flag the order. Region * and Country are exact matches due to not being input by hand. * * @param array[string] $a1 * @param array[string] $a2 * @return boolean */ public static function shouldFlag($a1, $a2) { if ($a1['region'] !== $a2['region']) { return false; } elseif ($a1['country'] !== $a2['country']) { return false; } $score = 0; $score += self::sCompare($a1['street'], $a2['street']); $score += self::compare($a1['city'], $a2['city']); $score += self::pCompare($a1['postcode'], $a2['postcode'], $a1['country'], $a2['country']); if ($score > 3) { return false; } return true; } /** * Compare two normalized strings via levenshtein distance. * * @param string $s1 String to compare. * @param string $s2 String to compare against. * @return float Score. */ private static function compare($s1, $s2) { $sn1 = self::normalize($s1); $sn2 = self::normalize($s2); $score = levenshtein($sn1, $sn2); return $score; } /** * Compare two normalized postcodes via levenshtein distance. * * @param string $s1 String to compare. * @param string $s2 String to compare against. * @param string $c1 Country of $s1. * @param string $c2 Country of $s2. * @return float Score. */ private static function pCompare($s1, $s2, $c1, $c2) { $sn1 = self::normalize($s1); $sn2 = self::normalize($s2); if (strtoupper($c1) === 'US' && strlen($sn1) === 9) { $sn1 = substr($sn1, 0, 5); } if (strtoupper($c2) === 'US' && strlen($sn2) === 9) { $sn2 = substr($sn2, 0, 5); } $score = levenshtein($sn1, $sn2); return $score; } /** * Compare two normalized street names via levenshtein distance. * * @param string $s1 String to compare. * @param string $s2 String to compare against. * @return float Score. */ private static function sCompare($s1, $s2) { $sn1 = self::sNormalize($s1); $sn2 = self::sNormalize($s2); $score = levenshtein($sn1, $sn2); return $score; } /** * Normalize a string for comparison. * * @param string $s String to normalize. * @return string */ private static function normalize($s) { // Normalize to lowercase alphanumeric. $s = strtolower(preg_replace('/[^a-zA-Z0-9]+/', '', $s)); return trim($s); } /** * Normalize an address for comparison. Has a few more steps that * are specific to addresses. * * @param string $s String to normalize. * @return string */ private static function sNormalize($s) { static $mappings = [ 'apt' => 'apartment', 'ave' => 'avenue', 'blvd' => 'boulevard', 'ct' => 'court', 'dr' => 'drive', 'e' => 'east', 'n' => 'north', 'ne' => 'northeast', 'nw' => 'northwest', 'pkwy' => 'parkway', 'pl' => 'place', 'rd' => 'road', 's' => 'south', 'se' => 'southeast', 'st' => 'street', 'sw' => 'southwest', 'w' => 'west', ]; $from = []; $to = []; foreach ($mappings as $key => $value) { $from[] = ' '.$key.' '; $to[] = ' '.$value. ' '; } // Normalize to lowercase alphanumeric with spaces. $s = strtolower(preg_replace('/[^a-zA-Z0-9 ]+/', '', $s)); // Ensure surrounded with spaces. $s = ' '.$s.' '; // Get rid of multiple spaces - turn into single spaces. $s = preg_replace('/\s{2,}/', ' ', $s); // Get rid of ordinal indicators. $s = preg_replace('/(\d+)(?:st|nd|rd|th)/', '\1', $s); // Replace street abbreviations. $s = str_replace($from, $to, $s); return trim($s); } } $result = Callcenter_Flagcustomer_Model_Observer::shouldFlag([ 'street' => '65 JANIS RD', 'city' => 'WESTFIELD', 'region' => 'MA', 'postcode' => '01085-4016', 'country' => 'US', ], [ 'street' => '65 janis road', 'city' => 'westfield', 'region' => 'MA', 'postcode' => '01085', 'country' => 'US', ]); var_dump($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ndTVa
function name:  (null)
number of ops:  9
compiled vars:  !0 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  162     0  E >   INIT_STATIC_METHOD_CALL                                  'Callcenter_Flagcustomer_Model_Observer', 'shouldFlag'
  163     1        SEND_VAL                                                 <array>
  169     2        SEND_VAL                                                 <array>
          3        DO_FCALL                                      0  $1      
  162     4        ASSIGN                                                   !0, $1
  176     5        INIT_FCALL                                               'var_dump'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Class Callcenter_Flagcustomer_Model_Observer:
Function shouldflag:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 50
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ndTVa
function name:  shouldFlag
number of ops:  52
compiled vars:  !0 = $a1, !1 = $a2, !2 = $score
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   22     2        FETCH_DIM_R                                      ~3      !0, 'region'
          3        FETCH_DIM_R                                      ~4      !1, 'region'
          4        IS_NOT_IDENTICAL                                         ~3, ~4
          5      > JMPZ                                                     ~5, ->8
   23     6    > > RETURN                                                   <false>
          7*       JMP                                                      ->13
   24     8    >   FETCH_DIM_R                                      ~6      !0, 'country'
          9        FETCH_DIM_R                                      ~7      !1, 'country'
         10        IS_NOT_IDENTICAL                                         ~6, ~7
         11      > JMPZ                                                     ~8, ->13
   25    12    > > RETURN                                                   <false>
   28    13    >   ASSIGN                                                   !2, 0
   29    14        INIT_STATIC_METHOD_CALL                                  'sCompare'
         15        CHECK_FUNC_ARG                                           
         16        FETCH_DIM_FUNC_ARG                               $10     !0, 'street'
         17        SEND_FUNC_ARG                                            $10
         18        CHECK_FUNC_ARG                                           
         19        FETCH_DIM_FUNC_ARG                               $11     !1, 'street'
         20        SEND_FUNC_ARG                                            $11
         21        DO_FCALL                                      0  $12     
         22        ASSIGN_OP                                     1          !2, $12
   30    23        INIT_STATIC_METHOD_CALL                                  'compare'
         24        CHECK_FUNC_ARG                                           
         25        FETCH_DIM_FUNC_ARG                               $14     !0, 'city'
         26        SEND_FUNC_ARG                                            $14
         27        CHECK_FUNC_ARG                                           
         28        FETCH_DIM_FUNC_ARG                               $15     !1, 'city'
         29        SEND_FUNC_ARG                                            $15
         30        DO_FCALL                                      0  $16     
         31        ASSIGN_OP                                     1          !2, $16
   31    32        INIT_STATIC_METHOD_CALL                                  'pCompare'
         33        CHECK_FUNC_ARG                                           
         34        FETCH_DIM_FUNC_ARG                               $18     !0, 'postcode'
         35        SEND_FUNC_ARG                                            $18
         36        CHECK_FUNC_ARG                                           
         37        FETCH_DIM_FUNC_ARG                               $19     !1, 'postcode'
         38        SEND_FUNC_ARG                                            $19
         39        CHECK_FUNC_ARG                                           
         40        FETCH_DIM_FUNC_ARG                               $20     !0, 'country'
         41        SEND_FUNC_ARG                                            $20
         42        CHECK_FUNC_ARG                                           
         43        FETCH_DIM_FUNC_ARG                               $21     !1, 'country'
         44        SEND_FUNC_ARG                                            $21
         45        DO_FCALL                                      0  $22     
         46        ASSIGN_OP                                     1          !2, $22
   33    47        IS_SMALLER                                               3, !2
         48      > JMPZ                                                     ~24, ->50
   34    49    > > RETURN                                                   <false>
   37    50    > > RETURN                                                   <true>
   38    51*     > RETURN                                                   null

End of function shouldflag

Function compare:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ndTVa
function name:  compare
number of ops:  17
compiled vars:  !0 = $s1, !1 = $s2, !2 = $sn1, !3 = $sn2, !4 = $score
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   48     2        INIT_STATIC_METHOD_CALL                                  'normalize'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $5      
          5        ASSIGN                                                   !2, $5
   49     6        INIT_STATIC_METHOD_CALL                                  'normalize'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $7      
          9        ASSIGN                                                   !3, $7
   51    10        INIT_FCALL                                               'levenshtein'
         11        SEND_VAR                                                 !2
         12        SEND_VAR                                                 !3
         13        DO_ICALL                                         $9      
         14        ASSIGN                                                   !4, $9
   53    15      > RETURN                                                   !4
   54    16*     > RETURN                                                   null

End of function compare

Function pcompare:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 17, Position 2 = 20
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 27
Branch analysis from position: 21
2 jumps found. (Code = 46) Position 1 = 32, Position 2 = 35
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 42
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
Branch analysis from position: 35
Branch analysis from position: 27
Branch analysis from position: 20
filename:       /in/ndTVa
function name:  pCompare
number of ops:  49
compiled vars:  !0 = $s1, !1 = $s2, !2 = $c1, !3 = $c2, !4 = $sn1, !5 = $sn2, !6 = $score
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   66     4        INIT_STATIC_METHOD_CALL                                  'normalize'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $7      
          7        ASSIGN                                                   !4, $7
   67     8        INIT_STATIC_METHOD_CALL                                  'normalize'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0  $9      
         11        ASSIGN                                                   !5, $9
   69    12        INIT_FCALL                                               'strtoupper'
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $11     
         15        IS_IDENTICAL                                     ~12     $11, 'US'
         16      > JMPZ_EX                                          ~12     ~12, ->20
         17    >   STRLEN                                           ~13     !4
         18        IS_IDENTICAL                                     ~14     ~13, 9
         19        BOOL                                             ~12     ~14
         20    > > JMPZ                                                     ~12, ->27
   70    21    >   INIT_FCALL                                               'substr'
         22        SEND_VAR                                                 !4
         23        SEND_VAL                                                 0
         24        SEND_VAL                                                 5
         25        DO_ICALL                                         $15     
         26        ASSIGN                                                   !4, $15
   72    27    >   INIT_FCALL                                               'strtoupper'
         28        SEND_VAR                                                 !3
         29        DO_ICALL                                         $17     
         30        IS_IDENTICAL                                     ~18     $17, 'US'
         31      > JMPZ_EX                                          ~18     ~18, ->35
         32    >   STRLEN                                           ~19     !5
         33        IS_IDENTICAL                                     ~20     ~19, 9
         34        BOOL                                             ~18     ~20
         35    > > JMPZ                                                     ~18, ->42
   73    36    >   INIT_FCALL                                               'substr'
         37        SEND_VAR                                                 !5
         38        SEND_VAL                                                 0
         39        SEND_VAL                                                 5
         40        DO_ICALL                                         $21     
         41        ASSIGN                                                   !5, $21
   76    42    >   INIT_FCALL                                               'levenshtein'
         43        SEND_VAR                                                 !4
         44        SEND_VAR                                                 !5
         45        DO_ICALL                                         $23     
         46        ASSIGN                                                   !6, $23
   78    47      > RETURN                                                   !6
   79    48*     > RETURN                                                   null

End of function pcompare

Function scompare:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ndTVa
function name:  sCompare
number of ops:  17
compiled vars:  !0 = $s1, !1 = $s2, !2 = $sn1, !3 = $sn2, !4 = $score
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   89     2        INIT_STATIC_METHOD_CALL                                  'sNormalize'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $5      
          5        ASSIGN                                                   !2, $5
   90     6        INIT_STATIC_METHOD_CALL                                  'sNormalize'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $7      
          9        ASSIGN                                                   !3, $7
   92    10        INIT_FCALL                                               'levenshtein'
         11        SEND_VAR                                                 !2
         12        SEND_VAR                                                 !3
         13        DO_ICALL                                         $9      
         14        ASSIGN                                                   !4, $9
   94    15      > RETURN                                                   !4
   95    16*     > RETURN                                                   null

End of function scompare

Function normalize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ndTVa
function name:  normalize
number of ops:  15
compiled vars:  !0 = $s
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  103     0  E >   RECV                                             !0      
  105     1        INIT_FCALL                                               'strtolower'
          2        INIT_FCALL                                               'preg_replace'
          3        SEND_VAL                                                 '%2F%5B%5Ea-zA-Z0-9%5D%2B%2F'
          4        SEND_VAL                                                 ''
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $1      
          7        SEND_VAR                                                 $1
          8        DO_ICALL                                         $2      
          9        ASSIGN                                                   !0, $2
  106    10        INIT_FCALL                                               'trim'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $4      
         13      > RETURN                                                   $4
  107    14*     > RETURN                                                   null

End of function normalize

Function snormalize:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 16
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 16
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/ndTVa
function name:  sNormalize
number of ops:  52
compiled vars:  !0 = $s, !1 = $mappings, !2 = $from, !3 = $to, !4 = $value, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  116     0  E >   RECV                                             !0      
  117     1        BIND_STATIC                                              !1
  137     2        ASSIGN                                                   !2, <array>
  138     3        ASSIGN                                                   !3, <array>
  139     4      > FE_RESET_R                                       $8      !1, ->16
          5    > > FE_FETCH_R                                       ~9      $8, !4, ->16
          6    >   ASSIGN                                                   !5, ~9
  140     7        CONCAT                                           ~12     '+', !5
          8        CONCAT                                           ~13     ~12, '+'
          9        ASSIGN_DIM                                               !2
         10        OP_DATA                                                  ~13
  141    11        CONCAT                                           ~15     '+', !4
         12        CONCAT                                           ~16     ~15, '+'
         13        ASSIGN_DIM                                               !3
         14        OP_DATA                                                  ~16
  139    15      > JMP                                                      ->5
         16    >   FE_FREE                                                  $8
  145    17        INIT_FCALL                                               'strtolower'
         18        INIT_FCALL                                               'preg_replace'
         19        SEND_VAL                                                 '%2F%5B%5Ea-zA-Z0-9+%5D%2B%2F'
         20        SEND_VAL                                                 ''
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $17     
         23        SEND_VAR                                                 $17
         24        DO_ICALL                                         $18     
         25        ASSIGN                                                   !0, $18
  148    26        CONCAT                                           ~20     '+', !0
         27        CONCAT                                           ~21     ~20, '+'
         28        ASSIGN                                                   !0, ~21
  151    29        INIT_FCALL                                               'preg_replace'
         30        SEND_VAL                                                 '%2F%5Cs%7B2%2C%7D%2F'
         31        SEND_VAL                                                 '+'
         32        SEND_VAR                                                 !0
         33        DO_ICALL                                         $23     
         34        ASSIGN                                                   !0, $23
  154    35        INIT_FCALL                                               'preg_replace'
         36        SEND_VAL                                                 '%2F%28%5Cd%2B%29%28%3F%3Ast%7Cnd%7Crd%7Cth%29%2F'
         37        SEND_VAL                                                 '%5C1'
         38        SEND_VAR                                                 !0
         39        DO_ICALL                                         $25     
         40        ASSIGN                                                   !0, $25
  157    41        INIT_FCALL                                               'str_replace'
         42        SEND_VAR                                                 !2
         43        SEND_VAR                                                 !3
         44        SEND_VAR                                                 !0
         45        DO_ICALL                                         $27     
         46        ASSIGN                                                   !0, $27
  158    47        INIT_FCALL                                               'trim'
         48        SEND_VAR                                                 !0
         49        DO_ICALL                                         $29     
         50      > RETURN                                                   $29
  159    51*     > RETURN                                                   null

End of function snormalize

End of class Callcenter_Flagcustomer_Model_Observer.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.56 ms | 1408 KiB | 29 Q