SymfonyRegistrationValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 4
c 4
b 1
f 1
lcom 1
cbo 10
dl 0
loc 69
rs 10
ccs 45
cts 45
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 55 3
1
<?php
2
3
namespace OpenTribes\Core\Silex\Validator;
4
5
use OpenTribes\Core\Validator\RegistrationValidator;
6
use Symfony\Component\Validator\ConstraintViolationList;
7
use Symfony\Component\Validator\Validator;
8
use Symfony\Component\Validator\Constraints;
9
10
class SymfonyRegistrationValidator extends RegistrationValidator
11
{
12
    /**
13
     * @var Validator
14
     */
15
    private $validator;
16
17 15
    public function __construct(Validator\RecursiveValidator $validator)
18
    {
19 15
        $this->validator = $validator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $validator of type object<Symfony\Component...tor\RecursiveValidator> is incompatible with the declared type object<Symfony\Component\Validator\Validator> of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
20 15
    }
21
22 14
    protected function validate()
23
    {
24 14
        $constraint = new Constraints\Collection(
25
            [
26
27 14
                'isUniqueEmail' => new Constraints\IsTrue(['message' => 'Email exists']),
28 14
                'isUniqueUsername' => new Constraints\IsTrue(['message' => 'Username exists']),
29 14
                'termsAndConditions' => new Constraints\IsTrue(['message' => 'Terms and Conditions are not accepted']),
30
                'username' => [
31 14
                    new Constraints\NotBlank(['message' => 'Username is empty']),
32 14
                    new Constraints\Length([
33 14
                        'min' => 3,
34 14
                        'max' => 20,
35 14
                        'minMessage' => 'Username is too short',
36
                        'maxMessage' => 'Username is too long'
37 14
                    ]),
38 14
                    new Constraints\Regex([
39 14
                        'pattern' => '/^[-a-z0-9_]++$/iD',
40
                        'message' => 'Username contains invalid character'
41 14
                    ])
42 14
                ],
43
                'email' => [
44 14
                    new Constraints\NotBlank(['message' => 'Email is empty']),
45 14
                    new Constraints\Email(['message' => 'Email is invalid'])
46 14
                ],
47
                'password' => [
48 14
                    new Constraints\NotBlank(['message' => 'Password is empty']),
49 14
                    new Constraints\Length(['min' => 6, 'minMessage' => 'Password is too short']),
50 14
                ],
51
                'passwordConfirm' => [
52 14
                    new Constraints\EqualTo(['value' => $this->password, 'message' => 'Password confirm not match'])
53 14
                ],
54
                'emailConfirm' => [
55 14
                    new Constraints\EqualTo(['value' => $this->email, 'message' => 'Email confirm not match'])
56 14
                ]
57 14
            ]);
58
59
        $value = [
60 14
            'username' => $this->username,
61 14
            'password' => $this->password,
62 14
            'isUniqueEmail' => !$this->emailExists,
63 14
            'isUniqueUsername' => !$this->usernameExists,
64 14
            'termsAndConditions' => $this->acceptedTerms,
65 14
            'email' => $this->email,
66 14
            'passwordConfirm' => $this->passwordConfirm,
67 14
            'emailConfirm' => $this->emailConfirm
68 14
        ];
69 14
        $result = $this->validator->validate($value, $constraint);
0 ignored issues
show
Documentation introduced by
$constraint is of type object<Symfony\Component...Constraints\Collection>, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70 14
        if ($result instanceof ConstraintViolationList) {
71 14
            foreach ($result->getIterator() as $constraintViolation) {
72 13
                $this->addError($constraintViolation->getMessage());
73 14
            }
74 14
        }
75
76 14
    }
77
78
}