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 (#1218)
by Henrique
03:28
created

Domain::createTldRule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.7462

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 3
cts 7
cp 0.4286
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.7462
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\DomainException;
17
use Respect\Validation\Exceptions\NestedValidationException;
18
use Respect\Validation\Exceptions\ValidationException;
19
use Respect\Validation\Validatable;
20
use function array_merge;
21
use function array_pop;
22
use function count;
23
use function explode;
24
use function iterator_to_array;
25
use function mb_substr_count;
26
27
/**
28
 * Validates whether the input is a valid domain name or not.
29
 *
30
 * @author Alexandre Gomes Gaigalas <[email protected]>
31
 * @author Henrique Moody <[email protected]>
32
 * @author Mehmet Tolga Avcioglu <[email protected]>
33
 * @author Nick Lombard <[email protected]>
34
 * @author Róbert Nagy <[email protected]>
35
 */
36
final class Domain extends AbstractRule
37
{
38
    /**
39
     * @var Validatable
40
     */
41
    private $genericRule;
42
43
    /**
44
     * @var Validatable
45
     */
46
    private $tldRule;
47
48
    /**
49
     * @var Validatable
50
     */
51
    private $partsRule;
52
53 1
    public function __construct(bool $tldCheck = true)
54
    {
55 1
        $this->genericRule = $this->createGenericRule();
56 1
        $this->tldRule = $this->createTldRule($tldCheck);
57 1
        $this->partsRule = $this->createPartsRule();
58 1
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 23
    public function assert($input): void
64
    {
65 23
        $exceptions = [];
66
67 23
        $this->collectAssertException($exceptions, $this->genericRule, $input);
68 23
        $this->throwExceptions($exceptions, $input);
69
70 16
        $parts = explode('.', (string) $input);
71 16
        if (count($parts) >= 2) {
72 16
            $this->collectAssertException($exceptions, $this->tldRule, array_pop($parts));
73
        }
74
75 16
        foreach ($parts as $part) {
76 16
            $this->collectAssertException($exceptions, $this->partsRule, $part);
77
        }
78
79 16
        $this->throwExceptions($exceptions, $input);
80 9
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 22
    public function validate($input): bool
86
    {
87
        try {
88 22
            $this->assert($input);
89 13
        } catch (ValidationException $exception) {
90 13
            return false;
91
        }
92
93 9
        return true;
94
    }
95
96
    /**
97
     * @param ValidationException[] $exceptions
98
     * @param mixed $input
99
     */
100 23
    private function collectAssertException(array &$exceptions, Validatable $validator, $input): void
101
    {
102
        try {
103 23
            $validator->assert($input);
104 14
        } catch (NestedValidationException $nestedValidationException) {
105 10
            $exceptions = array_merge(
106 10
                $exceptions,
107 10
                iterator_to_array($nestedValidationException)
108
            );
109 4
        } catch (ValidationException $validationException) {
110 4
            $exceptions[] = $validationException;
111
        }
112 23
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 1
    public function check($input): void
118
    {
119
        try {
120 1
            $this->assert($input);
121 1
        } catch (NestedValidationException $exception) {
122
            /** @var ValidationException $childException */
123 1
            $childException = $exception->getIterator()->current();
124
125 1
            throw $childException;
126
        }
127
    }
128
129 1
    private function createGenericRule(): Validatable
130
    {
131 1
        return new AllOf(
132 1
            new StringType(),
133 1
            new NoWhitespace(),
134 1
            new Contains('.'),
135 1
            new Length(3)
136
        );
137
    }
138
139 1
    private function createTldRule(bool $realTldCheck): Validatable
140
    {
141 1
        if ($realTldCheck) {
142 1
            return new Tld();
143
        }
144
145
        return new AllOf(
146
            new Not(new StartsWith('-')),
147
            new NoWhitespace(),
148
            new Length(2)
149
        );
150
    }
151
152 16
    private function createPartsRule(): Validatable
153
    {
154 1
        return new AllOf(
155 1
            new Alnum('-'),
156 1
            new Not(new StartsWith('-')),
157 1
            new AnyOf(
158 1
                new Not(new Contains('--')),
159
                new Callback(static function ($str) {
160 16
                    return mb_substr_count($str, '--') == 1;
161 1
                })
162
            ),
163 1
            new Not(new EndsWith('-'))
164
        );
165
    }
166
167
    /**
168
     * @param ValidationException[] $exceptions
169
     * @param mixed $input
170
     */
171 23
    private function throwExceptions(array $exceptions, $input): void
172
    {
173 23
        if (count($exceptions)) {
174
            /** @var DomainException $domainException */
175 14
            $domainException = $this->reportError($input);
176 14
            $domainException->addChildren($exceptions);
177
178 14
            throw $domainException;
179
        }
180 16
    }
181
}
182