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 (#1077)
by Henrique
04:07 queued 01:48
created

Sf::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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\SfException;
17
use Respect\Validation\Exceptions\ValidationException;
18
use Symfony\Component\Validator\Constraint;
19
use Symfony\Component\Validator\Validation;
20
use Symfony\Component\Validator\Validator\ValidatorInterface;
21
22
/**
23
 * Validate the input with a Symfony Validator (>=4.0 or >=3.0) Constraint.
24
 *
25
 * @author Alexandre Gomes Gaigalas <[email protected]>
26
 * @author Augusto Pascutti <[email protected]>
27
 * @author Henrique Moody <[email protected]>
28
 */
29
final class Sf extends AbstractRule
30
{
31
    /**
32
     * @var Constraint
33
     */
34
    private $constraint;
35
36
    /**
37
     * @var ValidatorInterface
38
     */
39
    private $validator;
40
41
    /**
42
     * Initializes the rule with the Constraint and the Validator.
43
     *
44
     * In the the Validator is not defined, tries to create one.
45
     *
46
     * @param Constraint $constraint
47
     * @param ValidatorInterface|null $validator
48
     */
49 5
    public function __construct(Constraint $constraint, ValidatorInterface $validator = null)
50
    {
51 5
        $this->constraint = $constraint;
52 5
        $this->validator = $validator ?: Validation::createValidator();
53 5
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 4
    public function assert($input): void
59
    {
60 4
        $violations = $this->validator->validate($input, $this->constraint);
61 4
        if (0 === $violations->count()) {
62 2
            return;
63
        }
64
65 3
        if (1 === $violations->count()) {
66 3
            throw $this->reportError($input, ['violations' => $violations[0]->getMessage()]);
67
        }
68
69 1
        throw $this->reportError($input, ['violations' => trim((string) $violations)]);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 3
    public function reportError($input, array $extraParams = []): ValidationException
76
    {
77 3
        $exception = parent::reportError($input, $extraParams);
78 3
        if (isset($extraParams['violations'])) {
79 3
            $exception->updateTemplate($extraParams['violations']);
80
        }
81
82 3
        return $exception;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 4
    public function validate($input): bool
89
    {
90
        try {
91 4
            $this->assert($input);
92 2
        } catch (SfException $exception) {
93 2
            return false;
94
        }
95
96 2
        return true;
97
    }
98
}
99