Passed
Push — master ( 1d0ea2...0f9d68 )
by Kacper
05:34
created

AbstractFormatter::_content()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
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) : '';
0 ignored issues
show
Bug introduced by
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 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++) : '');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->formatLineEnd(PostIncNode) targeting Kadet\Highlighter\Format...matter::formatLineEnd() 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...
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);
0 ignored issues
show
Bug introduced by
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...
Bug introduced by
Are you sure the usage of $this->formatLineEnd(PostIncNode) targeting Kadet\Highlighter\Format...matter::formatLineEnd() 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...
54 5
        }, $content) : $content;
55
    }
56
57 1
    protected function content($text)
58
    {
59 1
        return $text;
60
    }
61
62
    protected function formatLineStart($line)
0 ignored issues
show
Unused Code introduced by
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
Unused Code introduced by
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