Completed
Pull Request — master (#9)
by Tomáš
04:03
created

MarkdownConverter::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Bundle\MarkdownBundle;
13
14
use Symplify\PHP7_Sculpin\Core\Converter\SourceConverterContext;
15
use Symplify\PHP7_Sculpin\Core\Converter\ConverterInterface;
16
use Symplify\PHP7_Sculpin\Core\Converter\ParserInterface;
17
18
final class MarkdownConverter implements ConverterInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    const NAME = 'markdown';
24
25
    /**
26
     * @var ParserInterface
27
     */
28
    private $markdown;
29
30
    public function __construct(ParserInterface $markdown)
31
    {
32
        $this->markdown = $markdown;
33
        $this->markdown->header_id_func = [$this, 'generateHeaderId'];
0 ignored issues
show
Bug introduced by
Accessing header_id_func on the interface Symplify\PHP7_Sculpin\Co...nverter\ParserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
34
    }
35
36
    public function getName() : string
37
    {
38
        return self::NAME;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function convert(SourceConverterContext $converterContext)
45
    {
46
        $converterContext->setContent(
47
            $this->markdown->transform(
48
                $converterContext->content()
49
            )
50
        );
51
    }
52
53
    /**
54
     * This method is called to generate an id="" attribute for a header.
55
     */
56
    public function generateHeaderId(string $headerText) : string
57
    {
58
        // $headerText is completely raw markdown input. We need to strip it
59
        // from all markup, because we are only interested in the actual 'text'
60
        // part of it.
61
62
        // Step 1: Remove html tags.
63
        $result = strip_tags($headerText);
64
65
        // Step 2: Remove all markdown links. To do this, we simply remove
66
        // everything between ( and ) if the ( occurs right after a ].
67
        $result = preg_replace('%
68
            (?<= \\]) # Look behind to find ]
69
            (
70
                \\(     # match (
71
                [^\\)]* # match everything except )
72
                \\)     # match )
73
            )
74
75
            %x', '', $result);
76
77
        // Step 3: Convert spaces to dashes, and remove unwanted special
78
        // characters.
79
        $map = [
80
            ' ' => '-',
81
            '(' => '',
82
            ')' => '',
83
            '[' => '',
84
            ']' => '',
85
        ];
86
87
        return rawurlencode(strtolower(
88
            strtr($result, $map)
89
        ));
90
    }
91
}
92