Completed
Push — master ( c4e3cd...c5cae6 )
by Anton
05:38
created

Highlighter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 102
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A setStyle() 0 6 1
A highlight() 0 17 3
B lines() 0 25 5
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Tokenizer;
9
10
use Spiral\Core\Component;
11
use Spiral\Tokenizer\Highlighter\Style;
12
use Spiral\Tokenizer\Traits\TokensTrait;
13
14
/**
15
 * Highlights php file using specified style.
16
 */
17
class Highlighter extends Component
18
{
19
    use TokensTrait;
20
21
    /**
22
     * @invisible
23
     * @var Style
24
     */
25
    private $style = null;
26
27
    /**
28
     * @invisible
29
     * @var array
30
     */
31
    private $tokens = [];
32
33
    /**
34
     * Highlighted source.
35
     *
36
     * @var string
37
     */
38
    private $highlighted = '';
39
40
    /**
41
     * @param string     $source
42
     * @param Style|null $style
43
     */
44
    public function __construct($source, Style $style = null)
45
    {
46
        $this->style = !empty($style) ? $style : new Style();
47
        $this->tokens = $this->normalizeTokens(token_get_all($source));
48
    }
49
50
    /**
51
     * Set highlighter styler.
52
     *
53
     * @param Style $style
54
     * @return $this
55
     */
56
    public function setStyle(Style $style)
57
    {
58
        $this->style = $style;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Get highlighted source.
65
     *
66
     * @return string
67
     */
68
    public function highlight()
69
    {
70
        if (!empty($this->highlighted)) {
71
            //Nothing to do
72
            return $this->highlighted;
73
        }
74
75
        $this->highlighted = '';
76
        foreach ($this->tokens as $tokenID => $token) {
77
            $this->highlighted .= $this->style->highlightToken(
78
                $token[TokenizerInterface::TYPE],
79
                htmlentities($token[TokenizerInterface::CODE])
80
            );
81
        }
82
83
        return $this->highlighted;
84
    }
85
86
    /**
87
     * Get only part of php file around specified line.
88
     *
89
     * @param int|null $line   Set as null to avoid line highlighting.
90
     * @param int|null $around Set as null to return every line.
91
     * @return string
92
     */
93
    public function lines($line = null, $around = null)
94
    {
95
        //Chinking by lines
96
        $lines = explode("\n", str_replace("\r\n", "\n", $this->highlight()));
97
98
        $result = "";
99
        foreach ($lines as $number => $code) {
100
            $human = $number + 1;
101
            if (
102
                !empty($around)
103
                && ($human <= $line - $around || $human >= $line + $around)
104
            ) {
105
                //Not included in a range
106
                continue;
107
            }
108
109
            $result .= $this->style->line(
110
                $human,
111
                mb_convert_encoding($code, 'utf-8'),
112
                $human === $line
113
            );
114
        }
115
116
        return $result;
117
    }
118
}