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   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 12
dl 0
loc 112
ccs 69
cts 72
cp 0.9583
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 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 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