Completed
Push — master ( 5cb919...3189ad )
by Joshua
15s queued 11s
created

PhoneNumberMatcher::parseAndVerify()   C

Complexity

Conditions 13
Paths 60

Size

Total Lines 48
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 24
nc 60
nop 2
dl 0
loc 48
ccs 21
cts 21
cp 1
crap 13
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace libphonenumber;
4
5
use libphonenumber\Leniency\AbstractLeniency;
6
7
/**
8
 * A class that finds and extracts telephone numbers from $text.
9
 * Instances can be created using PhoneNumberUtil::findNumbers()
10
 *
11
 * Vanity numbers (phone numbers using alphabetic digits such as '1-800-SIX-FLAGS' are
12
 * not found.
13
 *
14
 * @package libphonenumber
15
 */
16
class PhoneNumberMatcher implements \Iterator
17
{
18
    protected static $initialized = false;
19
20
    /**
21
     * The phone number pattern used by $this->find(), similar to
22
     * PhoneNumberUtil::VALID_PHONE_NUMBER, but with the following differences:
23
     * <ul>
24
     *   <li>All captures are limited in order to place an upper bound to the text matched by the
25
     *       pattern.
26
     * <ul>
27
     *   <li>Leading punctuation / plus signs are limited.
28
     *   <li>Consecutive occurrences of punctuation are limited.
29
     *   <li>Number of digits is limited.
30
     * </ul>
31
     *   <li>No whitespace is allowed at the start or end.
32
     *   <li>No alpha digits (vanity numbers such as 1-800-SIX-FLAGS) are currently supported.
33
     * </ul>
34
     *
35
     * @var string
36
     */
37
    protected static $pattern;
38
39
    /**
40
     * Matches strings that look like publication pages. Example:
41
     * <pre>Computing Complete Answers to Queries in the Presence of Limited Access Patterns.
42
     * Chen Li. VLDB J. 12(3): 211-227 (2003).</pre>
43
     *
44
     * The string "211-227 (2003)" is not a telephone number.
45
     *
46
     * @var string
47
     */
48
    protected static $pubPages = "\\d{1,5}-+\\d{1,5}\\s{0,4}\\(\\d{1,4}";
49
50
    /**
51
     * Matches strings that look like dates using "/" as a separator. Examples 3/10/2011, 31/10/2011 or
52
     * 08/31/95.
53
     *
54
     * @var string
55
     */
56
    protected static $slashSeparatedDates = "(?:(?:[0-3]?\\d/[01]?\\d)|(?:[01]?\\d/[0-3]?\\d))/(?:[12]\\d)?\\d{2}";
57
58
    /**
59
     * Matches timestamps. Examples: "2012-01-02 08:00". Note that the reg-ex does not include the
60
     * trailing ":\d\d" -- that is covered by timeStampsSuffix.
61
     *
62
     * @var string
63
     */
64
    protected static $timeStamps = "[12]\\d{3}[-/]?[01]\\d[-/]?[0-3]\\d +[0-2]\\d$";
65
    protected static $timeStampsSuffix = ":[0-5]\\d";
66
67
    /**
68
     * Pattern to check that brackets match. Opening brackets should be closed within a phone number.
69
     * This also checks that there is something inside the brackets. Having no brackets at all is also
70
     * fine.
71
     *
72
     * @var string
73
     */
74
    protected static $matchingBrackets;
75
76
    /**
77
     * Patterns used to extract phone numbers from a larger phone-number-like pattern. These are
78
     * ordered according to specificity. For example, white-space is last since that is frequently
79
     * used in numbers, not just to separate two numbers. We have separate patterns since we don't
80
     * want to break up the phone-number-like text on more than one different kind of symbol at one
81
     * time, although symbols of the same type (e.g. space) can be safely grouped together.
82
     *
83
     * Note that if there is a match, we will always check any text found up to the first match as
84
     * well.
85
     *
86
     * @var string[]
87
     */
88
    protected static $innerMatches = array();
89
90
    /**
91
     * Punctuation that may be at the start of a phone number - brackets and plus signs.
92
     *
93
     * @var string
94
     */
95
    protected static $leadClass;
96
97
    /**
98
     * Prefix of the files
99
     * @var string
100
     */
101
    protected static $alternateFormatsFilePrefix;
102
    const META_DATA_FILE_PREFIX = 'PhoneNumberAlternateFormats';
103
104 1
    protected static function init()
105
    {
106 1
        static::$alternateFormatsFilePrefix = dirname(__FILE__) . '/data/' . static::META_DATA_FILE_PREFIX;
107
108 1
        static::$innerMatches = array(
109
            // Breaks on the slash - e.g. "651-234-2345/332-445-1234"
110 1
            "/+(.*)",
111
            // Note that the bracket here is inside the capturing group, since we consider it part of the
112
            // phone number. Will match a pattern like "(650) 223 3345 (754) 223 3321".
113 1
            "(\\([^(]*)",
114
            // Breaks on a hyphen - e.g. "12345 - 332-445-1234 is my number."
115
            // We require a space on either side of the hyphen for it to be considered a separator.
116 1
            "(?:\\p{Z}-|-\\p{Z})\\p{Z}*(.+)",
117
            // Various types of wide hyphens. Note we have decided not to enforce a space here, since it's
118
            // possible that it's supposed to be used to break two numbers without spaces, and we haven't
119
            // seen many instances of it used within a number.
120 1
            "[‒-―-]\\p{Z}*(.+)",
121
            // Breaks on a full stop - e.g. "12345. 332-445-1234 is my number."
122 1
            "\\.+\\p{Z}*([^.]+)",
123
            // Breaks on space - e.g. "3324451234 8002341234"
124
            "\\p{Z}+(\\P{Z}+)"
125 1
        );
126
127
        /*
128
         * Builds the matchingBrackets and pattern regular expressions. The building blocks exist
129
         * to make the pattern more easily understood.
130
         */
131
132 1
        $openingParens = "(\\[\xEF\xBC\x88\xEF\xBC\xBB";
133 1
        $closingParens = ")\\]\xEF\xBC\x89\xEF\xBC\xBD";
134 1
        $nonParens = "[^" . $openingParens . $closingParens . "]";
135
136
        // Limit on the number of pairs of brackets in a phone number.
137 1
        $bracketPairLimit = static::limit(0, 3);
138
139
        /*
140
         * An opening bracket at the beginning may not be closed, but subsequent ones should be.  It's
141
         * also possible that the leading bracket was dropped, so we shouldn't be surprised if we see a
142
         * closing bracket first. We limit the sets of brackets in a phone number to four.
143
         */
144 1
        static::$matchingBrackets =
145 1
            "(?:[" . $openingParens . "])?" . "(?:" . $nonParens . "+" . "[" . $closingParens . "])?"
146 1
            . $nonParens . "+"
147 1
            . "(?:[" . $openingParens . "]" . $nonParens . "+[" . $closingParens . "])" . $bracketPairLimit
148 1
            . $nonParens . "*";
149
150
        // Limit on the number of leading (plus) characters.
151 1
        $leadLimit = static::limit(0, 2);
152
153
        // Limit on the number of consecutive punctuation characters.
154 1
        $punctuationLimit = static::limit(0, 4);
155
156
        /*
157
         * The maximum number of digits allowed in a digit-separated block. As we allow all digits in a
158
         * single block, set high enough to accommodate the entire national number and the international
159
         * country code
160
         */
161 1
        $digitBlockLimit = PhoneNumberUtil::MAX_LENGTH_FOR_NSN + PhoneNumberUtil::MAX_LENGTH_COUNTRY_CODE;
162
163
        /*
164
         * Limit on the number of blocks separated by the punctuation. Uses digitBlockLimit since some
165
         * formats use spaces to separate each digit
166
         */
167 1
        $blockLimit = static::limit(0, $digitBlockLimit);
168
169
        // A punctuation sequence allowing white space
170 1
        $punctuation = '[' . PhoneNumberUtil::VALID_PUNCTUATION . ']' . $punctuationLimit;
171
172
        // A digits block without punctuation.
173 1
        $digitSequence = "\\p{Nd}" . static::limit(1, $digitBlockLimit);
174
175
176 1
        $leadClassChars = $openingParens . PhoneNumberUtil::PLUS_CHARS;
177 1
        $leadClass = '[' . $leadClassChars . ']';
178 1
        static::$leadClass = $leadClass;
179
180
        // Init extension patterns from PhoneNumberUtil
181 1
        PhoneNumberUtil::initCapturingExtnDigits();
182 1
        PhoneNumberUtil::initExtnPatterns();
183
184
185
        // Phone number pattern allowing optional punctuation.
186 1
        static::$pattern = "(?:" . $leadClass . $punctuation . ")" . $leadLimit
187 1
            . $digitSequence . "(?:" . $punctuation . $digitSequence . ")" . $blockLimit
188 1
            . "(?:" . PhoneNumberUtil::$EXTN_PATTERNS_FOR_MATCHING . ")?";
189
190 1
        static::$initialized = true;
191 1
    }
192
193
    /**
194
     * Helper function to generate regular expression with an upper and lower limit.
195
     *
196
     * @param int $lower
197
     * @param int $upper
198
     * @return string
199
     */
200 1
    protected static function limit($lower, $upper)
201
    {
202 1
        if (($lower < 0) || ($upper <= 0) || ($upper < $lower)) {
203
            throw new \InvalidArgumentException();
204
        }
205
206 1
        return '{' . $lower . ',' . $upper . '}';
207
    }
208
209
    /**
210
     * The phone number utility.
211
     * @var PhoneNumberUtil
212
     */
213
    protected $phoneUtil;
214
215
    /**
216
     * The text searched for phone numbers.
217
     * @var string
218
     */
219
    protected $text;
220
221
    /**
222
     * The region (country) to assume for phone numbers without an international prefix, possibly
223
     * null.
224
     * @var string
225
     */
226
    protected $preferredRegion;
227
228
    /**
229
     * The degrees of validation requested.
230
     * @var AbstractLeniency
231
     */
232
    protected $leniency;
233
234
    /**
235
     * The maximum number of retires after matching an invalid number.
236
     * @var int
237
     */
238
    protected $maxTries;
239
240
    /**
241
     * One of:
242
     *  - NOT_READY
243
     *  - READY
244
     *  - DONE
245
     * @var string
246
     */
247
    protected $state = 'NOT_READY';
248
249
    /**
250
     * The last successful match, null unless $this->state = READY
251
     * @var PhoneNumberMatch
252
     */
253
    protected $lastMatch;
254
255
    /**
256
     * The next index to start searching at. Undefined when $this->state = DONE
257
     * @var int
258
     */
259
    protected $searchIndex = 0;
260
261
    /**
262
     * Creates a new instance. See the factory methods in PhoneNumberUtil on how to obtain a new instance.
263
     *
264
     *
265
     * @param PhoneNumberUtil $util The Phone Number Util to use
266
     * @param string|null $text The text that we will search, null for no text
267
     * @param string|null $country The country to assume for phone numbers not written in international format.
268
     *  (with a leading plus, or with the international dialling prefix of the specified region).
269
     *  May be null, or "ZZ" if only numbers with a leading plus should be considered.
270
     * @param AbstractLeniency $leniency The leniency to use when evaluating candidate phone numbers
271
     * @param int $maxTries The maximum number of invalid numbers to try before giving up on the text.
272
     *  This is to cover degenerate cases where the text has a lot of false positives in it. Must be >= 0
273
     * @throws \NullPointerException
274
     * @throws \InvalidArgumentException
275
     */
276 205
    public function __construct(PhoneNumberUtil $util, $text, $country, AbstractLeniency $leniency, $maxTries)
277
    {
278 205
        if ($maxTries < 0) {
279
            throw new \InvalidArgumentException();
280
        }
281
282 205
        $this->phoneUtil = $util;
283 205
        $this->text = ($text !== null) ? $text : "";
284 205
        $this->preferredRegion = $country;
285 205
        $this->leniency = $leniency;
286 205
        $this->maxTries = $maxTries;
287
288 205
        if (static::$initialized === false) {
289 1
            static::init();
290 1
        }
291 205
    }
292
293
    /**
294
     * Attempts to find the next subsequence in the searched sequence on or after {@code searchIndex}
295
     * that represents a phone number. Returns the next match, null if none was found.
296
     *
297
     * @param int $index The search index to start searching at
298
     * @return PhoneNumberMatch|null The Phone Number Match found, null if none can be found
299
     */
300 199
    protected function find($index)
301
    {
302 199
        $matcher = new Matcher(static::$pattern, $this->text);
303 199
        while (($this->maxTries > 0) && $matcher->find($index)) {
304 198
            $start = $matcher->start();
305 198
            $cutLength = $matcher->end() - $start;
306 198
            $candidate = mb_substr($this->text, $start, $cutLength);
307
308
            // Check for extra numbers at the end.
309
            // TODO: This is the place to start when trying to support extraction of multiple phone number
310
            // from split notations (+41 49 123 45 67 / 68).
311 198
            $candidate = static::trimAfterFirstMatch(PhoneNumberUtil::$SECOND_NUMBER_START_PATTERN, $candidate);
312
313 198
            $match = $this->extractMatch($candidate, $start);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $match is correct as $this->extractMatch($candidate, $start) targeting libphonenumber\PhoneNumberMatcher::extractMatch() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
314 198
            if ($match !== null) {
315 126
                return $match;
316
            }
317
318 90
            $index = $start + mb_strlen($candidate);
319 90
            $this->maxTries--;
320 90
        }
321
322 93
        return null;
323
    }
324
325
    /**
326
     * Trims away any characters after the first match of $pattern in $candidate,
327
     * returning the trimmed version.
328
     *
329
     * @param string $pattern
330
     * @param string $candidate
331
     * @return string
332
     */
333 198
    protected static function trimAfterFirstMatch($pattern, $candidate)
334
    {
335 198
        $trailingCharsMatcher = new Matcher($pattern, $candidate);
336 198
        if ($trailingCharsMatcher->find()) {
337 10
            $startChar = $trailingCharsMatcher->start();
338 10
            $candidate = mb_substr($candidate, 0, $startChar);
339 10
        }
340 198
        return $candidate;
341
    }
342
343
    /**
344
     * Helper method to determine if a character is a Latin-script letter or not. For our purposes,
345
     * combining marks should also return true since we assume they have been added to a preceding
346
     * Latin character.
347
     *
348
     * @param string $letter
349
     * @return bool
350
     * @internal
351
     */
352 58
    public static function isLatinLetter($letter)
353
    {
354
        // Combining marks are a subset of non-spacing-mark.
355 58
        if (preg_match('/\p{L}/u', $letter) !== 1 && preg_match('/\p{Mn}/u', $letter) !== 1) {
356 52
            return false;
357
        }
358
359 9
        return (preg_match('/\p{Latin}/u', $letter) === 1)
360 9
        || (preg_match('/\pM+/u', $letter) === 1);
361
    }
362
363
    /**
364
     * @param string $character
365
     * @return bool
366
     */
367 47
    protected static function isInvalidPunctuationSymbol($character)
368
    {
369 47
        return $character == '%' || preg_match('/\p{Sc}/u', $character);
370
    }
371
372
    /**
373
     * Attempts to extract a match from a $candidate.
374
     *
375
     * @param string $candidate The candidate text that might contain a phone number
376
     * @param int $offset The offset of $candidate within $this->text
377
     * @return PhoneNumberMatch|null The match found, null if none can be found
378
     */
379 198
    protected function extractMatch($candidate, $offset)
380
    {
381
        // Skip a match that is more likely to be a date.
382 198
        $dateMatcher = new Matcher(static::$slashSeparatedDates, $candidate);
383 198
        if ($dateMatcher->find()) {
384 33
            return null;
385
        }
386
387
        // Skip potential time-stamps.
388 178
        $timeStampMatcher = new Matcher(static::$timeStamps, $candidate);
389 178
        if ($timeStampMatcher->find()) {
390 20
            $followingText = mb_substr($this->text, $offset + mb_strlen($candidate));
391 20
            $timeStampSuffixMatcher = new Matcher(static::$timeStampsSuffix, $followingText);
392 20
            if ($timeStampSuffixMatcher->lookingAt()) {
393 16
                return null;
394
            }
395 4
        }
396
397
        // Try to come up with a valid match given the entire candidate.
398 178
        $match = $this->parseAndVerify($candidate, $offset);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $match is correct as $this->parseAndVerify($candidate, $offset) targeting libphonenumber\PhoneNumb...tcher::parseAndVerify() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
399 178
        if ($match !== null) {
0 ignored issues
show
introduced by
The condition $match !== null is always false.
Loading history...
400 124
            return $match;
401
        }
402
403
        // If that failed, try to find an "inner match" - there might be a phone number within this
404
        // candidate.
405 74
        return $this->extractInnerMatch($candidate, $offset);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->extractInnerMatch($candidate, $offset) targeting libphonenumber\PhoneNumb...er::extractInnerMatch() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
406
    }
407
408
    /**
409
     * Attempts to extract a match from $candidate if the whole candidate does not qualify as a
410
     * match.
411
     *
412
     * @param string $candidate The candidate text that might contact a phone number
413
     * @param int $offset The current offset of $candidate within $this->text
414
     * @return PhoneNumberMatch|null The match found, null if none can be found
415
     */
416 74
    protected function extractInnerMatch($candidate, $offset)
417
    {
418 74
        foreach (static::$innerMatches as $possibleInnerMatch) {
419 74
            $groupMatcher = new Matcher($possibleInnerMatch, $candidate);
420 74
            $isFirstMatch = true;
421
422 74
            while ($groupMatcher->find() && $this->maxTries > 0) {
423 18
                if ($isFirstMatch) {
424
                    // We should handle any group before this one too.
425 18
                    $group = static::trimAfterFirstMatch(PhoneNumberUtil::$UNWANTED_END_CHAR_PATTERN,
426 18
                        mb_substr($candidate, 0, $groupMatcher->start()));
427
428 18
                    $match = $this->parseAndVerify($group, $offset);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $match is correct as $this->parseAndVerify($group, $offset) targeting libphonenumber\PhoneNumb...tcher::parseAndVerify() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
429 18
                    if ($match !== null) {
430 6
                        return $match;
431
                    }
432 15
                    $this->maxTries--;
433 15
                    $isFirstMatch = false;
434 15
                }
435 15
                $group = static::trimAfterFirstMatch(PhoneNumberUtil::$UNWANTED_END_CHAR_PATTERN,
436 15
                    $groupMatcher->group(1));
437 15
                $match = $this->parseAndVerify($group, $offset + $groupMatcher->start(1));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $match is correct as $this->parseAndVerify($g...groupMatcher->start(1)) targeting libphonenumber\PhoneNumb...tcher::parseAndVerify() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
438 15
                if ($match !== null) {
439 7
                    return $match;
440
                }
441 14
                $this->maxTries--;
442 14
            }
443 74
        }
444 70
        return null;
445
    }
446
447
    /**
448
     * Parses a phone number from the $candidate} using PhoneNumberUtil::parse() and
449
     * verifies it matches the requested leniency. If parsing and verification succeed, a
450
     * corresponding PhoneNumberMatch is returned, otherwise this method returns null.
451
     *
452
     * @param string $candidate The candidate match
453
     * @param int $offset The offset of $candidate within $this->text
454
     * @return PhoneNumberMatch|null The parsed and validated phone number match, or null
455
     */
456 178
    protected function parseAndVerify($candidate, $offset)
457
    {
458
        try {
459
            // Check the candidate doesn't contain any formatting which would indicate that it really
460
            // isn't a phone number
461 178
            $matchingBracketsMatcher = new Matcher(static::$matchingBrackets, $candidate);
462 178
            $pubPagesMatcher = new Matcher(static::$pubPages, $candidate);
463 178
            if (!$matchingBracketsMatcher->matches() || $pubPagesMatcher->find()) {
464 11
                return null;
465
            }
466
467
            // If leniency is set to VALID or stricter, we also want to skip numbers that are surrounded
468
            // by Latin alphabetic characters, to skip cases like abc8005001234 or 8005001234def.
469 178
            if ($this->leniency->compareTo(Leniency::VALID()) >= 0) {
470
                // If the candidate is not at the start of the text, and does not start with phone-number
471
                // punctuation, check the previous character.
472 135
                $leadClassMatcher = new Matcher(static::$leadClass, $candidate);
473 135
                if ($offset > 0 && !$leadClassMatcher->lookingAt()) {
474 42
                    $previousChar = mb_substr($this->text, $offset - 1, 1);
475
                    // We return null if it is a latin letter or an invalid punctuation symbol.
476 42
                    if (static::isInvalidPunctuationSymbol($previousChar) || static::isLatinLetter($previousChar)) {
477 2
                        return null;
478
                    }
479 42
                }
480 135
                $lastCharIndex = $offset + mb_strlen($candidate);
481 135
                if ($lastCharIndex < mb_strlen($this->text)) {
482 38
                    $nextChar = mb_substr($this->text, $lastCharIndex, 1);
483 38
                    if (static::isInvalidPunctuationSymbol($nextChar) || static::isLatinLetter($nextChar)) {
484 2
                        return null;
485
                    }
486 37
                }
487 134
            }
488
489 177
            $number = $this->phoneUtil->parseAndKeepRawInput($candidate, $this->preferredRegion);
490
491
            if ($this->leniency->verify($number, $candidate, $this->phoneUtil)) {
492
                // We used parseAndKeepRawInput to create this number, but for now we don't return the extra
493
                // values parsed. TODO: stop clearing all values here and switch all users over
494
                // to using rawInput() rather than the rawString() of PhoneNumberMatch
495
                $number->clearCountryCodeSource();
496
                $number->clearRawInput();
497
                $number->clearPreferredDomesticCarrierCode();
498
                return new PhoneNumberMatch($offset, $candidate, $number);
499
            }
500
        } catch (NumberParseException $e) {
501
            // ignore and continue
502
        }
503 176
        return null;
504 176
    }
505 176
506 176
    /**
507
     * @param PhoneNumberUtil $util
508
     * @param PhoneNumber $number
509
     * @param string $normalizedCandidate
510
     * @param string[] $formattedNumberGroups
511 176
     * @return bool
512
     */
513
    public static function allNumberGroupsRemainGrouped(
514
        PhoneNumberUtil $util,
515 126
        PhoneNumber $number,
516 126
        $normalizedCandidate,
517 126
        $formattedNumberGroups
518 126
    ) {
519
        $fromIndex = 0;
520 72
        if ($number->getCountryCodeSource() !== CountryCodeSource::FROM_DEFAULT_COUNTRY) {
521
            // First skip the country code if the normalized candidate contained it.
522
            $countryCode = $number->getCountryCode();
523 72
            $fromIndex = mb_strpos($normalizedCandidate, $countryCode) + mb_strlen($countryCode);
524
        }
525
526
        // Check each group of consecutive digits are not broken into separate groupings in the
527
        // $normalizedCandidate string.
528
        $formattedNumberGroupsLength = count($formattedNumberGroups);
529
        for ($i = 0; $i < $formattedNumberGroupsLength; $i++) {
530
            // Fails if the substring of $normalizedCandidate starting from $fromIndex
531
            // doesn't contain the consecutive digits in $formattedNumberGroups[$i].
532
            $fromIndex = mb_strpos($normalizedCandidate, $formattedNumberGroups[$i], $fromIndex);
533 26
            if ($fromIndex === false) {
534
                return false;
535
            }
536
537
            // Moves $fromIndex forward.
538
            $fromIndex += mb_strlen($formattedNumberGroups[$i]);
539 26
            if ($i === 0 && $fromIndex < mb_strlen($normalizedCandidate)) {
540 26
                // We are at the position right after the NDC. We get the region used for formatting
541
                // information based on the country code in the phone number, rather than the number itself,
542 10
                // as we do not need to distinguish between different countries with the same country
543 10
                // calling code and this is faster.
544 10
                $region = $util->getRegionCodeForCountryCode($number->getCountryCode());
545
546
                if ($util->getNddPrefixForRegion($region, true) !== null
547
                    && is_int(mb_substr($normalizedCandidate, $fromIndex, 1))
548 26
                ) {
549 26
                    // This means there is no formatting symbol after the NDC. In this case, we only
550
                    // accept the number if there is no formatting symbol at all in the number, except
551
                    // for extensions. This is only important for countries with national prefixes.
552 26
                    $nationalSignificantNumber = $util->getNationalSignificantNumber($number);
553 26
                    return mb_substr(
554 8
                        mb_substr($normalizedCandidate, $fromIndex - mb_strlen($formattedNumberGroups[$i])),
555
                        mb_strlen($nationalSignificantNumber)
556
                    ) === $nationalSignificantNumber;
557
                }
558 25
            }
559 25
        }
560
        // The check here makes sure that we haven't mistakenly already used the extension to
561
        // match the last group of the subscriber number. Note the extension cannot have
562
        // formatting in-between digits
563
564 25
        if ($number->hasExtension()) {
565
            return mb_strpos(mb_substr($normalizedCandidate, $fromIndex), $number->getExtension()) !== false;
566 25
        }
567 25
568 25
        return true;
569
    }
570
571
    /**
572
     * @param PhoneNumberUtil $util
573
     * @param PhoneNumber $number
574
     * @param string $normalizedCandidate
575
     * @param string[] $formattedNumberGroups
576
     * @return bool
577
     */
578 25
    public static function allNumberGroupsAreExactlyPresent(
579 25
        PhoneNumberUtil $util,
580
        PhoneNumber $number,
581
        $normalizedCandidate,
582
        $formattedNumberGroups
583
    ) {
584 25
        $candidateGroups = preg_split(PhoneNumberUtil::NON_DIGITS_PATTERN, $normalizedCandidate);
585 4
586
        // Set this to the last group, skipping it if the number has an extension.
587
        $candidateNumberGroupIndex = $number->hasExtension() ? count($candidateGroups) - 2 : count($candidateGroups) - 1;
0 ignored issues
show
Bug introduced by
It seems like $candidateGroups can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

587
        $candidateNumberGroupIndex = $number->hasExtension() ? count(/** @scrutinizer ignore-type */ $candidateGroups) - 2 : count($candidateGroups) - 1;
Loading history...
588 21
589
        // First we check if the national significant number is formatted as a block.
590
        // We use contains and not equals, since the national significant number may be present with
591
        // a prefix such as a national number prefix, or the country code itself.
592
        if (count($candidateGroups) == 1
593
            || mb_strpos($candidateGroups[$candidateNumberGroupIndex],
594
                $util->getNationalSignificantNumber($number)) !== false
595
        ) {
596
            return true;
597
        }
598 26
599
        // Starting from the end, go through in reverse, excluding the first group, and check the
600
        // candidate and number groups are the same.
601
        for ($formattedNumberGroupIndex = (count($formattedNumberGroups) - 1);
602
             $formattedNumberGroupIndex > 0 && $candidateNumberGroupIndex >= 0;
603
             $formattedNumberGroupIndex--, $candidateNumberGroupIndex--) {
604 26
            if ($candidateGroups[$candidateNumberGroupIndex] != $formattedNumberGroups[$formattedNumberGroupIndex]) {
605
                return false;
606
            }
607 26
        }
608
609
        // Now check the first group. There may be a national prefix at the start, so we only check
610
        // that the candidate group ends with the formatted number group.
611
        return ($candidateNumberGroupIndex >= 0
612 26
            && mb_substr($candidateGroups[$candidateNumberGroupIndex],
613 26
                -mb_strlen($formattedNumberGroups[0])) == $formattedNumberGroups[0]);
614 23
    }
615 26
616 8
    /**
617
     * Helper method to get the national-number part of a number, formatted without any national
618
     * prefix, and return it as a set of digit blocks that would be formatted together.
619
     *
620
     * @param PhoneNumberUtil $util
621 18
     * @param PhoneNumber $number
622 18
     * @param NumberFormat $formattingPattern
623 18
     * @return string[]
624 18
     */
625 5
    protected static function getNationalNumberGroups(
626
        PhoneNumberUtil $util,
627 18
        PhoneNumber $number,
628
        NumberFormat $formattingPattern = null
629
    ) {
630
        if ($formattingPattern === null) {
631
            // This will be in the format +CC-DG;ext=EXT where DG represents groups of digits.
632 18
            $rfc3966Format = $util->format($number, PhoneNumberFormat::RFC3966);
633 18
            // We remove the extension part from the formatted string before splitting it into different
634
            // groups.
635
            $endIndex = mb_strpos($rfc3966Format, ';');
636
            if ($endIndex === false) {
637
                $endIndex = mb_strlen($rfc3966Format);
638
            }
639
640
            // The country-code will have a '-' following it.
641
            $startIndex = mb_strpos($rfc3966Format, '-') + 1;
642
            return explode('-', mb_substr($rfc3966Format, $startIndex, $endIndex - $startIndex));
643
        } else {
644
            // We format the NSN only, and split that according to the separator.
645 52
            $nationalSignificantNumber = $util->getNationalSignificantNumber($number);
646
            return explode('-', $util->formatNsnUsingPattern($nationalSignificantNumber, $formattingPattern,
647
                PhoneNumberFormat::RFC3966));
648
        }
649
    }
650 52
651
    /**
652 52
     * @param PhoneNumber $number
653
     * @param string $candidate
654
     * @param PhoneNumberUtil $util
655 52
     * @param \Closure $checker
656 52
     * @return bool
657 42
     */
658 42
    public static function checkNumberGroupingIsValid(
659
        PhoneNumber $number,
660
        $candidate,
661 52
        PhoneNumberUtil $util,
662 52
        \Closure $checker
663
    ) {
664
        // TODO: Evaluate how this works for other locales (testing has been limited to NANPA regions)
665 13
        // and optimise if necessary.
666 13
        $normalizedCandidate = PhoneNumberUtil::normalizeDigits($candidate, true /* keep non-digits */);
667 13
        $formattedNumberGroups = static::getNationalNumberGroups($util, $number, null);
668
        if ($checker($util, $number, $normalizedCandidate, $formattedNumberGroups)) {
669
            return true;
670
        }
671
672
        // If this didn't pass, see if there are any alternative formats, and try them instead.
673
        $alternateFormats = static::getAlternateFormatsForCountry($number->getCountryCode());
674
675
        if ($alternateFormats !== null) {
676
            foreach ($alternateFormats->numberFormats() as $alternateFormat) {
677
                $formattedNumberGroups = static::getNationalNumberGroups($util, $number, $alternateFormat);
678 52
                if ($checker($util, $number, $normalizedCandidate, $formattedNumberGroups)) {
679
                    return true;
680
                }
681
            }
682
        }
683
        return false;
684
    }
685
686 52
    /**
687 52
     * @param PhoneNumber $number
688 52
     * @param string $candidate
689 39
     * @return bool
690
     */
691
    public static function containsMoreThanOneSlashInNationalNumber(PhoneNumber $number, $candidate)
692
    {
693 13
        $firstSlashInBodyIndex = mb_strpos($candidate, '/');
694
        if ($firstSlashInBodyIndex === false) {
695 13
            // No slashes, this is okay
696 13
            return false;
697 13
        }
698 13
699 11
        // Now look for a second one.
700
        $secondSlashInBodyIndex = mb_strpos($candidate, '/', $firstSlashInBodyIndex + 1);
701 11
        if ($secondSlashInBodyIndex === false) {
702 2
            // Only one slash, this is okay
703 2
            return false;
704
        }
705
706
        // If the first slash is after the country calling code, this is permitted
707
        $candidateHasCountryCode = ($number->getCountryCodeSource() === CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN
708
            || $number->getCountryCodeSource() === CountryCodeSource::FROM_NUMBER_WITHOUT_PLUS_SIGN);
709
710
        if ($candidateHasCountryCode
711 53
            && PhoneNumberUtil::normalizeDigitsOnly(
712
                mb_substr($candidate, 0, $firstSlashInBodyIndex)
713 53
            ) == $number->getCountryCode()
714 53
        ) {
715
            // Any more slashes and this is illegal
716 51
            return (mb_strpos(mb_substr($candidate, $secondSlashInBodyIndex + 1), '/') !== false);
717
        }
718
719
        return true;
720 2
    }
721 2
722
    /**
723 1
     * @param PhoneNumber $number
724
     * @param string $candidate
725
     * @param PhoneNumberUtil $util
726
     * @return bool
727 1
     */
728 1
    public static function containsOnlyValidXChars(PhoneNumber $number, $candidate, PhoneNumberUtil $util)
729
    {
730
        // The characters 'x' and 'X' can be (1) a carrier code, in which case they always precede the
731 1
        // national significant number or (2) an extension sign, in which case they always precede the
732 1
        // extension number. We assume a carrier code is more than 1 digit, so the first case has to
733 1
        // have more than 1 consecutive 'x' or 'X', whereas the second case can only have exactly 1 'x'
734 1
        // or 'X'. We ignore the character if it appears as the last character of the string.
735
        $candidateLength = mb_strlen($candidate);
736 1
737
        for ($index = 0; $index < $candidateLength - 1; $index++) {
738
            $charAtIndex = mb_substr($candidate, $index, 1);
739 1
            if ($charAtIndex == 'x' || $charAtIndex == 'X') {
740
                $charAtNextIndex = mb_substr($candidate, $index + 1, 1);
741
                if ($charAtNextIndex == 'x' || $charAtNextIndex == 'X') {
742
                    // This is the carrier code case, in which the 'X's always precede the national
743
                    // significant number.
744
                    $index++;
745
746
                    if ($util->isNumberMatch($number, mb_substr($candidate, $index)) != MatchType::NSN_MATCH) {
747
                        return false;
748 97
                    }
749
                } elseif (!PhoneNumberUtil::normalizeDigitsOnly(mb_substr($candidate,
750
                        $index)) == $number->getExtension()
751
                ) {
752
                    // This is the extension sign case, in which the 'x' or 'X' should always precede the
753
                    // extension number
754
                    return false;
755 97
                }
756
            }
757 97
        }
758 97
        return true;
759 97
    }
760 15
761 15
    /**
762
     * @param PhoneNumber $number
763
     * @param PhoneNumberUtil $util
764
     * @return bool
765
     */
766
    public static function isNationalPrefixPresentIfRequired(PhoneNumber $number, PhoneNumberUtil $util)
767
    {
768
        // First, check how we deduced the country code. If it was written in international format, then
769 15
        // the national prefix is not required.
770 15
        if ($number->getCountryCodeSource() !== CountryCodeSource::FROM_DEFAULT_COUNTRY) {
771 15
            return true;
772
        }
773
774
        $phoneNumberRegion = $util->getRegionCodeForCountryCode($number->getCountryCode());
775
        $metadata = $util->getMetadataForRegion($phoneNumberRegion);
776 15
        if ($metadata === null) {
777 97
            return true;
778 97
        }
779
780
        // Check if a national prefix should be present when formatting this number.
781
        $nationalNumber = $util->getNationalSignificantNumber($number);
782
        $formatRule = $util->chooseFormattingPatternForNumber($metadata->numberFormats(), $nationalNumber);
783
        // To do this, we check that a national prefix formatting rule was present and that it wasn't
784
        // just the first-group symbol ($1) with punctuation.
785
        if (($formatRule !== null) && mb_strlen($formatRule->getNationalPrefixFormattingRule()) > 0) {
786 97
            if ($formatRule->getNationalPrefixOptionalWhenFormatting()) {
787
                // The national-prefix is optional in these cases, so we don't need to check if it was
788
                // present.
789
                return true;
790 97
            }
791 39
792
            if (PhoneNumberUtil::formattingRuleHasFirstGroupOnly($formatRule->getNationalPrefixFormattingRule())) {
793
                // National Prefix not needed for this number.
794 65
                return true;
795 65
            }
796 65
797
            // Normalize the remainder.
798
            $rawInputCopy = PhoneNumberUtil::normalizeDigitsOnly($number->getRawInput());
799
            $rawInput = $rawInputCopy;
800
            // Check if we found a national prefix and/or carrier code at the start of the raw input, and
801 65
            // return the result.
802 65
            $carrierCode = null;
803
            return $util->maybeStripNationalPrefixAndCarrierCode($rawInput, $metadata, $carrierCode);
804
        }
805 65
        return true;
806 44
    }
807
808
809 7
    /**
810
     * Storage for Alternate Formats
811
     * @var PhoneMetadata[]
812 37
     */
813
    protected static $callingCodeToAlternateFormatsMap = array();
814 3
815
    /**
816
     * @param $countryCallingCode
817
     * @return PhoneMetadata|null
818 34
     */
819 34
    protected static function getAlternateFormatsForCountry($countryCallingCode)
820
    {
821
        $countryCodeSet = AlternateFormatsCountryCodeSet::$alternateFormatsCountryCodeSet;
822 34
823 34
        if (!in_array($countryCallingCode, $countryCodeSet)) {
824
            return null;
825 25
        }
826
827
        if (!isset(static::$callingCodeToAlternateFormatsMap[$countryCallingCode])) {
828
            static::loadAlternateFormatsMetadataFromFile($countryCallingCode);
829
        }
830
831
        return static::$callingCodeToAlternateFormatsMap[$countryCallingCode];
832
    }
833
834
    /**
835
     * @param string $countryCallingCode
836
     * @throws \Exception
837
     */
838
    protected static function loadAlternateFormatsMetadataFromFile($countryCallingCode)
839 13
    {
840
        $fileName = static::$alternateFormatsFilePrefix . '_' . $countryCallingCode . '.php';
841 13
842
        if (!is_readable($fileName)) {
843 13
            throw new \Exception('missing metadata: ' . $fileName);
844
        }
845
846
        $metadataLoader = new DefaultMetadataLoader();
847 13
        $data = $metadataLoader->loadMetadata($fileName);
848 2
        $metadata = new PhoneMetadata();
849 2
        $metadata->fromArray($data);
850
        static::$callingCodeToAlternateFormatsMap[$countryCallingCode] = $metadata;
851 13
    }
852
853
854
    /**
855
     * Return the current element
856
     * @link http://php.net/manual/en/iterator.current.php
857
     * @return PhoneNumberMatch|null
858 2
     */
859
    public function current()
860 2
    {
861
        return $this->lastMatch;
862 2
    }
863
864
    /**
865
     * Move forward to next element
866 2
     * @link http://php.net/manual/en/iterator.next.php
867 2
     * @return void Any returned value is ignored.
868 2
     */
869 2
    public function next()
870 2
    {
871 2
        $this->lastMatch = $this->find($this->searchIndex);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->lastMatch is correct as $this->find($this->searchIndex) targeting libphonenumber\PhoneNumberMatcher::find() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
872
873
        if ($this->lastMatch === null) {
874
            $this->state = 'DONE';
875
        } else {
876
            $this->searchIndex = $this->lastMatch->end();
0 ignored issues
show
Bug introduced by
The method end() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

876
            /** @scrutinizer ignore-call */ 
877
            $this->searchIndex = $this->lastMatch->end();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
877
            $this->state = 'READY';
878
        }
879 197
880
        $this->searchIndex++;
881 197
    }
882
883
    /**
884
     * Return the key of the current element
885
     * @link http://php.net/manual/en/iterator.key.php
886
     * @return mixed scalar on success, or null on failure.
887
     * @since 5.0.0
888
     */
889 199
    public function key()
890
    {
891 199
        return $this->searchIndex;
892
    }
893 199
894 93
    /**
895 93
     * Checks if current position is valid
896 126
     * @link http://php.net/manual/en/iterator.valid.php
897 126
     * @return boolean The return value will be casted to boolean and then evaluated.
898
     * Returns true on success or false on failure.
899
     * @since 5.0.0
900 199
     */
901 199
    public function valid()
902
    {
903
        return $this->state === 'READY';
904
    }
905
906
    /**
907
     * Rewind the Iterator to the first element
908
     * @link http://php.net/manual/en/iterator.rewind.php
909
     * @return void Any returned value is ignored.
910
     * @since 5.0.0
911
     */
912
    public function rewind()
913
    {
914
        $this->searchIndex = 0;
915
        $this->next();
916
    }
917
}
918