URI_Core   B
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 269
Duplicated Lines 15.61 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 42
loc 269
rs 8.8
wmc 36
lcom 1
cbo 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 11 2
A segment() 14 14 4
A rsegment() 14 14 4
A argument() 14 14 4
A segment_array() 0 4 1
A rsegment_array() 0 4 1
A argument_array() 0 4 1
B build_array() 0 22 4
A string() 0 4 1
A __toString() 0 4 1
A total_segments() 0 4 1
A total_rsegments() 0 4 1
A total_arguments() 0 4 1
A last_segment() 0 8 2
A last_rsegment() 0 8 2
A controller_path() 0 4 2
A controller() 0 4 2
A method() 0 4 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 defined('SYSPATH') or die('No direct access allowed.');
2
/**
3
 * URI library.
4
 *
5
 * $Id: URI.php 4072 2009-03-13 17:20:38Z jheathco $
6
 *
7
 * @package    Core
8
 * @author     Kohana Team
9
 * @copyright  (c) 2007-2008 Kohana Team
10
 * @license    http://kohanaphp.com/license.html
11
 */
12
class URI_Core extends Router
13
{
14
15
    /**
16
     * Returns a singleton instance of URI.
17
     *
18
     * @return  object
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use URI.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
19
     */
20
    public static function instance()
21
    {
22
        static $instance;
23
24
        if ($instance == null) {
25
            // Initialize the URI instance
26
            $instance = new URI;
27
        }
28
29
        return $instance;
30
    }
31
32
    /**
33
     * Retrieve a specific URI segment.
34
     *
35
     * @param   integer|string  segment number or label
36
     * @param   mixed           default value returned if segment does not exist
37
     * @return  string
38
     */
39 View Code Duplication
    public function segment($index = 1, $default = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
40
    {
41
        if (is_string($index)) {
42
            if (($key = array_search($index, URI::$segments)) === false) {
43
                return $default;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $default; (boolean) is incompatible with the return type documented by URI_Core::segment of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
44
            }
45
46
            $index = $key + 2;
47
        }
48
49
        $index = (int) $index - 1;
50
51
        return isset(URI::$segments[$index]) ? URI::$segments[$index] : $default;
52
    }
53
54
    /**
55
     * Retrieve a specific routed URI segment.
56
     *
57
     * @param   integer|string  rsegment number or label
58
     * @param   mixed           default value returned if segment does not exist
59
     * @return  string
60
     */
61 View Code Duplication
    public function rsegment($index = 1, $default = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
62
    {
63
        if (is_string($index)) {
64
            if (($key = array_search($index, URI::$rsegments)) === false) {
65
                return $default;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $default; (boolean) is incompatible with the return type documented by URI_Core::rsegment of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
66
            }
67
68
            $index = $key + 2;
69
        }
70
71
        $index = (int) $index - 1;
72
73
        return isset(URI::$rsegments[$index]) ? URI::$rsegments[$index] : $default;
74
    }
75
76
    /**
77
     * Retrieve a specific URI argument.
78
     * This is the part of the segments that does not indicate controller or method
79
     *
80
     * @param   integer|string  argument number or label
81
     * @param   mixed           default value returned if segment does not exist
82
     * @return  string
83
     */
84 View Code Duplication
    public function argument($index = 1, $default = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
85
    {
86
        if (is_string($index)) {
87
            if (($key = array_search($index, URI::$arguments)) === false) {
88
                return $default;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $default; (boolean) is incompatible with the return type documented by URI_Core::argument of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
89
            }
90
91
            $index = $key + 2;
92
        }
93
94
        $index = (int) $index - 1;
95
96
        return isset(URI::$arguments[$index]) ? URI::$arguments[$index] : $default;
97
    }
98
99
    /**
100
     * Returns an array containing all the URI segments.
101
     *
102
     * @param   integer  segment offset
103
     * @param   boolean  return an associative array
104
     * @return  array
105
     */
106
    public function segment_array($offset = 0, $associative = false)
107
    {
108
        return $this->build_array(URI::$segments, $offset, $associative);
109
    }
110
111
    /**
112
     * Returns an array containing all the re-routed URI segments.
113
     *
114
     * @param   integer  rsegment offset
115
     * @param   boolean  return an associative array
116
     * @return  array
117
     */
118
    public function rsegment_array($offset = 0, $associative = false)
119
    {
120
        return $this->build_array(URI::$rsegments, $offset, $associative);
121
    }
122
123
    /**
124
     * Returns an array containing all the URI arguments.
125
     *
126
     * @param   integer  segment offset
127
     * @param   boolean  return an associative array
128
     * @return  array
129
     */
130
    public function argument_array($offset = 0, $associative = false)
131
    {
132
        return $this->build_array(URI::$arguments, $offset, $associative);
133
    }
134
135
    /**
136
     * Creates a simple or associative array from an array and an offset.
137
     * Used as a helper for (r)segment_array and argument_array.
138
     *
139
     * @param   array    array to rebuild
140
     * @param   integer  offset to start from
141
     * @param   boolean  create an associative array
142
     * @return  array
143
     */
144
    public function build_array($array, $offset = 0, $associative = false)
145
    {
146
        // Prevent the keys from being improperly indexed
147
        array_unshift($array, 0);
148
149
        // Slice the array, preserving the keys
150
        $array = array_slice($array, $offset + 1, count($array) - 1, true);
151
152
        if ($associative === false) {
153
            return $array;
154
        }
155
156
        $associative = array();
157
        $pairs       = array_chunk($array, 2);
158
159
        foreach ($pairs as $pair) {
160
            // Add the key/value pair to the associative array
161
            $associative[$pair[0]] = isset($pair[1]) ? $pair[1] : '';
162
        }
163
164
        return $associative;
165
    }
166
167
    /**
168
     * Returns the complete URI as a string.
169
     *
170
     * @return  string
171
     */
172
    public function string()
173
    {
174
        return URI::$current_uri;
175
    }
176
177
    /**
178
     * Magic method for converting an object to a string.
179
     *
180
     * @return  string
181
     */
182
    public function __toString()
183
    {
184
        return URI::$current_uri;
185
    }
186
187
    /**
188
     * Returns the total number of URI segments.
189
     *
190
     * @return  integer
191
     */
192
    public function total_segments()
193
    {
194
        return count(URI::$segments);
195
    }
196
197
    /**
198
     * Returns the total number of re-routed URI segments.
199
     *
200
     * @return  integer
201
     */
202
    public function total_rsegments()
203
    {
204
        return count(URI::$rsegments);
205
    }
206
207
    /**
208
     * Returns the total number of URI arguments.
209
     *
210
     * @return  integer
211
     */
212
    public function total_arguments()
213
    {
214
        return count(URI::$arguments);
215
    }
216
217
    /**
218
     * Returns the last URI segment.
219
     *
220
     * @param   mixed   default value returned if segment does not exist
221
     * @return  string
222
     */
223
    public function last_segment($default = false)
224
    {
225
        if (($end = $this->total_segments()) < 1) {
226
            return $default;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $default; (boolean) is incompatible with the return type documented by URI_Core::last_segment of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
227
        }
228
229
        return URI::$segments[$end - 1];
230
    }
231
232
    /**
233
     * Returns the last re-routed URI segment.
234
     *
235
     * @param   mixed   default value returned if segment does not exist
236
     * @return  string
237
     */
238
    public function last_rsegment($default = false)
239
    {
240
        if (($end = $this->total_segments()) < 1) {
241
            return $default;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $default; (boolean) is incompatible with the return type documented by URI_Core::last_rsegment of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
242
        }
243
244
        return URI::$rsegments[$end - 1];
245
    }
246
247
    /**
248
     * Returns the path to the current controller (not including the actual
249
     * controller), as a web path.
250
     *
251
     * @param   boolean  return a full url, or only the path specifically
252
     * @return  string
253
     */
254
    public function controller_path($full = true)
255
    {
256
        return ($full) ? url::site(URI::$controller_path) : URI::$controller_path;
257
    }
258
259
    /**
260
     * Returns the current controller, as a web path.
261
     *
262
     * @param   boolean  return a full url, or only the controller specifically
263
     * @return  string
264
     */
265
    public function controller($full = true)
266
    {
267
        return ($full) ? url::site(URI::$controller_path.URI::$controller) : URI::$controller;
268
    }
269
270
    /**
271
     * Returns the current method, as a web path.
272
     *
273
     * @param   boolean  return a full url, or only the method specifically
274
     * @return  string
275
     */
276
    public function method($full = true)
277
    {
278
        return ($full) ? url::site(URI::$controller_path.URI::$controller.'/'.URI::$method) : URI::$method;
279
    }
280
} // End URI Class
281