Passed
Pull Request — main (#22)
by Andrey
14:27
created

HttpBuilder::prepare()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 10
nc 256
nop 0
dl 0
loc 12
rs 6.5222
c 1
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Helldar\Support\Concerns\Makeable;
6
use Helldar\Support\Facades\Helpers\Arr;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\Support\Helpers\Arr. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
/**
9
 * Based on code by Maksim (Ellrion) Platonov.
10
 *
11
 * @see https://gist.github.com/Ellrion/f51ba0d40ae1d62eeae44fd1adf7b704
12
 */
13
final class HttpBuilder
14
{
15
    use Makeable;
16
17
    protected $parsed = [];
18
19
    protected $components = [
20
        PHP_URL_SCHEME   => 'scheme',
21
        PHP_URL_HOST     => 'host',
22
        PHP_URL_PORT     => 'port',
23
        PHP_URL_USER     => 'user',
24
        PHP_URL_PASS     => 'pass',
25
        PHP_URL_QUERY    => 'query',
26
        PHP_URL_PATH     => 'path',
27
        PHP_URL_FRAGMENT => 'fragment',
28
    ];
29
30
    public function same(): self
31
    {
32
        return $this;
33
    }
34
35
    public function parse(string $url, int $component = -1): self
36
    {
37
        $component = $this->componentIndex($component);
38
        $key       = $this->componentKey($component);
39
40
        $component === -1 || empty($key)
41
            ? $this->parsed = parse_url($url)
42
            : $this->parsed[$key] = parse_url($url, $component);
43
44
        return $this;
45
    }
46
47
    public function getScheme(): ?string
48
    {
49
        return $this->get('scheme');
50
    }
51
52
    public function getHost(): ?string
53
    {
54
        return $this->get('host');
55
    }
56
57
    public function getPort(): ?string
58
    {
59
        return $this->get('port');
60
    }
61
62
    public function getUser(): ?string
63
    {
64
        return $this->get('user');
65
    }
66
67
    public function getPassword(): ?string
68
    {
69
        return $this->get('pass');
70
    }
71
72
    public function getPath(): ?string
73
    {
74
        return $this->get('path');
75
    }
76
77
    public function getQuery(): ?string
78
    {
79
        return $this->get('query');
80
    }
81
82
    public function getFragment(): ?string
83
    {
84
        return $this->get('fragment');
85
    }
86
87
    public function compile(): string
88
    {
89
        return implode('', array_filter($this->prepare()));
90
    }
91
92
    protected function prepare(): array
93
    {
94
        return [
95
            $this->getScheme() ? $this->getScheme() . '://' : '',
96
            $this->getUser(),
97
            $this->getPassword() ? ':' . $this->getPassword() : '',
98
            $this->getUser() || $this->getPassword() ? '@' : '',
99
            $this->getHost(),
100
            $this->getPort() ? ':' . $this->getPort() : '',
101
            $this->getPath() ? '/' . ltrim($this->getPath(), '/') : '',
102
            $this->getQuery() ? '?' . $this->getQuery() : '',
103
            $this->getFragment() ? '#' . $this->getFragment() : '',
104
        ];
105
    }
106
107
    protected function get(string $key): ?string
108
    {
109
        return Arr::get($this->parsed, $key);
110
    }
111
112
    protected function componentIndex(int $component = -1): int
113
    {
114
        return Arr::getKey($this->components, $component, -1);
115
    }
116
117
    protected function componentKey(int $component = -1): ?string
118
    {
119
        return Arr::get($this->components, $component);
120
    }
121
}
122