GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UrlValidator::validateField()   C
last analyzed

Complexity

Conditions 11
Paths 10

Size

Total Lines 62
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 6.1722
c 0
b 0
f 0
cc 11
eloc 34
nc 10
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the PHPMongo package.
5
 *
6
 * (c) Dmytro Sokil <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sokil\Mongo\Validator;
13
14
use Sokil\Mongo\Structure;
15
16
class UrlValidator extends \Sokil\Mongo\Validator
17
{
18
19
    public function validateField(Structure $document, $fieldName, array $params)
20
    {
21
        $value = $document->get($fieldName);
22
        
23
        // check only if set
24
        if (!$value) {
25
            return;
26
        }
27
28
        // check if url valid
29
        $isValidUrl = (bool) filter_var($value, FILTER_VALIDATE_URL);
30
        if (!$isValidUrl) {
31
            if (!isset($params['message'])) {
32
                $params['message'] = 'Value of field "'
33
                    . $fieldName
34
                    . '" is not valid url in model '
35
                    . get_called_class();
36
            }
37
38
            $document->addError($fieldName, $this->getName(), $params['message']);
39
            return;
40
        }
41
        
42
        // ping not required - so url is valid
43
        if (empty($params['ping'])) {
44
            return;
45
        }
46
        
47
        // ping required
48
        $dnsRecordExists = dns_get_record(parse_url($value, PHP_URL_HOST));
49
50
        // network not allowed
51
        if ($dnsRecordExists === false) {
52
            throw new \RuntimeException('Error getting DNS record to validated url');
53
        }
54
55
        // empty array - host not found
56
        if (is_array($dnsRecordExists) && empty($dnsRecordExists)) {
57
            if (!isset($params['message'])) {
58
                $params['message'] = 'Value of field "'
59
                    . $fieldName
60
                    . '" is valid url but host is unreachable in model '
61
                    . get_called_class();
62
            }
63
64
            $document->addError($fieldName, $this->getName(), $params['message']);
65
            return;
66
        }
67
68
        if ($this->isUrlAccessible($value)) {
69
            return;
70
        }
71
        
72
        if (!isset($params['message'])) {
73
            $params['message'] = 'Value of field "'
74
                . $fieldName
75
                . '" is valid url but page not found '
76
                . get_called_class();
77
        }
78
79
        $document->addError($fieldName, $this->getName(), $params['message']);
80
    }
81
82
    private function isUrlAccessible($url)
83
    {
84
        $headers = get_headers($url, true);
85
86
        $isAccessible = false;
87
        $i = 0;
88
        while (true) {
89
            if (false !== strpos($headers[$i], '200')) {
90
                $isAccessible = true;
91
                break;
92
            }
93
94
            $i++;
95
            if (!isset($headers[$i])) {
96
                break;
97
            }
98
        }
99
100
        // page is accessible
101
        return $isAccessible;
102
    }
103
}
104