Passed
Branch fuck-54 (18c095)
by Kacper
03:37
created

RegexMatcher::match()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 7
Ratio 41.18 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 9
Bugs 1 Features 0
Metric Value
c 9
b 1
f 0
dl 7
loc 17
ccs 11
cts 12
cp 0.9167
rs 9.2
cc 4
eloc 9
nc 4
nop 2
crap 4.0092
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Matcher;
17
18
use Kadet\Highlighter\Parser\TokenFactoryInterface;
19
20
class RegexMatcher implements MatcherInterface
21
{
22
    private $regex;
23
    private $groups;
24
25
    /**
26
     * RegexMatcher constructor.
27
     *
28
     * @param            $regex
29
     * @param array|null $groups
30
     */
31 10
    public function __construct($regex, array $groups = [1 => null])
32
    {
33 10
        $this->regex = $regex;
34 10
        $this->groups = $groups;
35 10
    }
36
37
    /**
38
     * Matches all occurrences and returns token list
39
     *
40
     * @param string                $source Source to match tokens
41
     *
42
     * @param TokenFactoryInterface $factory
43
     *
44
     * @return array
45
     */
46 10
    public function match($source, TokenFactoryInterface $factory)
47
    {
48 10
        preg_match_all($this->regex, $source, $matches, PREG_OFFSET_CAPTURE);
49 10
        $matches = array_intersect_key($matches, $this->groups);
50
51 10
        foreach ($matches as $id => $group) {
52 10
            $name = $this->groups[$id];
53
54 10 View Code Duplication
            foreach ($group as $match) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55 10
                if ($match[1] === -1) {
56
                    continue;
57
                }
58
59 10
                yield $factory->create([$name, 'pos' => $match[1], 'length' => strlen($match[0])]);
60 10
            }
61 10
        }
62
    }
63
}