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
Push — 1.1 ( a00928...120d92 )
by Henrique
04:43 queued 01:40
created

Domain::validate()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.9936

Importance

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