UrlParser::parseSegments()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
c 0
b 0
f 0
rs 8.439
cc 6
eloc 26
nc 6
nop 1
1
<?php
2
3
namespace Weew\Url;
4
5
class UrlParser implements IUrlParser {
6
    /**
7
     * @param $string
8
     *
9
     * @return array
10
     */
11
    public function parse($string) {
12
        $segments = $this->parseSegments($string);
13
        $segments = array_extend($segments, $this->parseHost(array_get($segments, 'host')));
0 ignored issues
show
Bug introduced by
It seems like $segments can also be of type null; however, array_get() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
14
15
        return $this->replaceEmptyStringsWithNull($segments);
16
    }
17
18
    /**
19
     * @param $string
20
     *
21
     * @return array
22
     */
23
    public function parseHost($string) {
24
        $segments = [
25
            'tld' => null,
26
            'domain' => null,
27
            'subdomain' => null,
28
        ];
29
30
        $parts = explode('.', $string);
31
32
        if (count($parts) == 1) {
33
            $segments['domain'] = $parts[0];
34
35
            return $segments;
36
        }
37
38
        if (count($parts)) {
39
            $segments['tld'] = array_pop($parts);
40
        }
41
42
        if (count($parts)) {
43
            $segments['domain'] = array_pop($parts);
44
        }
45
46
        if (count($parts)) {
47
            $segments['subdomain'] = implode('.', $parts);
48
        }
49
50
        return $this->replaceEmptyStringsWithNull($segments);
51
    }
52
53
    /**
54
     * @param $string
55
     *
56
     * @return array
57
     */
58
    protected function parseSegments($string) {
59
        preg_match_all(
60
            '/^' .
61
            '((?P<protocol>.*):\/\/)?' .
62
            '(' .
63
                '(?P<username>.*?)' .
64
                '(:(?P<password>.*?)' .
65
            ')@)?' .
66
            '(?P<host>[^:\/\s]+)?' .
67
            '(:(?P<port>\d*))?' .
68
            '(?P<path>[^#\?]*)?' .
69
            '(\?(?P<query>[^#]*))?' .
70
            '(#(?P<fragment>.*))?' .
71
            '$/',
72
            $string,
73
            $segments
74
        );
75
76
        foreach ($segments as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $segments of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
77
            if (is_numeric($key)) {
78
                unset($segments[$key]);
79
            } else {
80
                $segments[$key] = array_pop($value);
81
            }
82
        }
83
84
        $host = array_get($segments, 'host');
0 ignored issues
show
Bug introduced by
It seems like $segments can also be of type null; however, array_get() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
86
        if ($host && $host !== 'localhost' && stripos($host, '.') === false) {
87
            $segments['path'] = '/' . $host . array_get($segments, 'path');
88
            $segments['host'] = null;
89
        }
90
91
        return $segments;
92
    }
93
94
    /**
95
     * @param $array
96
     *
97
     * @return array
98
     */
99
    protected function replaceEmptyStringsWithNull(array $array) {
100
        return array_map(function($item) {
101
            return ($item === '') ? null : $item;
102
        }, $array);
103
    }
104
}
105