Completed
Push — develop ( 67257c...87c3e8 )
by Lars
17:33
created

pdp-parse-url.php ➔ pdp_parse_url()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

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