pdp-parse-url.php ➔ pdp_parse_url()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 2
dl 0
loc 34
ccs 17
cts 17
cp 1
crap 5
rs 9.0648
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PHP Domain Parser: Public Suffix List based URL parsing.
5
 *
6
 * @link      http://github.com/jeremykendall/php-domain-parser for the canonical source repository
7
 *
8
 * @copyright Copyright (c) 2014 Jeremy Kendall (http://about.me/jeremykendall)
9
 * @license   http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
10
 */
11
12
namespace {
13
14
  if (!\function_exists('pdp_parse_url')) {
15
    /**
16
     * UTF-8 aware parse_url() replacement.
17
     *
18
     * Taken from php.net manual comments {@link http://php.net/manual/en/function.parse-url.php#114817}
19
     *
20
     * @param string $url       The URL to parse
21
     * @param int    $component Specify one of PHP_URL_SCHEME, PHP_URL_HOST,
22
     *                          PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or
23
     *                          PHP_URL_FRAGMENT to retrieve just a specific URL component as a string
24
     *                          (except when PHP_URL_PORT is given, in which case the return value will
25
     *                          be an integer).
26
     *
27
     * @return mixed See parse_url documentation {@link http://us1.php.net/parse_url}
28
     */
29
    function pdp_parse_url($url, $component = -1)
30
    {
31 57
      $pattern = '%([a-zA-Z][a-zA-Z0-9+\-.]*)?(:?//)?([^:/@?&=#\[\]]+)%us';
32
33 57
      $enc_url = \preg_replace_callback(
34 57
          $pattern,
35
          function ($matches) {
36 57
            $encoded = \urlencode($matches[3]);
37
38 57
            return \sprintf('%s%s%s', $matches[1], $matches[2], $encoded);
39 57
          },
40 57
          $url
41
      );
42
43 57
      $parts = \parse_url($enc_url, $component);
44
45 57
      if ($parts === false) {
46 2
        return $parts;
47
      }
48
49 55
      if (\is_array($parts)) {
50 11
        foreach ($parts as $name => &$value) {
51 11
          if ($name === 'scheme') {
52 11
            continue;
53
          }
54
55 11
          $value = \urldecode($value);
56
        }
57
      } else {
58 44
        $parts = \urldecode($parts);
59
      }
60
61 55
      return $parts;
62
    }
63
  }
64
}
65