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

SubStringMatcher::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
crap 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
}