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 (#923)
by lee
03:57
created

Sf::assert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
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.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 ReflectionException;
18
use Respect\Validation\Exceptions\ComponentException;
19
use Symfony\Component\Validator\Constraint;
20
use Symfony\Component\Validator\Validation;
21
22
class Sf extends AbstractRule
23
{
24
    const SYMFONY_CONSTRAINT_NAMESPACE = 'Symfony\Component\Validator\Constraints\%s';
25
    public $name;
26
    private $constraint;
27
28 4
    public function __construct($name, array $params = [])
29
    {
30 4
        $this->name = ucfirst($name);
31 4
        $this->constraint = $this->createSymfonyConstraint($this->name, $params);
32 3
    }
33
34 4
    private function createSymfonyConstraint($constraintName, array $constraintConstructorParameters = [])
35
    {
36 4
        $fullClassName = sprintf(self::SYMFONY_CONSTRAINT_NAMESPACE, $constraintName);
37
        try {
38 4
            $constraintReflection = new ReflectionClass($fullClassName);
39 1
        } catch (ReflectionException $previousException) {
40 1
            $baseExceptionMessage = 'Symfony/Validator constraint "%s" does not exist.';
41 1
            $exceptionMessage = sprintf($baseExceptionMessage, $constraintName);
42 1
            throw new ComponentException($exceptionMessage, 0, $previousException);
43
        }
44 3
        if ($constraintReflection->hasMethod('__construct')) {
45 3
            return $constraintReflection->newInstanceArgs($constraintConstructorParameters);
46
        }
47
48
        return $constraintReflection->newInstance();
49
    }
50
51 3
    private function returnViolationsForConstraint($valueToValidate, Constraint $symfonyConstraint)
52
    {
53 3
        $validator = Validation::createValidator(); // You gotta love those Symfony namings
54
55 3
        return $validator->validate($valueToValidate, $symfonyConstraint);
56
    }
57
58 2
    public function assert($input): void
59
    {
60 2
        $violations = $this->returnViolationsForConstraint($input, $this->constraint);
61 2
        if (0 == count($violations)) {
62 1
            return;
63
        }
64
65 1
        throw $this->reportError((string) $violations);
66
    }
67
68 1
    public function validate($input): bool
69
    {
70 1
        $violations = $this->returnViolationsForConstraint($input, $this->constraint);
71 1
        if (count($violations)) {
72 1
            return false;
73
        }
74
75 1
        return true;
76
    }
77
}
78