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 ( fe7fed...1da164 )
by Henrique
05:48
created

Sf::reportError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

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