Passed
Pull Request — master (#102)
by Dmitriy
12:15
created

CombinedRegexp::compilePatterns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 9
c 2
b 1
f 0
nc 2
nop 1
dl 0
loc 13
ccs 10
cts 10
cp 1
crap 2
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Strings;
6
7
final class CombinedRegexp
8
{
9
    private string $freeQuoter;
10
    private string $quoteReplacer;
11
    private string $compiledPattern;
12
13 3
    public function __construct(
14
        private array $patterns,
15
        private string $regexpQuoter = '/'
16
    ) {
17 3
        $this->freeQuoter = $this->regexpQuoter === '/' ? '~' : '/';
18 3
        $this->quoteReplacer = preg_quote($this->regexpQuoter, $this->regexpQuoter);
19 3
        $this->compiledPattern = $this->compilePatterns($this->patterns);
20
    }
21
22 3
    public function getCompiledPattern(): string
23
    {
24 3
        return $this->compiledPattern;
25
    }
26
27
    public function matchAny(string $string, string $flags = 'i'): bool
28
    {
29
        $pattern = $this->compiledPattern . $flags;
30
31
        return preg_match($pattern, $string) === 1;
32
    }
33
34 3
    public function matchPattern(string $string)
35
    {
36 3
        return $this->patterns[$this->matchPatternPosition($string)];
37
    }
38
39 3
    public function matchPatternPosition(string $string): int
40
    {
41 3
        $match = preg_match($this->compiledPattern, $string, $matches);
42 3
        if ($match !== 1) {
43
            throw new \Exception(
44
                sprintf(
45
                    'Failed to match pattern "%s" with string "%s".',
46
                    $this->compiledPattern,
47
                    $string,
48
                )
49
            );
50
        }
51
52 3
        return count($matches) - 1;
53
    }
54
55 3
    private function compilePatterns(array $patterns): string
56
    {
57 3
        $quotedPatterns = [];
58 3
        for ($i = 0; $i < count($patterns); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
59 3
            $quotedPatterns[] = preg_replace(
60 3
                $this->freeQuoter . $this->regexpQuoter . $this->freeQuoter,
61 3
                $this->quoteReplacer,
62 3
                $patterns[$i]
63 3
            ) . str_repeat('()', $i);
64
        }
65 3
        $combinedRegexps = '(?|' . implode('|', $quotedPatterns) . ')';
66
67 3
        return $this->freeQuoter . $combinedRegexps . $this->freeQuoter;
68
    }
69
}
70