ImageRenderer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A render() 0 31 4
1
<?php
2
3
namespace Pageon\Lib\Markdown;
4
5
use InvalidArgumentException;
6
use League\CommonMark\ElementRendererInterface;
7
use League\CommonMark\HtmlElement;
8
use League\CommonMark\Inline\Element\AbstractInline;
9
use League\CommonMark\Inline\Element\Image;
10
use League\CommonMark\Inline\Element\Text;
11
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
12
use Pageon\Html\Image\ImageFactory;
13
use Stitcher\Exception\InvalidConfiguration;
14
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
15
16
class ImageRenderer implements InlineRendererInterface
17
{
18
    /** @var \Pageon\Html\Image\ImageFactory */
19
    private $imageFactory;
20
21
    public function __construct(ImageFactory $imageFactory)
22
    {
23
        $this->imageFactory = $imageFactory;
24
    }
25
26
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
27
    {
28
        if (! $inline instanceof Image) {
29
            throw new InvalidArgumentException('Inline must be instance of ' . Image::class);
30
        }
31
32
        $attributes = [];
33
34
        $src = $inline->getUrl();
35
36
        try {
37
            $responsiveImage = $this->imageFactory->create($src);
38
        } catch (FileNotFoundException $e) {
39
            throw InvalidConfiguration::fileNotFound($src);
40
        }
41
42
        $alt = $inline->firstChild();
43
44
        $attributes['src'] = $src;
45
        $attributes['srcset'] = $responsiveImage->srcset() ?? null;
46
        $attributes['sizes'] = $responsiveImage->sizes() ?? null;
47
        $attributes['alt'] = $alt instanceof Text
48
            ? $alt->getContent()
49
            : '';
50
51
        return new HtmlElement(
52
            'img',
53
            $attributes,
54
            $htmlRenderer->renderInlines($inline->children())
0 ignored issues
show
Documentation introduced by
$inline->children() is of type array<integer,object<Lea...\CommonMark\Node\Node>>, but the function expects a array<integer,object<Lea...lement\AbstractInline>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        );
56
    }
57
}
58