Completed
Push — master ( a12164...2a5888 )
by Arman
16s queued 12s
created

Resource   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 3 1
A ipv4() 0 3 1
A urlExists() 0 16 5
A ipv6() 0 3 1
A ip() 0 3 1
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.8
13
 */
14
15
namespace Quantum\Libraries\Validation\Traits;
16
17
/**
18
 * Trait Resource
19
 * @package Quantum\Libraries\Validation\Rules
20
 */
21
trait Resource
22
{
23
24
    /**
25
     * Checks for valid URL or subdomain
26
     * @param string $field
27
     * @param string $value
28
     * @return bool
29
     */
30
    protected function url(string $field, string $value): bool
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

30
    protected function url(/** @scrutinizer ignore-unused */ string $field, string $value): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        return filter_var($value, FILTER_VALIDATE_URL) !== false;
33
    }
34
35
    /**
36
     * Checks to see if the url exists
37
     * @param string $field
38
     * @param string $value
39
     * @return bool
40
     */
41
    protected function urlExists(string $field, string $value): bool
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

41
    protected function urlExists(/** @scrutinizer ignore-unused */ string $field, string $value): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        if (filter_var($value, FILTER_VALIDATE_URL) === false) {
44
            return false;
45
        }
46
47
        $host = parse_url(strtolower($value), PHP_URL_HOST);
48
49
        if (!$host) {
50
            return false;
51
        }
52
53
        if (function_exists('checkdnsrr') && function_exists('idn_to_ascii')) {
54
            return checkdnsrr(idn_to_ascii($host, 0, INTL_IDNA_VARIANT_UTS46), 'A');
55
        } else {
56
            return gethostbyname($host) !== $host;
57
        }
58
    }
59
60
    /**
61
     * Checks for valid IP address
62
     * @param string $field
63
     * @param string $value
64
     * @return bool
65
     */
66
    protected function ip(string $field, string $value): bool
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

66
    protected function ip(/** @scrutinizer ignore-unused */ string $field, string $value): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
        return filter_var($value, FILTER_VALIDATE_IP) !== false;
69
    }
70
71
    /**
72
     * Checks for valid IPv4 address
73
     * @param string $field
74
     * @param string $value
75
     * @return bool
76
     */
77
    protected function ipv4(string $field, string $value): bool
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

77
    protected function ipv4(/** @scrutinizer ignore-unused */ string $field, string $value): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
80
    }
81
82
    /**
83
     * Checks for valid IPv6 address
84
     * @param string $field
85
     * @param string $value
86
     * @return bool
87
     */
88
    protected function ipv6(string $field, string $value): bool
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

88
    protected function ipv6(/** @scrutinizer ignore-unused */ string $field, string $value): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
91
    }
92
}