getJsonErrorMessage()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ClientDataConstraintValidator extends ConstraintValidator
26
{
27
    const STRICT = true;
28
29
    public function validate($value, Constraint $constraint)
30
    {
31
        if (!$constraint instanceof ClientDataConstraint) {
32
            throw new LogicException('ClientDataConstraintValidator can only validate ClientDataConstraints');
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
        $clientData = json_decode($decoded);
48
        $jsonLastError = json_last_error();
49
50
        if ($jsonLastError) {
51
            $jsonErrorReason = $this->getJsonErrorMessage($jsonLastError);
52
            $this->context->addViolation($constraint->message, ['%reason%' => $jsonErrorReason], $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...
53
            return;
54
        }
55
56
        if (!isset($clientData->challenge)) {
57
            $this->context->addViolation(
58
                $constraint->message,
59
                ['%reason%' => 'Challenge not present on client data'],
60
                $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...
61
            );
62
            return;
63
        }
64
    }
65
66
    /**
67
     * @param int $errorCode
68
     * @return string
69
     *
70
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
71
     */
72
    private function getJsonErrorMessage($errorCode)
73
    {
74
        switch ($errorCode) {
75
            case JSON_ERROR_NONE:
76
                return 'JSON_ERROR_NONE';
77
            case JSON_ERROR_DEPTH:
78
                return 'JSON_ERROR_DEPTH';
79
            case JSON_ERROR_STATE_MISMATCH:
80
                return 'JSON_ERROR_STATE_MISMATCH';
81
            case JSON_ERROR_CTRL_CHAR:
82
                return 'JSON_ERROR_CTRL_CHAR';
83
            case JSON_ERROR_SYNTAX:
84
                return 'JSON_ERROR_SYNTAX';
85
            case JSON_ERROR_UTF8:
86
                return 'JSON_ERROR_UTF8';
87
            case constant('JSON_ERROR_RECURSION'):
88
                return 'JSON_ERROR_RECURSION';
89
            case constant('JSON_ERROR_INF_OR_NAN'):
90
                return 'JSON_ERROR_INF_OR_NAN';
91
            case constant('JSON_ERROR_UNSUPPORTED_TYPE'):
92
                return 'JSON_ERROR_UNSUPPORTED_TYPE';
93
            default:
94
                throw new LogicException(sprintf('Unknown JSON decoding error code %d encountered', $errorCode));
95
        }
96
    }
97
}
98