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 (#1181)
by mazen
04:32
created

Domain   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 94.83%

Importance

Changes 0
Metric Value
wmc 21
eloc 55
dl 0
loc 107
ccs 55
cts 58
cp 0.9483
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 12 4
A assert() 0 17 5
A tldCheck() 0 15 2
A collectAssertException() 0 6 2
A __construct() 0 19 1
B validate() 0 20 7
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
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use Respect\Validation\Exceptions\ValidationException;
17
18
/**
19
 * @author Alexandre Gomes Gaigalas <[email protected]>
20
 * @author Henrique Moody <[email protected]>
21
 * @author Mehmet Tolga Avcioglu <[email protected]>
22
 * @author Nick Lombard <[email protected]>
23
 * @author Róbert Nagy <[email protected]>
24
 */
25
class Domain extends AbstractComposite
26
{
27
    protected $tld;
28
    protected $checks = [];
29
    protected $otherParts;
30
31 36
    public function __construct($tldCheck = true)
32
    {
33 36
        $this->checks[] = new NoWhitespace();
34 36
        $this->checks[] = new Contains('.');
35 36
        $this->checks[] = new Length(3, null);
36 36
        $this->tldCheck($tldCheck);
37 36
        $this->otherParts = new AllOf(
38 36
            new Alnum('-'),
39 36
            new Not(new StartsWith('-')),
40 36
            new AnyOf(
41 36
                new Not(new Contains('--')),
42
                new Callback(function ($str) {
43 24
                    return 1 == mb_substr_count($str, '--');
44 36
                })
45
            ),
46 36
            new Not(new EndsWith('-'))
47
        );
48
49 36
        parent::__construct();
50 36
    }
51
52 36
    public function tldCheck($do = true)
53
    {
54 36
        if (true === $do) {
55 36
            $this->tld = new Tld();
56
        } else {
57 4
            $this->tld = new AllOf(
58 4
                new Not(
59 4
                    new StartsWith('-')
60
                ),
61 4
                new NoWhitespace(),
62 4
                new Length(2, null)
63
            );
64
        }
65
66 36
        return true;
67
    }
68
69 18
    public function validate($input): bool
70
    {
71 18
        foreach ($this->checks as $chk) {
72 18
            if (!$chk->validate($input)) {
73
                return false;
74
            }
75
        }
76
77 18
        if (count($parts = explode('.', (string) $input)) < 2
78 18
            || !$this->tld->validate(array_pop($parts))) {
79
            return false;
80
        }
81
82 18
        foreach ($parts as $p) {
83 18
            if (!$this->otherParts->validate($p)) {
84
                return false;
85
            }
86
        }
87
88 18
        return true;
89
    }
90
91 18
    public function assert($input): void
92
    {
93 18
        $e = [];
94 18
        foreach ($this->checks as $chk) {
95 18
            $this->collectAssertException($e, $chk, $input);
96
        }
97
98 18
        if (count($parts = explode('.', (string) $input)) >= 2) {
99 16
            $this->collectAssertException($e, $this->tld, array_pop($parts));
100
        }
101
102 18
        foreach ($parts as $p) {
103 18
            $this->collectAssertException($e, $this->otherParts, $p);
104
        }
105
106 18
        if (count($e)) {
107 9
            throw $this->reportError($input)->addChildren($e);
0 ignored issues
show
Bug introduced by
The method addChildren() does not exist on Respect\Validation\Exceptions\ValidationException. It seems like you code against a sub-type of Respect\Validation\Exceptions\ValidationException such as Respect\Validation\Excep...stedValidationException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
            throw $this->reportError($input)->/** @scrutinizer ignore-call */ addChildren($e);
Loading history...
108
        }
109 9
    }
110
111 18
    protected function collectAssertException(&$exceptions, $validator, $input): void
112
    {
113
        try {
114 18
            $validator->assert($input);
115 9
        } catch (ValidationException $e) {
116 9
            $exceptions[] = $e;
117
        }
118 18
    }
119
120 18
    public function check($input): void
121
    {
122 18
        foreach ($this->checks as $chk) {
123 18
            $chk->check($input);
124
        }
125
126 16
        if (count($parts = explode('.', $input)) >= 2) {
127 16
            $this->tld->check(array_pop($parts));
128
        }
129
130 12
        foreach ($parts as $p) {
131 12
            $this->otherParts->check($p);
132
        }
133 9
    }
134
}
135