Passed
Pull Request — main (#22)
by Andrey
13:21
created

Http::scheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Exception;
6
use Helldar\Support\Exceptions\NotValidUrlException;
7
use Helldar\Support\Facades\Helpers\Filesystem\File;
8
use Helldar\Support\Facades\Helpers\HttpBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\Support\Helpers\HttpBuilder. 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...
9
10
final class Http
11
{
12
    /**
13
     * Check if the string is a valid URL.
14
     *
15
     * @param  string|null  $url
16
     *
17
     * @return bool
18
     */
19
    public function isUrl(?string $url): bool
20
    {
21
        return filter_var($url, FILTER_VALIDATE_URL) !== false;
22
    }
23
24
    /**
25
     * Validate if the value is a valid URL or throw an error.
26
     *
27
     * @param  string|null  $url
28
     *
29
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
30
     */
31
    public function validateUrl(?string $url): void
32
    {
33
        if (! $this->isUrl($url)) {
34
            throw new NotValidUrlException($url);
35
        }
36
    }
37
38
    /**
39
     * Check if the specified URL exists.
40
     *
41
     * @param  string|null  $url
42
     *
43
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
44
     *
45
     * @return bool
46
     */
47
    public function exists(?string $url): bool
48
    {
49
        $this->validateUrl($url);
50
51
        try {
52
            $headers = get_headers($url);
53
54
            $key   = array_search('HTTP/', $headers);
55
            $value = $headers[$key] ?? null;
56
57
            preg_match('/HTTP\/\d{1}\.?\d?\s[2-3]\d{2}/i', $value, $matches);
58
59
            return count($matches) > 0;
60
        }
61
        catch (Exception $e) {
62
            return false;
63
        }
64
    }
65
66
    /**
67
     * Get the domain name from the URL.
68
     *
69
     * @param  string|null  $url
70
     *
71
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
72
     *
73
     * @return string
74
     */
75
    public function domain(?string $url): string
76
    {
77
        $this->validateUrl($url);
78
79
        return HttpBuilder::parse($url)->getHost();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Helldar\Support\F...:parse($url)->getHost() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82
    /**
83
     * Get the subdomain name from the URL.
84
     *
85
     * @param  string|null  $url
86
     *
87
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
88
     *
89
     * @return string|null
90
     */
91
    public function subdomain(?string $url): ?string
92
    {
93
        $this->validateUrl($url);
94
95
        $host = explode('.', HttpBuilder::parse($url)->getHost());
96
97
        return count($host) > 2 ? reset($host) : null;
98
    }
99
100
    /**
101
     * Get the scheme and host from the URL.
102
     *
103
     * @param  string|null  $url
104
     *
105
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
106
     *
107
     * @return string
108
     */
109
    public function host(?string $url): string
110
    {
111
        $this->validateUrl($url);
112
113
        return HttpBuilder::same()
114
            ->parse($url, PHP_URL_SCHEME)
0 ignored issues
show
Bug introduced by
It seems like $url can also be of type null; however, parameter $url of Helldar\Support\Helpers\HttpBuilder::parse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
            ->parse(/** @scrutinizer ignore-type */ $url, PHP_URL_SCHEME)
Loading history...
115
            ->parse($url, PHP_URL_HOST)
116
            ->compile();
117
    }
118
119
    /**
120
     * Get the scheme name from the URL.
121
     *
122
     * @param  string|null  $url
123
     *
124
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
125
     *
126
     * @return string
127
     */
128
    public function scheme(?string $url): string
129
    {
130
        $this->validateUrl($url);
131
132
        return HttpBuilder::parse($url)->getScheme();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Helldar\Support\F...arse($url)->getScheme() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
133
    }
134
135
    /**
136
     * Check the existence of the file and return the default value if it is missing.
137
     *
138
     * @param  string  $url
139
     * @param  string|null  $default
140
     *
141
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
142
     *
143
     * @return string|null
144
     */
145
    public function image(string $url, string $default = null): ?string
146
    {
147
        return $this->isUrl($url)
148
            ? ($this->exists($url) ? $url : $default)
149
            : (File::exists($url) ? $url : $default);
150
    }
151
}
152