Passed
Branch fuck-54 (abba45)
by Kacper
02:46
created

RegexMatcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 15.91 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 1 Features 0
Metric Value
wmc 6
c 10
b 1
f 0
lcom 1
cbo 1
dl 7
loc 44
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B match() 7 17 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 11
    public function __construct($regex, array $groups = [1 => null])
32
    {
33 11
        $this->regex = $regex;
34 11
        $this->groups = $groups;
35 11
    }
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 \Generator
45
     */
46 11
    public function match($source, TokenFactoryInterface $factory)
47
    {
48 11
        preg_match_all($this->regex, $source, $matches, PREG_OFFSET_CAPTURE);
49 11
        $matches = array_intersect_key($matches, $this->groups);
50
51 11
        foreach ($matches as $id => $group) {
52 11
            $name = $this->groups[$id];
53
54 11 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 11
                if (empty($match) || $match[1] === -1) {
56 1
                    continue;
57
                }
58
59 11
                yield $factory->create([$name, 'pos' => $match[1], 'length' => strlen($match[0])]);
60 11
            }
61 11
        }
62
    }
63
}