Completed
Push — master ( a2a7f2...f21cde )
by John
9s
created

Encoder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 18.87 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 0
dl 10
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A encode() 0 4 1
A jsonEncode() 10 10 2
A base64Encode() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the KleijnWeb\JwtBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\JwtBundle\Authenticator;
9
10
class Encoder
11
{
12
    /**
13
     * @var array
14
     */
15
    private static $messages = [
16
        JSON_ERROR_NONE           => 'No error',
17
        JSON_ERROR_DEPTH          => 'Maximum stack depth exceeded',
18
        JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
19
        JSON_ERROR_CTRL_CHAR      => 'Control character error, possibly incorrectly encoded',
20
        JSON_ERROR_SYNTAX         => 'Syntax error',
21
        JSON_ERROR_UTF8           => 'Malformed UTF-8 characters, possibly incorrectly encoded'
22
    ];
23
24
    /**
25
     * @param array $base64Decoded
26
     *
27
     * @return string
28
     */
29
    public function encode($base64Decoded)
30
    {
31
        return $this->base64Encode($this->jsonEncode($base64Decoded));
32
    }
33
34
    /**
35
     * @param array $data
36
     *
37
     * @return string
38
     */
39 View Code Duplication
    public function jsonEncode($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
40
    {
41
        $plain = json_encode($data);
42
43
        if (json_last_error() != JSON_ERROR_NONE) {
44
            throw new \RuntimeException(self::$messages[json_last_error()]);
45
        }
46
47
        return $plain;
48
    }
49
50
    /**
51
     * @param string $base64Decoded
52
     *
53
     * @return string
54
     */
55
    public function base64Encode($base64Decoded)
56
    {
57
        $base64Decoded = base64_encode($base64Decoded);
58
        $base64Decoded = rtrim(strtr($base64Decoded, '-_', '+/'), '=');
59
60
        return $base64Decoded;
61
    }
62
}
63