SymfonyLoginValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace OpenTribes\Core\Silex\Validator;
4
5
6
use OpenTribes\Core\Validator\LoginValidator;
7
use Symfony\Component\Validator\ConstraintViolationList;
8
use Symfony\Component\Validator\Validator;
9
use Symfony\Component\Validator\Constraints;
10
11
12
class SymfonyLoginValidator extends LoginValidator
13
{
14
    /**
15
     * @var Validator
16
     */
17
    private $validator;
18
19 10
    public function __construct(Validator\RecursiveValidator $validator)
20
    {
21 10
        $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...
22 10
    }
23
24 8
    protected function validate()
25
    {
26 8
        $constraint = new Constraints\Collection(
27
            [
28
29
                'username' => [
30 8
                    new Constraints\NotBlank(array('message' => 'Username is empty')),
31 8
                    new Constraints\Length(array(
32 8
                        'min' => 3,
33 8
                        'max' => 20,
34 8
                        'minMessage' => 'Username is too short',
35
                        'maxMessage' => 'Username is too long'
36 8
                    )),
37 8
                    new Constraints\Regex(array(
38 8
                        'pattern' => '/^[-a-z0-9_]++$/iD',
39
                        'message' => 'Username contains invalid character'
40 8
                    ))
41 8
                ]
42 8
                ,
43
                'password' => [
44 8
                    new Constraints\NotBlank(array('message' => 'Password is empty')),
45 8
                    new Constraints\Length(array('min' => 6, 'minMessage' => 'Password is too short')),
46
47 8
                ],
48
                'verified' => [
49 8
                    new Constraints\IsTrue(['message' => 'Invalid login'])
50 8
                ]
51 8
            ]);
52
53
        $value = [
54 8
            'username' => $this->username,
55 8
            'password' => $this->password,
56 8
            'verified' => $this->verified
57 8
        ];
58 8
        $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...
59 8
        if ($result instanceof ConstraintViolationList) {
60 8
            foreach ($result->getIterator() as $constraintViolation) {
61 7
                $this->addError($constraintViolation->getMessage());
62 8
            }
63 8
        }
64
65 8
    }
66
67
}