Completed
Push — master ( 09826c...11cbad )
by Mike
132:19 queued 117:25
created

HostTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 16
c 0
b 0
f 0
dl 0
loc 40
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A checkWhetherHostIsLive() 0 31 2
1
<?php
2
3
namespace Mediadevs\Validator\Traits;
4
5
trait HostTrait
6
{
7
    /**
8
     * Pinging the hostname to see if it is live / an actual working hostname.
9
     *
10
     * @param string $host
11
     *
12
     * @return bool
13
     */
14 2
    protected function checkWhetherHostIsLive(string $host): bool
15
    {
16
        $statuses = array(
17 2
            200,
18
            301,
19
            302,
20
        );
21
22
        // Initializing curl
23 2
        $curlHandle = curl_init($host);
24
25
        // Calling the host for a response
26 2
        curl_setopt_array($curlHandle, array(
0 ignored issues
show
Bug introduced by
It seems like $curlHandle can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, 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

26
        curl_setopt_array(/** @scrutinizer ignore-type */ $curlHandle, array(
Loading history...
27 2
            CURLOPT_HEADER          => true,
28 2
            CURLOPT_NOBODY          => true,
29 2
            CURLOPT_RETURNTRANSFER  => true,
30 2
            CURLOPT_TIMEOUT         => 10,
31 2
            CURLOPT_USERAGENT       => 'page-check/1.0',
32
        ));
33
34
        // Executing request
35 2
        curl_exec($curlHandle);
0 ignored issues
show
Bug introduced by
It seems like $curlHandle can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, 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

35
        curl_exec(/** @scrutinizer ignore-type */ $curlHandle);
Loading history...
36
37
        // Collecting the status code and casting it to an integer
38 2
        $status = (int) curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
It seems like $curlHandle can also be of type false; however, parameter $ch of curl_getinfo() does only seem to accept resource, 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

38
        $status = (int) curl_getinfo(/** @scrutinizer ignore-type */ $curlHandle, CURLINFO_HTTP_CODE);
Loading history...
39
40
        // Closing curl
41 2
        curl_close($curlHandle);
0 ignored issues
show
Bug introduced by
It seems like $curlHandle can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, 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

41
        curl_close(/** @scrutinizer ignore-type */ $curlHandle);
Loading history...
42
43
        // Validating whether the http status is inside the allowed statuses array (Whether the page is live or not)
44 2
        return in_array($status, $statuses) ? true : false;
45
    }
46
}
47