Passed
Push — master ( 55204c...64475d )
by Alexander
23:33 queued 21:19
created

AbstractCombinedRegexp   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 48
ccs 6
cts 6
cp 1
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A throwFailedMatchException() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Strings;
6
7
use Exception;
8
9
/**
10
 * `CombinedRegexp` optimizes matching of multiple regular expressions.
11
 * Read more about the concept in
12
 * {@see https://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html}.
13
 */
14
abstract class AbstractCombinedRegexp
15
{
16
    public const REGEXP_DELIMITER = '/';
17
    public const QUOTE_REPLACER = '\\/';
18
19
    /**
20
     * @return string[] Regular expressions to combine.
21
     */
22
    abstract public function getPatterns(): array;
23
24
    /**
25
     * @return string Flags to apply to all regular expressions.
26
     */
27
    abstract public function getFlags(): string;
28
29
    /**
30
     * @return string The compiled pattern.
31
     */
32
    abstract public function getCompiledPattern(): string;
33
34
    /**
35
     * Returns `true` whether the given string matches any of the patterns, `false` - otherwise.
36
     */
37
    abstract public function matches(string $string): bool;
38
39
    /**
40
     * Returns pattern that matches the given string.
41
     * @throws Exception if the string does not match any of the patterns.
42
     */
43
    abstract public function getMatchingPattern(string $string): string;
44
45
    /**
46
     * Returns position of the pattern that matches the given string.
47
     * @throws Exception if the string does not match any of the patterns.
48
     */
49
    abstract public function getMatchingPatternPosition(string $string): int;
50
51
    /**
52
     * @throws Exception
53
     * @return never-return
0 ignored issues
show
Documentation Bug introduced by
The doc comment never-return at position 0 could not be parsed: Unknown type name 'never-return' at position 0 in never-return.
Loading history...
54
     */
55 3
    protected function throwFailedMatchException(string $string): void
56
    {
57 3
        throw new Exception(
58 3
            sprintf(
59 3
                'Failed to match pattern "%s" with string "%s".',
60 3
                $this->getCompiledPattern(),
61 3
                $string,
62 3
            )
63 3
        );
64
    }
65
}
66