Completed
Pull Request — master (#18)
by Tobias
03:16
created

Json   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 51.28 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 20
loc 39
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encode() 10 10 2
A decode() 10 10 2

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
declare(strict_types=1);
4
5
namespace Gnumoksha\FreeIpa\Infra\Json;
6
7
use function json_decode;
8
use function json_encode;
9
10
/**
11
 * This class is a wrapper around php built-in functions json_encode and json_decode.
12
 *
13
 * It is useful because it throws an exception if the operation fails, as consequence the
14
 * returned values will not be null.
15
 *
16
 * @internal
17
 */
18
final class Json
19
{
20
    /**
21
     * Encode a value as json.
22
     *
23
     * @param mixed $value
24
     *
25
     * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException
26
     * @see \json_encode()
27
     */
28 View Code Duplication
    public static function encode($value, int $options = 0, int $depth = 512): string
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...
29
    {
30
        $encoded = json_encode($value, $options, $depth);
31
32
        if (json_last_error() !== JSON_ERROR_NONE) {
33
            throw new JsonException(sprintf('Unable to encode json. Error was: "%s".', json_last_error_msg()));
34
        }
35
36
        return (string)$encoded;
37
    }
38
39
    /**
40
     * Decode a value from json.
41
     *
42
     * @return mixed[]|\stdClass
43
     * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException
44
     * @see \json_decode()
45
     */
46 View Code Duplication
    public static function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
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...
47
    {
48
        $decoded = json_decode($json, $assoc, $depth, $options);
49
50
        if (json_last_error() !== JSON_ERROR_NONE) {
51
            throw new JsonException(sprintf('Unable to decode json. Error was: "%s".', json_last_error_msg()));
52
        }
53
54
        return $decoded;
55
    }
56
}
57