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 (#923)
by lee
03:57
created

Domain::validate()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 10
nc 9
nop 1
crap 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
class Domain extends AbstractComposite
19
{
20
    protected $tld;
21
    protected $checks = [];
22
    protected $otherParts;
23
24 45
    public function __construct($tldCheck = true)
25
    {
26 45
        $this->checks[] = new NoWhitespace();
27 45
        $this->checks[] = new Contains('.');
28 45
        $this->checks[] = new Length(3, null);
29 45
        $this->tldCheck($tldCheck);
30 45
        $this->otherParts = new AllOf(
31 45
            new Alnum('-'),
32 45
            new Not(new StartsWith('-')),
33 45
            new AnyOf(
34 45
                new Not(new Contains('--')),
35 45
                new Callback(function ($str) {
36 25
                    return 1 == mb_substr_count($str, '--');
37 45
                })
38
            ),
39 45
            new Not(new EndsWith('-'))
40
        );
41 45
    }
42
43 45
    public function tldCheck($do = true)
44
    {
45 45
        if (true === $do) {
46 45
            $this->tld = new Tld();
47
        } else {
48 4
            $this->tld = new AllOf(
49 4
                new Not(
50 4
                    new StartsWith('-')
51
                ),
52 4
                new NoWhitespace(),
53 4
                new Length(2, null)
54
            );
55
        }
56
57 45
        return true;
58
    }
59
60 27
    public function validate($input): bool
61
    {
62 27
        foreach ($this->checks as $chk) {
63 27
            if (!$chk->validate($input)) {
64 27
                return false;
65
            }
66
        }
67
68 25
        if (count($parts = explode('.', (string) $input)) < 2
69 25
            || !$this->tld->validate(array_pop($parts))) {
70 4
            return false;
71
        }
72
73 21
        foreach ($parts as $p) {
74 21
            if (!$this->otherParts->validate($p)) {
75 21
                return false;
76
            }
77
        }
78
79 18
        return true;
80
    }
81
82 18
    public function assert($input): void
83
    {
84 18
        $e = [];
85 18
        foreach ($this->checks as $chk) {
86 18
            $this->collectAssertException($e, $chk, $input);
87
        }
88
89 18
        if (count($parts = explode('.', (string) $input)) >= 2) {
90 16
            $this->collectAssertException($e, $this->tld, array_pop($parts));
91
        }
92
93 18
        foreach ($parts as $p) {
94 18
            $this->collectAssertException($e, $this->otherParts, $p);
95
        }
96
97 18
        if (count($e)) {
98 9
            throw $this->reportError($input)->setRelated($e);
0 ignored issues
show
Bug introduced by
The method setRelated() 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

98
            throw $this->reportError($input)->/** @scrutinizer ignore-call */ setRelated($e);
Loading history...
99
        }
100 9
    }
101
102 18
    protected function collectAssertException(&$exceptions, $validator, $input): void
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): void
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 9
    }
125
}
126