UrlHelper::absoluteUrlWithProtocol()   B
last analyzed

Complexity

Conditions 11
Paths 15

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 29
rs 7.3166
c 0
b 0
f 0
cc 11
nc 15
nop 1

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
 * ImageOptimize plugin for Craft CMS
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\imageoptimize\helpers;
12
13
use Craft;
14
use craft\errors\SiteNotFoundException;
15
use craft\helpers\UrlHelper as CraftUrlHelper;
16
17
use yii\base\Exception;
18
19
/**
20
 * ImageOptimize Settings model
21
 *
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
23
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
26
class UrlHelper extends CraftUrlHelper
27
{
28
    // Public Static Properties
29
    // =========================================================================
30
31
    // Public Static Methods
32
    // =========================================================================
33
34
    /**
35
     * Return an absolute URL with protocol that curl will be happy with
36
     *
37
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
38
     *
39
     * @return string
40
     */
41
    public static function absoluteUrlWithProtocol($url)
42
    {
43
        // Make this a full URL
44
        if (!self::isAbsoluteUrl($url)) {
45
            $protocol = "http";
46
            if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (IssetNode && strcasecmp...PROTO'], 'https') === 0, Probably Intended Meaning: IssetNode && (strcasecmp...ROTO'], 'https') === 0)
Loading history...
47
                || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0
48
            ) {
49
                $protocol = "https";
50
            }
51
            if (self::isProtocolRelativeUrl($url)) {
52
                try {
53
                    $url = self::urlWithScheme($url, $protocol);
54
                } catch (SiteNotFoundException $e) {
55
                    Craft::error($e->getMessage(), __METHOD__);
56
                }
57
            } else {
58
                try {
59
                    $url = self::siteUrl($url, null, $protocol);
60
                    if (self::isProtocolRelativeUrl($url)) {
61
                        $url = self::urlWithScheme($url, $protocol);
62
                    }
63
                } catch (Exception $e) {
64
                    Craft::error($e->getMessage(), __METHOD__);
65
                }
66
            }
67
        }
68
69
        return $url;
70
    }
71
}
72