Passed
Push — images ( 06004f...067171 )
by Arnaud
02:46
created

Parsedown::inlineImage()   C

Complexity

Conditions 14
Paths 113

Size

Total Lines 99
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
cc 14
eloc 54
c 7
b 0
f 0
nc 113
nop 1
dl 0
loc 99
rs 6.1583

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\Image;
14
use Cecil\Builder;
15
use Cecil\Util;
16
use ParsedownExtra;
17
18
class Parsedown extends ParsedownExtra
19
{
20
    /** @var Builder */
21
    private $builder;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function __construct(Builder $builder)
27
    {
28
        parent::__construct();
29
        $this->builder = $builder;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function inlineImage($excerpt)
36
    {
37
        $image = parent::inlineImage($excerpt);
38
39
        if (!isset($image)) {
40
            return null;
41
        }
42
43
        $path = Util::joinFile(
44
            $this->builder->getConfig()->getStaticTargetPath(),
45
            ltrim($this->removeQuery($image['element']['attributes']['src']))
46
        );
47
        if (Util::isExternalUrl($image['element']['attributes']['src'])) {
48
            $path = $this->removeQuery($image['element']['attributes']['src']);
49
        }
50
        $size = getimagesize($path);
51
        $width = $size[0];
52
        $type = $size[2];
53
54
        // sets default attributes
55
        $image['element']['attributes']['width'] = $width;
56
        if ($type !== null) {
57
            $image['element']['attributes']['loading'] = 'lazy';
58
        }
59
60
        // captures query string.
61
        // ie: "?resize=300&responsive"
62
        $query = parse_url($image['element']['attributes']['src'], PHP_URL_QUERY);
63
        if ($query === null) {
64
            return $image;
65
        }
66
        parse_str($query, $result);
67
        // cleans URL
68
        $image['element']['attributes']['src'] = $this->removeQuery($image['element']['attributes']['src']);
69
70
        /**
71
         * Should be responsive?
72
         */
73
        $responsive = false;
74
        if (array_key_exists('responsive', $result) && !Util::isExternalUrl($image['element']['attributes']['src'])) {
75
            $responsive = true;
76
            // process
77
            $steps = 5;
78
            $wMin = 320;
79
            $wMax = 2560;
80
            if ($width < $wMax) {
81
                $wMax = $width;
82
            }
83
            $srcset = '';
84
            for ($i = 1; $i <= $steps; $i++) {
85
                $w = (int) ceil($wMin + ($wMax - $wMin) / $steps * $i);
86
                $img = (new Image($this->builder))->resize($image['element']['attributes']['src'], $w);
87
                $srcset .= sprintf('%s %sw', $img, $w);
88
                if ($i < $steps) {
89
                    $srcset .= ', ';
90
                }
91
            }
92
            // ie: srcset="/img-480.jpg 480w, img-800.jpg 800w"
93
            $image['element']['attributes']['srcset'] = $srcset;
94
        }
95
96
        /**
97
         * Should be resized?
98
         */
99
        if (array_key_exists('resize', $result)) {
100
            $size = (int) $result['resize'];
101
            $width = $size;
102
103
            $imageResized = (new Image($this->builder))
104
                ->resize($image['element']['attributes']['src'], $size);
105
106
            if (Util::isExternalUrl($image['element']['attributes']['src'])) {
107
                return $image;
108
            }
109
110
            $image['element']['attributes']['src'] = $imageResized;
111
112
            $path = Util::joinPath(
0 ignored issues
show
Unused Code introduced by
The assignment to $path is dead and can be removed.
Loading history...
113
                $this->builder->getConfig()->getOutputPath(),
114
                ltrim($image['element']['attributes']['src'])
115
            );
116
117
            $image['element']['attributes']['width'] = $width;
118
        }
119
120
        // if responsive: set 'sizes' attribute
121
        if ($responsive) {
122
            // sizes="(max-width: 2800px) 100vw, 2800px"
123
            $image['element']['attributes']['sizes'] = sprintf('(max-width: %spx) 100vw, %spx', $width, $width);
124
        }
125
126
        // set 'class' attribute
127
        if (array_key_exists('class', $result)) {
128
            $class = $result['class'];
129
            $class = strtr($class, ',', ' ');
130
            $image['element']['attributes']['class'] = $class;
131
        }
132
133
        return $image;
134
    }
135
136
    private function removeQuery(string $path): string
137
    {
138
        return strtok($path, '?');
139
    }
140
}
141