SignatureDataConstraintValidator::validate()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 0
Metric Value
dl 8
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 5
nop 2
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupU2fBundle\Validator\Constraints;
20
21
use Surfnet\StepupU2fBundle\Exception\LogicException;
22
use Symfony\Component\Validator\Constraint;
23
use Symfony\Component\Validator\ConstraintValidator;
24
25
final class SignatureDataConstraintValidator extends ConstraintValidator
26
{
27
    const STRICT = true;
28
29
    public function validate($value, Constraint $constraint)
30
    {
31
        if (!$constraint instanceof SignatureDataConstraint) {
32
            throw new LogicException('SignatureDataConstraintValidator can only validate SignatureDataConstraints');
33
        }
34
35
        if (!is_string($value)) {
36
            $this->context->addViolation($constraint->message, ['%reason%' => 'Not a string'], $value);
0 ignored issues
show
Unused Code introduced by
The call to ExecutionContextInterface::addViolation() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
37
            return;
38
        }
39
40
        $decoded = base64_decode(strtr($value, '-_', '+/'), self::STRICT);
41
42 View Code Duplication
        if ($decoded === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
            $this->context->addViolation($constraint->message, ['%reason%' => 'Not base64 decodable'], $value);
0 ignored issues
show
Unused Code introduced by
The call to ExecutionContextInterface::addViolation() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
44
            return;
45
        }
46
47
        // See yubico/u2flib-server/src/u2flib_server/U2F.php:290
48 View Code Duplication
        if (strlen($decoded) < 6) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            $this->context->addViolation($constraint->message, ['%reason%' => 'Too short'], $value);
0 ignored issues
show
Unused Code introduced by
The call to ExecutionContextInterface::addViolation() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
50
            return;
51
        }
52
    }
53
}
54