Completed
Push — master ( 66cdb5...bfea69 )
by Maxime
04:50
created

Image::url()   C

Complexity

Conditions 13
Paths 97

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
nc 97
nop 7
dl 0
loc 48
ccs 30
cts 30
cp 1
crap 13
rs 6.6166
c 0
b 0
f 0

How to fix   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
namespace Distilleries\Contentful\Helpers;
4
5
class Image
6
{
7
    /**
8
     * Return image URL to serve based on given parameters.
9
     *
10
     * @param  string  $url
11
     * @param  integer  $width
12
     * @param  integer  $height
13
     * @param  string  $format
14
     * @param  integer  $quality
15
     * @param  boolean|null  $useProgressive
16
     * @param  string  $fit
17
     * @return string
18
     */
19 18
    public static function url(string $url, int $width = 0, int $height = 0, $format = '', int $quality = 0, ?bool $useProgressive = null, $fit = ''): string
20
    {
21 18
        if (empty($url)) {
22 2
            return '';
23
        }
24
25 16
        $imageUrl = '';
26
27 16
        $format = static::detectFormat($format);
28 16
        if ($format === 'webp') {
29 4
            $useProgressive = false;
30
        }
31 16
        if ($useProgressive === null) {
32 10
            $useProgressive = ! empty(config('contentful.image.use_progressive'));
33
        }
34
35 16
        if (empty($fit)) {
36 14
            $fit = 'fill';
37
        } else {
38 2
            $fit = ($fit !== 'default') ? $fit : null;
39
        }
40
41 16
        collect([
42 16
            'w' => $width,
43 16
            'h' => $height,
44 16
            'q' => ! empty($quality) ? $quality : config('contentful.image.default_quality'),
45 16
            'fm' => config('contentful.image.use_webp') ? $format : null,
46 16
            'fl' => $useProgressive ? 'progressive' : null,
47 16
            'fit' => $fit,
48
        ])->filter(function ($value) {
49 16
            return ! empty($value);
50
        })->each(function ($value, $key) use (& $imageUrl) {
51 16
            $imageUrl .= $key . '=' . $value . '&';
52 16
        });
53
54 16
        if (str_contains($url, '?')) {
55 6
            $url = explode('?', $url);
56 6
            $url = $url[0];
57
        }
58
59 16
        $searchHosts = config('contentful.image.search_hosts');
60 16
        $replaceHost = config('contentful.image.replace_host');
61 16
        if (! empty($searchHosts) and ! empty($replaceHost)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
62 16
            $url = str_replace(explode(',', $searchHosts), $replaceHost, $url);
63
        }
64
65 16
        return ! empty($url) ? $url . '?' . trim($imageUrl, '&'): '';
66
    }
67
68
    /**
69
     * Auto-detect image format to serve (based on browser capability).
70
     *
71
     * @param  string  $format
72
     * @return string|null
73
     */
74 16
    protected static function detectFormat(string $format = ''): ?string
75
    {
76
        /** @var \Jenssegers\Agent\Agent $agent */
77 16
        $agent = app('agent');
78
79 16
        if (empty($format)) {
80 16
            $browser = mb_strtolower($agent->browser());
81 16
            if (($browser === 'chrome') and ! $agent->isMobile()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
82 16
                $format = config('contentful.image.use_webp') ? 'webp' : null;
83
            }
84
        }
85
86 16
        return $format;
87
    }
88
}