Passed
Pull Request — master (#26)
by Maciej
03:01
created

Formatter/AbstractFormatter.php (3 issues)

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 41
            'lines' => [
19
                'enable' => false,
20
                'start'  => 1,
21
                'marked' => []
22
            ]
23 41
        ], $options);
24 41
    }
25
26 6
    public function format(Tokens $tokens)
27
    {
28 6
        $source = $tokens->getSource();
29
30 6
        $this->_line = (int)$this->_options['lines']['start'];
31 6
        $this->_options['lines']['max'] = substr_count($source, '\n') + $this->_options['lines']['start'];
32
33 6
        $result = $this->_options['lines']['enable'] ? $this->formatLineStart($this->_line) : '';
0 ignored issues
show
Are you sure the usage of $this->formatLineStart($this->_line) targeting Kadet\Highlighter\Format...tter::formatLineStart() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
34 6
        $last   = 0;
35
36
        /** @var Token $token */
37 6
        foreach ($tokens as $token) {
38 6
            $result .= $this->_content(substr($source, $last, $token->pos - $last));
39 6
            $result .= $this->token($token);
40
41 6
            $last = $token->pos;
42
        }
43 6
        $result .= $this->_content(substr($source, $last));
44
45 6
        return $result . ($this->_options['lines']['enable'] ? $this->formatLineEnd($this->_line++) : '');
46
    }
47
48 6
    private function _content($text)
49
    {
50 6
        $content = $this->content($text);
51
52
        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 6
        }, $content) : $content;
55
    }
56
57 2
    protected function content($text)
58
    {
59 2
        return $text;
60
    }
61
62
    protected function formatLineStart($line)
0 ignored issues
show
The parameter $line is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

62
    protected function formatLineStart(/** @scrutinizer ignore-unused */ $line)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        return null;
65
    }
66
67
    protected function formatLineEnd($line)
0 ignored issues
show
The parameter $line is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

67
    protected function formatLineEnd(/** @scrutinizer ignore-unused */ $line)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
    {
69
        return null;
70
    }
71
72
    protected abstract function token(Token $token);
73
}
74