Test Failed
Push — feature-laravel-5.4 ( dbda8c...02b5b6 )
by Kirill
03:30
created

ContentHeadersRenderer::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace App\Services\ContentRenderer;
11
12
use Illuminate\Support\Str;
13
14
/**
15
 * Class ContentHeadersRenderer.
16
 */
17
class ContentHeadersRenderer
18
{
19
    /**
20
     * @var string
21
     */
22
    private $body;
23
24
    /**
25
     * @var array
26
     */
27
    private $links = [];
28
29
    /**
30
     * @var string
31
     */
32
    private $content = '';
33
34
    /**
35
     * @var array
36
     */
37
    private $tags = [
38
        'h6' => 'h6',
39
        'h5' => 'h6',
40
        'h4' => 'h5',
41
        'h3' => 'h4',
42
        'h2' => 'h3',
43
        'h1' => 'h2',
44
    ];
45
46
    /**
47
     * HeadersPrerender constructor.
48
     *
49
     * @param string $body
50
     */
51
    public function __construct(string $body)
52
    {
53
        $this->body = $body;
54
    }
55
56
    /**
57
     * @return ContentHeadersRenderer
58
     */
59
    public function parse(): ContentHeadersRenderer
60
    {
61
        $this->links = [];
62
63
        $this->content = preg_replace_callback('/<(h\d+)>(.*?)<\/h\d+>/isu', function ($data) {
64
            list($data, $tag, $header) = $data;
0 ignored issues
show
Unused Code introduced by
The assignment to $data is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
65
            $id = Str::slug($header);
66
67
            foreach ($this->tags as $from => $to) {
68
                $tag = str_replace($from, $to, $tag);
69
            }
70
71
            $this->links[] = [
72
                'anchor' => $id,
73
                'title'  => $header,
74
                'level'  => $tag,
75
            ];
76
77
            $link = '<%s><a href="#%s" class="anchor" name="%s"></a>%s</%s>';
78
79
            return sprintf($link, $tag, $id, $id, $header, $tag);
80
        }, $this->body);
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getLinks(): array
89
    {
90
        return $this->links;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getContent(): string
97
    {
98
        return $this->content;
99
    }
100
}
101