UrlValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 48
ccs 0
cts 15
cp 0
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatusCode() 0 9 3
A getHeader() 0 6 2
A validate() 0 8 3
1
<?php
2
/**
3
 * @author Muhammed Akbulut <[email protected]>
4
 * @copyright Zicht Online <http://www.zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\UrlBundle\Service;
8
9
/**
10
 * Class UrlValidator
11
 *
12
 * @package Zicht\Bundle\UrlBundle\Util
13
 */
14
class UrlValidator
15
{
16
    /**
17
     * Returns true when url does not return error codes or not found.
18
     *
19
     * @param string $url
20
     * @return boolean
21
     */
22
    public function validate($url)
23
    {
24
        if (null !== ($headers = $this->getHeader($url))) {
0 ignored issues
show
introduced by
The condition null !== $headers = $this->getHeader($url) is always false.
Loading history...
Bug introduced by
Are you sure the assignment to $headers is correct as $this->getHeader($url) targeting Zicht\Bundle\UrlBundle\S...lValidator::getHeader() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
25
            $statusCode = $this->getStatusCode($headers);
26
            // see https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
27
            return ($statusCode >= 200 && $statusCode < 300);
28
        }
29
        return false;
30
    }
31
32
    /**
33
     * Fetch a url with a HEAD request because we just want to check status code.
34
     *
35
     * @param string $url
36
     * @return array|null
37
     */
38
    protected function getHeader($url)
39
    {
40
        if (false !== @file_get_contents($url, false, stream_context_create(['http' => ['method' => 'HEAD']]))) {
41
            return $http_response_header;
42
        } else {
43
            return null;
44
        }
45
    }
46
47
    /**
48
     * Parse the headers array and search for the status pattern
49
     *
50
     * @param array $headers
51
     * @return int
52
     */
53
    protected function getStatusCode(array $headers)
54
    {
55
        $status = 0;
56
        foreach ($headers as $header) {
57
            if (preg_match('#^HTTP/(?:[^\s]+)\s(?P<code>\d+)\s#', $header, $match)) {
58
                $status = (int)$match['code'];
59
            }
60
        }
61
        return $status;
62
    }
63
}
64