|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kadet\Highlighter\Formatter; |
|
6
|
|
|
|
|
7
|
|
|
use Kadet\Highlighter\Parser\Token\Token; |
|
8
|
|
|
use Kadet\Highlighter\Parser\Tokens; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractFormatter implements FormatterInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected $_options = []; |
|
13
|
|
|
protected $_line = 1; |
|
14
|
|
|
|
|
15
|
41 |
|
public function __construct(array $options = []) |
|
16
|
|
|
{ |
|
17
|
41 |
|
$this->_options = array_replace_recursive([ |
|
18
|
|
|
'lines' => [ |
|
19
|
41 |
|
'enable' => false, |
|
20
|
|
|
'start' => 1, |
|
21
|
|
|
'marked' => [] |
|
22
|
|
|
] |
|
23
|
|
|
], $options); |
|
24
|
41 |
|
} |
|
25
|
|
|
|
|
26
|
5 |
|
public function format(Tokens $tokens) |
|
27
|
|
|
{ |
|
28
|
5 |
|
$source = $tokens->getSource(); |
|
29
|
|
|
|
|
30
|
5 |
|
$this->_line = (int)$this->_options['lines']['start']; |
|
31
|
5 |
|
$this->_options['lines']['max'] = substr_count($source, '\n') + $this->_options['lines']['start']; |
|
32
|
|
|
|
|
33
|
5 |
|
$result = $this->_options['lines']['enable'] ? $this->formatLineStart($this->_line) : ''; |
|
|
|
|
|
|
34
|
5 |
|
$last = 0; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Token $token */ |
|
37
|
5 |
|
foreach ($tokens as $token) { |
|
38
|
5 |
|
$result .= $this->_content(substr($source, $last, $token->pos - $last)); |
|
39
|
5 |
|
$result .= $this->token($token); |
|
40
|
|
|
|
|
41
|
5 |
|
$last = $token->pos; |
|
42
|
|
|
} |
|
43
|
5 |
|
$result .= $this->_content(substr($source, $last)); |
|
44
|
|
|
|
|
45
|
5 |
|
return $result . ($this->_options['lines']['enable'] ? $this->formatLineEnd($this->_line++) : ''); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
5 |
|
private function _content($text) |
|
49
|
|
|
{ |
|
50
|
5 |
|
$content = $this->content($text); |
|
51
|
|
|
|
|
52
|
5 |
|
return $this->_options['lines']['enable'] ? preg_replace_callback('/\R/u', function ($feed) { |
|
53
|
|
|
return $this->formatLineEnd($this->_line++) . $feed[0] . $this->formatLineStart($this->_line); |
|
|
|
|
|
|
54
|
5 |
|
}, $content) : $content; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
protected function content($text) |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $text; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function formatLineStart($line) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function formatLineEnd($line) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected abstract function token(Token $token); |
|
73
|
|
|
} |
|
74
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.