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 (#678)
by Henrique
02:58
created

Sf::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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