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 (#825)
by
unknown
06:28
created

Domain   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 94.34%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 9
dl 0
loc 105
ccs 50
cts 53
cp 0.9434
rs 10
c 0
b 0
f 0

6 Methods

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