Passed
Push — master ( d2ad69...686350 )
by Kacper
02:44
created

HtmlFormatter::format()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
crap 3
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\Formatter;
17
18
use Kadet\Highlighter\Parser\Token;
19
use Kadet\Highlighter\Parser\TokenIterator;
20
21
/**
22
 * Class HtmlFormatter
23
 *
24
 * @package Kadet\Highlighter\Formatter
25
 */
26
class HtmlFormatter implements FormatterInterface
27
{
28
29 1
    public function format(TokenIterator $tokens)
30
    {
31 1
        $source = $tokens->getSource();
32
33 1
        $result = '';
34 1
        $last = 0;
35
36
        /** @var Token $token */
37 1
        foreach ($tokens as $token) {
38 1
            $result .= htmlspecialchars(substr($source, $last, $token->pos - $last));
39 1
            $result .= $token->isStart() ? '<span class="' . str_replace('.', ' ', $token->name) . '">' : '</span>';
40
41 1
            $last = $token->pos;
42 1
        }
43 1
        $result .= substr($source, $last);
44
45 1
        return $result;
46
    }
47
}