Passed
Push — master ( 1564fa...d2ad69 )
by Kacper
03:00 queued 15s
created

SubStringMatcher   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 3
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 43
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A match() 0 18 2
1
<?php
2
/**
3
 * Highlighter
4
 *1
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
19
use Kadet\Highlighter\Parser\Token;
20
use Kadet\Highlighter\Parser\TokenFactoryInterface;
21
22
class SubStringMatcher implements MatcherInterface
23
{
24
    private $_substr;
25
26
    /**
27
     * SubstrMatcher constructor.
28
     *
29
     * @param $substr
30
     */
31 1
    public function __construct($substr)
32
    {
33 1
        $this->_substr = $substr;
34 1
    }
35
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 1
    public function match($source, TokenFactoryInterface $factory)
47
    {
48 1
        $results = [];
49 1
        $pos = 0;
50
51 1
        $len = strlen($this->_substr);
52
53 1
        while (($pos = strpos($source, $this->_substr, $pos)) !== false) {
54 1
            $token = $factory->create(['pos' => $pos, 'length' => $len]);
55 1
            $end = $token->getEnd();
56
57 1
            $results[spl_object_hash($token)] = $token;
58 1
            $results[spl_object_hash($end)]   = $end;
59 1
            $pos += $len;
60 1
        }
61
62 1
        return $results;
63
    }
64
}