Completed
Push — master ( 098b56...4e0e81 )
by Stéphane
02:32
created

LinkRenderer::render()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 53
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 26
nc 9
nop 2
dl 0
loc 53
ccs 0
cts 35
cp 0
crap 56
rs 8.5706
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Todaymade\Daux\Format\HTMLFile\ContentTypes\Markdown;
2
3
use League\CommonMark\ElementRendererInterface;
4
use League\CommonMark\HtmlElement;
5
use League\CommonMark\Inline\Element\AbstractInline;
6
use League\CommonMark\Inline\Element\Link;
7
use Todaymade\Daux\Config;
8
use Todaymade\Daux\DauxHelper;
9
use Todaymade\Daux\Exception\LinkNotFoundException;
10
use Todaymade\Daux\Tree\Entry;
11
12
class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
13
{
14
    /**
15
     * @param AbstractInline|Link $inline
16
     * @param ElementRendererInterface $htmlRenderer
17
     * @return HtmlElement
18
     * @throws LinkNotFoundException
19
     */
20
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
21
    {
22
        // This can't be in the method type as
23
        // the method is an abstract and should
24
        // have the same interface
25
        if (!$inline instanceof Link) {
26
            throw new \RuntimeException(
27
                'Wrong type passed to ' . __CLASS__ . '::' . __METHOD__ .
28
                " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" .
29
                get_class($inline) . "' was provided"
30
            );
31
        }
32
33
        $element = parent::render($inline, $htmlRenderer);
34
35
        $url = $inline->getUrl();
36
37
        // empty urls and anchors should
38
        // not go through the url resolver
39
        if (!$this->isValidUrl($url)) {
40
            return $element;
41
        }
42
43
        // Absolute urls, shouldn't either
44
        if ($this->isExternalUrl($url)) {
45
            $element->setAttribute('class', 'Link--external');
46
47
            return $element;
48
        }
49
50
        // if there's a hash component in the url, we can directly use it as all pages are in the same file
51
        $urlAndHash = explode('#', $url);
52
        if (isset($urlAndHash[1])) {
53
            $element->setAttribute('href', '#' . $urlAndHash[1]);
54
            
55
            return $element;
56
        }
57
58
        try {
59
            $file = $this->resolveInternalFile($url);
60
            $url = $file->getUrl();
61
        } catch (LinkNotFoundException $e) {
62
            if ($this->daux->isStatic()) {
63
                throw $e;
64
            }
65
66
            $element->setAttribute('class', 'Link--broken');
67
        }
68
69
        $url = str_replace('/', '_', $url);
70
        $element->setAttribute('href', "#file_$url");
71
72
        return $element;
73
    }
74
}