Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#838)
by Jens
04:03
created

Domain::validate()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.0368

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
rs 7.551
c 0
b 0
f 0
cc 7
eloc 11
nc 9
nop 1
crap 7.0368
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Respect\Validation\Rules;
13
14
use Respect\Validation\Exceptions\ValidationException;
15
16
class Domain extends AbstractComposite
17
{
18
    protected $tld;
19
    protected $checks = [];
20
    protected $otherParts;
21
22 30
    public function __construct($tldCheck = true)
23
    {
24 30
        $this->checks[] = new NoWhitespace();
25 30
        $this->checks[] = new Contains('.');
26 30
        $this->checks[] = new Length(3, null);
27 30
        $this->tldCheck($tldCheck);
28 30
        $this->otherParts = new AllOf(
29 30
            new Alnum('-'),
30 30
            new Not(new StartsWith('-')),
31 30
            new AnyOf(
32 30
                new Not(
33 30
                    new Contains('--')
34
                ),
35 30
                new AllOf(
36 30
                    new StartsWith('xn--'),
37 30
                    new Callback(function ($str) {
38 18
                        return mb_substr_count($str, '--') == 1;
39 30
                    })
40
                )
41
            )
42
        );
43 30
    }
44
45 30
    public function tldCheck($do = true)
46
    {
47 30
        if ($do === true) {
48 30
            $this->tld = new Tld();
49
        } else {
50 4
            $this->tld = new AllOf(
51 4
                new Not(
52 4
                    new StartsWith('-')
53
                ),
54 4
                new NoWhitespace(),
55 4
                new Length(2, null)
56
            );
57
        }
58
59 30
        return true;
60
    }
61
62 12
    public function validate($input)
63
    {
64 12
        foreach ($this->checks as $chk) {
65 12
            if (!$chk->validate($input)) {
66 12
                return false;
67
            }
68
        }
69
70 12
        if (count($parts = explode('.', $input)) < 2
71 12
            || !$this->tld->validate(array_pop($parts))) {
72
            return false;
73
        }
74
75 12
        foreach ($parts as $p) {
76 12
            if (!$this->otherParts->validate($p)) {
77 12
                return false;
78
            }
79
        }
80
81 12
        return true;
82
    }
83
84 15
    public function assert($input)
85
    {
86 15
        $e = [];
87 15
        foreach ($this->checks as $chk) {
88 15
            $this->collectAssertException($e, $chk, $input);
89
        }
90
91 15
        if (count($parts = explode('.', $input)) >= 2) {
92 13
            $this->collectAssertException($e, $this->tld, array_pop($parts));
93
        }
94
95 15
        foreach ($parts as $p) {
96 15
            $this->collectAssertException($e, $this->otherParts, $p);
97
        }
98
99 15
        if (count($e)) {
100 9
            throw $this->reportError($input)->setRelated($e);
101
        }
102
103 6
        return true;
104
    }
105
106 15
    protected function collectAssertException(&$exceptions, $validator, $input)
107
    {
108
        try {
109 15
            $validator->assert($input);
110 9
        } catch (ValidationException $e) {
111 9
            $exceptions[] = $e;
112
        }
113 15
    }
114
115 15
    public function check($input)
116
    {
117 15
        foreach ($this->checks as $chk) {
118 15
            $chk->check($input);
119
        }
120
121 13
        if (count($parts = explode('.', $input)) >= 2) {
122 13
            $this->tld->check(array_pop($parts));
123
        }
124
125 9
        foreach ($parts as $p) {
126 9
            $this->otherParts->check($p);
127
        }
128
129 6
        return true;
130
    }
131
}
132