Completed
Push — master ( c5fb5d...5395de )
by Stefano
02:24
created

URL::__construct()   B

Complexity

Conditions 10
Paths 49

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 7.2765
cc 10
eloc 9
nc 49
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
/**
4
 * URL
5
 *
6
 * Helper object for handling URLs
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2016 - http://caffeina.com
11
 */
12
13
class URL {
14
15
  public    $scheme    = false,
16
            $user      = false,
17
            $pass      = false,
18
            $host      = false,
19
            $port      = false,
20
            $path      = false,
21
            $query     = [],
22
            $fragment  = false;
23
24
  public function __construct($url=''){
25
    if (empty($url) || !is_string($url)) return;
26
    $tmp_url      = (strpos($url, '://') === false) ? "..N..://$url" : $url;
27
    if (mb_detect_encoding($tmp_url, 'UTF-8', true) || ($parsed = parse_url($tmp_url)) === false) {
28
      preg_match('(^((?P<scheme>[^:/?#]+):(//))?((\\3|//)?(?:(?P<user>[^:]+):(?P<pass>[^@]+)@)?(?P<host>[^/?:#]*))(:(?P<port>\\d+))?(?P<path>[^?#]*)(\\?(?P<query>[^#]*))?(#(?P<fragment>.*))?)u', $tmp_url, $parsed);
29
    }
30
    foreach($parsed as $k => $v) if(isset($this->$k)) $this->$k = $v;
31
    if ($this->scheme == '..N..') $this->scheme = null;
32
    if (!empty($this->query)) {
33
      parse_str($this->query, $this->query);
34
    }
35
  }
36
37
  public function __toString(){
38
    $d = [];
39
    if ($this->scheme)     $d[] = "{$this->scheme}://";
40
    if ($this->user)       $d[] = "{$this->user}" . (empty($this->pass)?'':":{$this->pass}") . "@";
41
    if ($this->host)       $d[] = "{$this->host}";
42
    if ($this->port)       $d[] = ":{$this->port}";
43
    if ($this->path)       $d[] = "/" . ltrim($this->path,"/");
44
    if ($this->query)      $d[] = "?" . http_build_query($this->query);
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->query of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
45
    if ($this->fragment)   $d[] = "#{$this->fragment}";
46
    return implode('', $d);
47
  }
48
49
} /* End of class */
50