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

Passed
Push — master ( df5cb1...166501 )
by Henrique
03:07
created

Zend::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
rs 10
cc 2
nc 2
nop 1
crap 2.0116
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 ReflectionClass;
17
use Respect\Validation\Exceptions\ComponentException;
18
use Respect\Validation\Exceptions\ValidationException;
19
use Respect\Validation\Exceptions\ZendException;
20
use Throwable;
21
use Zend\Validator\ValidatorInterface;
22
use function array_map;
23
use function current;
24
use function is_string;
25
use function sprintf;
26
use function stripos;
27
28
/**
29
 * Use Zend validators inside Respect\Validation flow.
30
 *
31
 * Messages are preserved.
32
 *
33
 * @author Alexandre Gomes Gaigalas <[email protected]>
34
 * @author Danilo Correa <[email protected]>
35
 * @author Henrique Moody <[email protected]>
36
 * @author Hugo Hamon <[email protected]>
37
 */
38
final class Zend extends AbstractRule
39
{
40
    /**
41
     * @var ValidatorInterface
42
     */
43
    private $zendValidator;
44
45
    /**
46
     * @param string|ValidatorInterface $validator
47
     * @param mixed[] $params
48
     *
49
     * @throws ComponentException
50
     */
51 8
    public function __construct($validator, array $params = [])
52
    {
53 8
        $this->zendValidator = $this->zendValidator($validator, $params);
54 1
    }
55
56
    /**
57
     * @param mixed $validator
58
     * @param mixed[] $params
59
     *
60
     * @throws ComponentException
61
     */
62 8
    private function zendValidator($validator, array $params = []): ValidatorInterface
63
    {
64 8
        if ($validator instanceof ValidatorInterface) {
65 1
            return $validator;
66
        }
67
68 8
        if (!is_string($validator)) {
69 5
            throw new ComponentException('The given argument is not a valid Zend Validator');
70
        }
71
72 5
        $className = stripos($validator, 'Zend') === false ? 'Zend\\Validator\\'.$validator : '\\'.$validator;
73
74
        try {
75 5
            $reflection = new ReflectionClass($className);
76 4
            if (!$reflection->isInstantiable()) {
77 1
                throw new ComponentException(sprintf('"%s" is not instantiable', $className));
78
            }
79
80 3
            return $this->zendValidator($reflection->newInstanceArgs($params));
81 4
        } catch (Throwable $exception) {
82 4
            throw new ComponentException(sprintf('Could not create "%s"', $validator), 0, $exception);
83
        }
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function assert($input): void
90
    {
91 1
        $validator = clone $this->zendValidator;
92 1
        if ($validator->isValid($input)) {
93
            return;
94
        }
95
96
        /** @var ZendException $zendException */
97 1
        $zendException = $this->reportError($input);
98 1
        $zendException->addChildren(
99 1
            array_map(
100
                function (string $message) use ($input): ValidationException {
101 1
                    $exception = $this->reportError($input);
102 1
                    $exception->updateTemplate($message);
103
104 1
                    return $exception;
105 1
                },
106 1
                $validator->getMessages()
107
            )
108
        );
109
110 1
        throw $zendException;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 1
    public function check($input): void
117
    {
118 1
        $validator = clone $this->zendValidator;
119 1
        if ($validator->isValid($input)) {
120
            return;
121
        }
122
123
        /** @var ZendException $zendException */
124 1
        $zendException = $this->reportError($input);
125 1
        $zendException->updateTemplate(current($validator->getMessages()));
126
127 1
        throw $zendException;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 8
    public function validate($input): bool
134
    {
135 8
        return (clone $this->zendValidator)->isValid($input);
136
    }
137
}
138