Test Failed
Push — feature-laravel-5.4 ( 2b4b90...5bfdc4 )
by Kirill
03:52
created

Parser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 16 1
A createAnchorName() 0 4 1
A render() 0 6 1
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\Anchors;
11
12
use Illuminate\Support\Str;
13
14
/**
15
 * Class Parser.
16
 */
17
class Parser
18
{
19
    /**
20
     * @param string $body
21
     * @return ProcessedBody
22
     */
23
    public function parse(string $body): ProcessedBody
24
    {
25
        $links = [];
26
27
        $body = preg_replace_callback('/<(h\d+)>(.*?)<\/h\d+>/isu', function ($data) use (&$links) {
28
            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...
29
30
            $id = $this->createAnchorName($header);
31
32
            $links[] = new AnchorLink($id, $header, $tag);
33
34
            return $this->render($tag, $id, $header);
35
        }, $body);
36
37
        return new ProcessedBody($body, $links);
38
    }
39
40
    /**
41
     * @param string $title
42
     * @return string
43
     */
44
    private function createAnchorName(string $title): string
45
    {
46
        return Str::slug($title);
47
    }
48
49
    /**
50
     * @param string $tag
51
     * @param string $id
52
     * @param string $header
53
     * @return string
54
     */
55
    private function render(string $tag, string $id, string $header): string
56
    {
57
        $link = '<%s><a href="#%s" class="anchor" name="%s"></a>%s</%s>';
58
59
        return sprintf($link, $tag, $id, $id, $header, $tag);
60
    }
61
}
62