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
Push — master ( 314542...f7a57a )
by Henrique
03:23
created

Zend::zendValidator()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.0671

Importance

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