CUrl   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 221
Duplicated Lines 14.03 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 31
loc 221
rs 10
wmc 26
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
C create() 0 29 7
B createRelative() 16 22 5
C asset() 15 24 7
A setSiteUrl() 0 5 1
A setBaseUrl() 0 5 1
A setStaticSiteUrl() 0 5 1
A setStaticBaseUrl() 0 5 1
A setScriptName() 0 5 1
A setUrlType() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Anax\Url;
4
5
/**
6
 * A helper to create urls.
7
 *
8
 */
9
class CUrl
10
{
11
12
    /**
13
     * Properties
14
     *
15
     */
16
    const URL_CLEAN  = 'clean';  // controller/action/param1/param2
17
    const URL_APPEND = 'append'; // index.php/controller/action/param1/param2
18
19
    private $urlType = self::URL_APPEND; // What type of urls to generate
20
21
    private $siteUrl = null; // Siteurl to prepend to all absolute urls created
22
    private $baseUrl = null; // Baseurl to prepend to all relative urls created
23
    private $scriptName = null; // Name of the frontcontroller script
24
25
26
    private $staticSiteUrl = null; // Siteurl to prepend to all absolute urls for assets
27
    private $staticBaseUrl = null; // Baseurl to prepend to all relative urls for assets
28
29
30
31
    /**
32
     * Create an url and prepending the baseUrl.
33
     *
34
     * @param string $uri part of uri to use when creating an url. "" or null means baseurl to
35
     * current frontcontroller.
36
     *
37
     * @return string as resulting url.
38
     */
39
    public function create($uri = null)
40
    {
41
        if (empty($uri)) {
42
43
            // Empty uri means baseurl
44
            return $this->baseUrl
45
                . (($this->urlType == self::URL_APPEND)
46
                    ? "/$this->scriptName"
47
                    : null);
48
49
        } elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") {
50
51
            // Fully qualified, just leave as is.
52
            return rtrim($uri, '/');
53
54
        } elseif ($uri[0] == '/') {
55
56
            // Absolute url, prepend with siteUrl
57
            return rtrim($this->siteUrl . rtrim($uri, '/'), '/');
58
59
        }
60
61
        $uri = rtrim($uri, '/');
62
        if ($this->urlType == self::URL_CLEAN) {
63
            return $this->baseUrl . '/' . $uri;
64
        } else {
65
            return $this->baseUrl . '/' . $this->scriptName . '/' . $uri;
66
        }
67
    }
68
69
70
71
    /**
72
     * Create an url and prepend the baseUrl to the directory of the frontcontroller.
73
     *
74
     * @param string $uri part of uri to use when creating an url. "" or null means baseurl to
75
     * directory of the current frontcontroller.
76
     *
77
     * @return string as resulting url.
78
     */
79
    public function createRelative($uri = null)
80
    {
81 View Code Duplication
        if (empty($uri)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
83
            // Empty uri means baseurl
84
            return $this->baseUrl;
85
86
        } elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") {
87
88
            // Fully qualified, just leave as is.
89
            return rtrim($uri, '/');
90
91
        } elseif ($uri[0] == '/') {
92
93
            // Absolute url, prepend with siteUrl
94
            return rtrim($this->siteUrl . rtrim($uri, '/'), '/');
95
96
        }
97
98
        $uri = rtrim($uri, '/');
99
        return $this->baseUrl . '/' . $uri;
100
    }
101
102
103
104
    /**
105
     * Create an url for a static asset.
106
     *
107
     * @param string $uri part of uri to use when creating an url.
108
     *
109
     * @return string as resulting url.
110
     */
111
    public function asset($uri = null)
112
    {
113 View Code Duplication
        if (empty($uri)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
114
115
            // Allow empty
116
117
        } elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") {
118
119
            // Fully qualified, just leave as is.
120
            return rtrim($uri, '/');
121
122
        } elseif ($uri[0] == '/') {
123
124
            // Absolute url, prepend with staticSiteUrl
125
            return rtrim($this->staticSiteUrl . rtrim($uri, '/'), '/');
126
127
        }
128
129
        $baseUrl = isset($this->staticBaseUrl) ? $this->staticBaseUrl : $this->baseUrl;
130
131
        return empty($uri)
132
            ? $baseUrl
133
            : $baseUrl . '/' . $uri;
134
    }
135
136
137
138
    /**
139
     * Set the siteUrl to prepend all absolute urls created.
140
     *
141
     * @param string $url part of url to use when creating an url.
142
     *
143
     * @return $this
144
     */
145
    public function setSiteUrl($url)
146
    {
147
        $this->siteUrl = rtrim($url, '/');
148
        return $this;
149
    }
150
151
152
153
    /**
154
     * Set the baseUrl to prepend all relative urls created.
155
     *
156
     * @param string $url part of url to use when creating an url.
157
     *
158
     * @return $this
159
     */
160
    public function setBaseUrl($url)
161
    {
162
        $this->baseUrl = rtrim($url, '/');
163
        return $this;
164
    }
165
166
167
168
    /**
169
     * Set the siteUrl to prepend absolute urls for assets.
170
     *
171
     * @param string $url part of url to use when creating an url.
172
     *
173
     * @return $this
174
     */
175
    public function setStaticSiteUrl($url)
176
    {
177
        $this->staticSiteUrl = rtrim($url, '/');
178
        return $this;
179
    }
180
181
182
183
    /**
184
     * Set the baseUrl to prepend relative urls for assets.
185
     *
186
     * @param string $url part of url to use when creating an url.
187
     *
188
     * @return $this
189
     */
190
    public function setStaticBaseUrl($url)
191
    {
192
        $this->staticBaseUrl = rtrim($url, '/');
193
        return $this;
194
    }
195
196
197
198
    /**
199
     * Set the scriptname to use when creating URL_APPEND urls.
200
     *
201
     * @param string $name as the scriptname, for example index.php.
202
     *
203
     * @return $this
204
     */
205
    public function setScriptName($name)
206
    {
207
        $this->scriptName = $name;
208
        return $this;
209
    }
210
211
212
213
    /**
214
     * Set the type of urls to be generated, URL_CLEAN, URL_APPEND.
215
     *
216
     * @param string $type what type of urls to create.
217
     *
218
     * @return $this
219
     */
220
    public function setUrlType($type)
221
    {
222
        if (!in_array($type, [self::URL_APPEND, self::URL_CLEAN])) {
223
            throw new \Exception("Unsupported Url type.");
224
        }
225
226
        $this->urlType = $type;
227
        return $this;
228
    }
229
}
230