Passed
Push — master ( 7acf4e...727c5c )
by Stéphane
02:27
created

ImageRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 1
f 0
1
<?php namespace Todaymade\Daux\Format\HTML\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\Image;
7
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
8
use League\CommonMark\Util\ConfigurationAwareInterface;
9
use League\CommonMark\Util\ConfigurationInterface;
10
use Todaymade\Daux\Config;
11
use Todaymade\Daux\DauxHelper;
12
13
class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterface
14
{
15
    /**
16
     * @var Config
17
     */
18
    protected $daux;
19
20
    /**
21
     * @var ConfigurationInterface
22
     */
23
    protected $config;
24
25 6
    public function __construct($daux)
26
    {
27 6
        $this->daux = $daux;
28 6
        $this->parent = new \League\CommonMark\Inline\Renderer\ImageRenderer();
0 ignored issues
show
Bug Best Practice introduced by Stéphane Goetz
The property parent does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29 6
    }
30
31
    /**
32
     * Relative URLs can be done using either the folder with 
33
     * number prefix or the final name (with prefix stripped).
34
     * This ensures that we always use the final name when generating.
35
     */
36
    protected function getCleanUrl(Image $element)
37
    {
38
        $url = $element->getUrl();
39
40
        // empty urls and anchors should
41
        // not go through the url resolver
42
        if (!DauxHelper::isValidUrl($url)) {
43
            return $url;
44
        }
45
46
        // Absolute urls, shouldn't either
47
        if (DauxHelper::isExternalUrl($url)) {
48
            return $url;
49
        }
50
51
        try {
52
            $file = DauxHelper::resolveInternalFile($this->daux, $url);
53
            return DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
54
        } catch (LinkNotFoundException $e) {
0 ignored issues
show
Bug introduced by Stéphane Goetz
The type Todaymade\Daux\Format\HT...n\LinkNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
            if ($this->daux->isStatic()) {
56
                throw $e;
57
            }
58
59
            $element->setAttribute('class', 'Link--broken');
0 ignored issues
show
Bug introduced by Stéphane Goetz
The method setAttribute() does not exist on League\CommonMark\Inline\Element\Image. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
            $element->/** @scrutinizer ignore-call */ 
60
                      setAttribute('class', 'Link--broken');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
        }
61
62
        return $url;
63
    }
64
65
    /**
66
     * @param Image                    $inline
67
     * @param ElementRendererInterface $htmlRenderer
68
     *
69
     * @return HtmlElement
70
     */
71
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
72
    {
73
        $original = $inline->getUrl();
0 ignored issues
show
Unused Code introduced by Stéphane Goetz
The assignment to $original is dead and can be removed.
Loading history...
Bug introduced by Stéphane Goetz
The method getUrl() does not exist on League\CommonMark\Inline\Element\AbstractInline. It seems like you code against a sub-type of League\CommonMark\Inline\Element\AbstractInline such as League\CommonMark\Inline...ent\AbstractWebResource. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        /** @scrutinizer ignore-call */ 
74
        $original = $inline->getUrl();
Loading history...
74
        $inline->setUrl($this->getCleanUrl($inline));
0 ignored issues
show
Bug introduced by Stéphane Goetz
The method setUrl() does not exist on League\CommonMark\Inline\Element\AbstractInline. It seems like you code against a sub-type of League\CommonMark\Inline\Element\AbstractInline such as League\CommonMark\Inline...ent\AbstractWebResource. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        $inline->/** @scrutinizer ignore-call */ 
75
                 setUrl($this->getCleanUrl($inline));
Loading history...
75
76
        return $this->parent->render($inline, $htmlRenderer);
77
    }
78
79
    /**
80
     * @param ConfigurationInterface $configuration
81
     */
82 6
    public function setConfiguration(ConfigurationInterface $configuration)
83
    {
84 6
        $this->config = $configuration;
85 6
        $this->parent->setConfiguration($configuration);
86 6
    }
87
}
88