Passed
Push — markdown-responsive ( e9a50f...82187b )
by Arnaud
03:31
created

Parsedown::inlineImage()   C

Complexity

Conditions 13
Paths 41

Size

Total Lines 60
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 37
nc 41
nop 1
dl 0
loc 60
rs 6.6166
c 2
b 0
f 0

How to fix   Long Method    Complexity   

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
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Converter;
12
13
use Cecil\Assets\Asset;
14
use Cecil\Builder;
15
16
class Parsedown extends \ParsedownToC
17
{
18
    /** @var Builder */
19
    protected $builder;
20
21
    /** {@inheritdoc} */
22
    protected $regexAttribute = '(?:[#.][-\w:\\\]+[ ]*|[-\w:\\\]+(?:=(?:["\'][^\n]*?["\']|[^\s]+)?)?[ ]*)';
23
24
    public function __construct(Builder $builder)
25
    {
26
        $this->builder = $builder;
27
        parent::__construct(['selectors' => $this->builder->getConfig()->get('body.toc')]);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function inlineImage($excerpt)
34
    {
35
        $image = parent::inlineImage($excerpt);
36
        if (!isset($image)) {
37
            return null;
38
        }
39
        $image['element']['attributes']['src'] = $imageSource = trim($this->removeQuery($image['element']['attributes']['src']));
40
        $asset = new Asset($this->builder, $image['element']['attributes']['src']);
41
        $width = $asset->getWidth();
42
        /**
43
         * Should be lazy loaded?
44
         */
45
        if ($this->builder->getConfig()->get('content.images.lazy.enabled')) {
46
            $image['element']['attributes']['loading'] = 'lazy';
47
        }
48
        /**
49
         * Should be resized?
50
         */
51
        if ($image['element']['attributes']['width'] !== null
52
            && (int) $image['element']['attributes']['width'] < $width
53
            && $this->builder->getConfig()->get('content.images.resize.enabled')
54
        ) {
55
            $width = (int) $image['element']['attributes']['width'];
56
            $imageResized = $asset->resize($width);
57
            $image['element']['attributes']['src'] = $imageResized;
58
        }
59
        if ($image['element']['attributes']['width'] === null) {
60
            $image['element']['attributes']['width'] = $width;
61
        }
62
        /**
63
         * Should be responsive?
64
         */
65
        if ($this->builder->getConfig()->get('content.images.responsive.enabled')) {
66
            $steps = $this->builder->getConfig()->get('content.images.responsive.width.steps');
67
            $wMin = $this->builder->getConfig()->get('content.images.responsive.width.min');
68
            $wMax = $this->builder->getConfig()->get('content.images.responsive.width.max');
69
            $srcset = '';
70
            for ($i = 1; $i <= $steps; $i++) {
71
                $w = ceil($wMin * $i);
72
                if ($w > $width || $w > $wMax) {
73
                    break;
74
                }
75
                $a = new Asset($this->builder, $imageSource);
76
                $img = $a->resize($w);
0 ignored issues
show
Bug introduced by
$w of type double is incompatible with the type integer expected by parameter $size of Cecil\Assets\Asset::resize(). ( Ignorable by Annotation )

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

76
                $img = $a->resize(/** @scrutinizer ignore-type */ $w);
Loading history...
77
                $srcset .= sprintf('%s %sw', $img, $w);
78
                if ($i < $steps) {
79
                    $srcset .= ', ';
80
                }
81
            }
82
            if ($imageResized) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imageResized does not seem to be defined for all execution paths leading up to this point.
Loading history...
83
                $srcset .= sprintf(',%s %sw', $imageResized, $width);
0 ignored issues
show
Bug introduced by
It seems like $width can also be of type false; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

83
                $srcset .= sprintf(',%s %sw', $imageResized, /** @scrutinizer ignore-type */ $width);
Loading history...
84
            } else {
85
                $srcset .= sprintf(',%s %sw', $imageSource, $width);
86
            }
87
            // ie: srcset="/img-480.jpg 480w, /img-800.jpg 800w"
88
            $image['element']['attributes']['srcset'] = $srcset;
89
            $image['element']['attributes']['sizes'] = $this->builder->getConfig()->get('content.images.responsive.sizes.default');
90
        }
91
92
        return $image;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function parseAttributeData($attributeString)
99
    {
100
        $Data = [];
101
102
        $attributes = preg_split('/[ ]+/', $attributeString, -1, PREG_SPLIT_NO_EMPTY);
103
104
        $HtmlAtt = [];
105
106
        foreach ($attributes as $attribute) {
107
            if ($attribute[0] === '#') {
108
                $Data['id'] = substr($attribute, 1);
109
            } elseif ($attribute[0] === '.') {
110
                $classes[] = substr($attribute, 1);
111
            } else {
112
                parse_str($attribute, $parsed);
113
                $HtmlAtt = array_merge($HtmlAtt, $parsed);
114
            }
115
        }
116
117
        if (isset($classes)) {
118
            $Data['class'] = implode(' ', $classes);
119
        }
120
        if (isset($HtmlAtt)) {
121
            foreach ($HtmlAtt as $a => $v) {
122
                $Data[$a] = trim($v, '"');
123
            }
124
        }
125
126
        return $Data;
127
    }
128
129
    /**
130
     * Removes query string from URL.
131
     */
132
    private function removeQuery(string $path): string
133
    {
134
        return strtok($path, '?');
135
    }
136
}
137