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 — 1.0 (#871)
by Henrique
03:26
created

Domain::collectAssertException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
crap 2
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 34
    public function __construct($tldCheck = true)
23
    {
24 34
        $this->checks[] = new NoWhitespace();
25 34
        $this->checks[] = new Contains('.');
26 34
        $this->checks[] = new Length(3, null);
27 34
        $this->tldCheck($tldCheck);
28 34
        $this->otherParts = new AllOf(
29 34
            new Alnum('-'),
30 34
            new Not(new StartsWith('-')),
31 34
            new OneOf(
32 34
                new Not(new Contains('--')),
33 34
                new Callback(function ($str) {
34 22
                    return substr_count($str, '--') == 1;
35 34
                })
36 34
            ),
37 34
            new Not(new EndsWith('-'))
38 34
        );
39 34
    }
40
41 34
    public function tldCheck($do = true)
42
    {
43 34
        if ($do === true) {
44 34
            $this->tld = new Tld();
45 34
        } else {
46 4
            $this->tld = new AllOf(
47 4
                new Not(
48 4
                    new StartsWith('-')
49 4
                ),
50 4
                new NoWhitespace(),
51 4
                new Length(2, null)
52 4
            );
53
        }
54
55 34
        return true;
56
    }
57
58 16
    public function validate($input)
59
    {
60 16
        foreach ($this->checks as $chk) {
61 16
            if (!$chk->validate($input)) {
62
                return false;
63
            }
64 16
        }
65
66 16
        if (count($parts = explode('.', $input)) < 2
67 16
            || !$this->tld->validate(array_pop($parts))) {
68
            return false;
69
        }
70
71 16
        foreach ($parts as $p) {
72 16
            if (!$this->otherParts->validate($p)) {
73
                return false;
74
            }
75 16
        }
76
77 16
        return true;
78
    }
79
80 17
    public function assert($input)
81
    {
82 17
        $e = [];
83 17
        foreach ($this->checks as $chk) {
84 17
            $this->collectAssertException($e, $chk, $input);
85 17
        }
86
87 17
        if (count($parts = explode('.', $input)) >= 2) {
88 15
            $this->collectAssertException($e, $this->tld, array_pop($parts));
89 15
        }
90
91 17
        foreach ($parts as $p) {
92 17
            $this->collectAssertException($e, $this->otherParts, $p);
93 17
        }
94
95 17
        if (count($e)) {
96 9
            throw $this->reportError($input)->setRelated($e);
97
        }
98
99 8
        return true;
100
    }
101
102 17
    protected function collectAssertException(&$exceptions, $validator, $input)
103
    {
104
        try {
105 17
            $validator->assert($input);
106 17
        } catch (ValidationException $e) {
107 9
            $exceptions[] = $e;
108
        }
109 17
    }
110
111 17
    public function check($input)
112
    {
113 17
        foreach ($this->checks as $chk) {
114 17
            $chk->check($input);
115 17
        }
116
117 15
        if (count($parts = explode('.', $input)) >= 2) {
118 15
            $this->tld->check(array_pop($parts));
119 11
        }
120
121 11
        foreach ($parts as $p) {
122 11
            $this->otherParts->check($p);
123 9
        }
124
125 8
        return true;
126
    }
127
}
128