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

Http   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 28
c 9
b 0
f 0
dl 0
loc 111
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A scheme() 0 5 1
A image() 0 5 4
A validateUrl() 0 4 2
A isUrl() 0 3 1
A exists() 0 15 2
A subdomain() 0 7 2
A host() 0 8 1
A domain() 0 5 1
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
     * Checks whether the string is URL address.
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
    public function validateUrl(?string $url): void
25
    {
26
        if (! $this->isUrl($url)) {
27
            throw new NotValidUrlException($url);
28
        }
29
    }
30
31
    /**
32
     * Checks whether a URL exists.
33
     *
34
     * @param  string  $url
35
     *
36
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
37
     *
38
     * @return bool
39
     */
40
    public function exists(?string $url): bool
41
    {
42
        $this->validateUrl($url);
43
44
        try {
45
            $headers = get_headers($url);
46
47
            $key   = array_search('HTTP/', $headers);
48
            $value = $headers[$key] ?? null;
49
50
            preg_match('/HTTP\/\d{1}\.?\d?\s[2-3]\d{2}/i', $value, $matches);
51
52
            return count($matches) > 0;
53
        } catch (Exception $e) {
54
            return false;
55
        }
56
    }
57
58
    /**
59
     * Get the domain name from the URL.
60
     *
61
     * @param  string|null  $url
62
     *
63
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
64
     *
65
     * @return string
66
     */
67
    public function domain(?string $url): string
68
    {
69
        $this->validateUrl($url);
70
71
        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...
72
    }
73
74
    /**
75
     * Retrieving the current subdomain name.
76
     *
77
     * @param  string|null  $url
78
     * @param  string|null  $default
79
     *
80
     * @return string|null
81
     */
82
    public function subdomain(?string $url): ?string
83
    {
84
        $this->validateUrl($url);
85
86
        $host = explode('.', HttpBuilder::parse($url)->getHost());
87
88
        return count($host) > 2 ? reset($host) : null;
89
    }
90
91
    public function host(?string $url): string
92
    {
93
        $this->validateUrl($url);
94
95
        return HttpBuilder::same()
96
            ->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

96
            ->parse(/** @scrutinizer ignore-type */ $url, PHP_URL_SCHEME)
Loading history...
97
            ->parse($url, PHP_URL_HOST)
98
            ->compile();
99
    }
100
101
    public function scheme(?string $url): string
102
    {
103
        $this->validateUrl($url);
104
105
        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...
106
    }
107
108
    /**
109
     * Check the existence of the file and return the default value if it is missing.
110
     *
111
     * @param  string  $url
112
     * @param  string|null  $default
113
     *
114
     * @return string|null
115
     */
116
    public function image(string $url, string $default = null): ?string
117
    {
118
        return $this->isUrl($url)
119
            ? ($this->exists($url) ? $url : $default)
120
            : (File::exists($url) ? $url : $default);
121
    }
122
}
123