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

HtmlFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A format() 0 18 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
}