JSONEncoder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 80 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 32
loc 40
ccs 12
cts 18
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A encode() 16 16 3
A decode() 16 16 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
namespace CouchDB\Encoder;
4
5
use CouchDB\Exception\JsonDecodeException;
6
use CouchDB\Exception\JsonEncodeException;
7
8
/**
9
 * @author Markus Bachmann <[email protected]>
10
 */
11
final class JSONEncoder
12
{
13
    private function __construct()
14
    {
15
    }
16
17 9 View Code Duplication
    public static function encode($value)
18
    {
19
        set_error_handler(function () use ($value) {
20
            throw new JsonEncodeException($value);
21 9
        });
22
23 9
        $json = json_encode($value);
24
25 9
        if ('null' === $json && $value !== null) {
26
            throw new JsonEncodeException($value);
27
        }
28
29 9
        restore_error_handler();
30
31 9
        return $json;
32
    }
33
34 View Code Duplication
    public static function decode($json)
35
    {
36 16
        set_error_handler(function () use ($json) {
37
            throw new JsonDecodeException($json);
38 16
        });
39
40 16
        $value = json_decode($json, true);
41
42 16
        if (false === $value) {
43
            throw new JsonDecodeException($json);
44
        }
45
46 16
        restore_error_handler();
47
48 16
        return $value;
49
    }
50
}
51