url()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 54
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 2
nop 4
dl 0
loc 54
rs 9.7666
c 0
b 0
f 0

How to fix   Long Method   

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 Helick\Imgix;
4
5
use Imgix\UrlBuilder;
6
7
/**
8
 * Get the imgix domain.
9
 *
10
 * @return string
11
 */
12
function domain(): string
13
{
14
    return defined('IMGIX_DOMAIN')
15
        ? IMGIX_DOMAIN
0 ignored issues
show
Bug introduced by
The constant Helick\Imgix\IMGIX_DOMAIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
        : '';
17
}
18
19
/**
20
 * Get the imgix url.
21
 *
22
 * @param string $imageUrl
23
 * @param array  $args
24
 * @param bool   $useHttps
25
 * @param string $signKey
26
 *
27
 * @return string
28
 */
29
function url(string $imageUrl, array $args = [], bool $useHttps = true, string $signKey = ''): string
30
{
31
    $requiresProcessing = strpos($imageUrl, wp_upload_dir()['baseurl']) === 0;
0 ignored issues
show
Bug introduced by
The function wp_upload_dir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

31
    $requiresProcessing = strpos($imageUrl, /** @scrutinizer ignore-call */ wp_upload_dir()['baseurl']) === 0;
Loading history...
32
33
    /**
34
     * Control whether the given image url is processable.
35
     *
36
     * @param bool   $isProcessable
37
     * @param string $imageUrl
38
     * @param array  $args
39
     */
40
    $isProcessable = apply_filters('helick_imgix_image_url_processable', true, $imageUrl, $args);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

40
    $isProcessable = /** @scrutinizer ignore-call */ apply_filters('helick_imgix_image_url_processable', true, $imageUrl, $args);
Loading history...
41
    $isProcessable = (bool)$isProcessable;
42
43
    if (!$requiresProcessing || !$isProcessable) {
44
        return $imageUrl;
45
    }
46
47
    $imageUrlPath = parse_url($imageUrl, PHP_URL_PATH);
48
49
    $imageFile = basename($imageUrlPath);
50
    $imageFile = urlencode($imageFile);
51
52
    $imageUrlPath = str_replace($imageUrl, $imageFile, $imageUrlPath);
53
54
    /**
55
     * Control the imgix image url path.
56
     *
57
     * @param string $imageUrlPath
58
     * @param array  $args
59
     */
60
    $imageUrlPath = apply_filters('helick_imgix_image_url_path', $imageUrlPath, $args);
61
62
    /**
63
     * Control the imgix arguments.
64
     *
65
     * @param array  $args
66
     * @param string $imageUrlPath
67
     */
68
    $args = apply_filters('helick_imgix_args', $args, $imageUrlPath);
69
70
    $urlBuilder = new UrlBuilder(domain(), $useHttps, $signKey);
71
    $imgixUrl   = $urlBuilder->createURL($imageUrlPath, $args);
72
73
    /**
74
     * Control the imgix url.
75
     *
76
     * @param string $imgixUrl
77
     * @param string $imageUrl
78
     * @param array  $args
79
     */
80
    $imgixUrl = apply_filters('helick_imgix_url', $imgixUrl, $imageUrl, $args);
81
82
    return $imgixUrl;
83
}
84