Completed
Pull Request — master (#1)
by Claude
02:42
created

Json::decode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 6
nc 2
nop 4
crap 2.0116
1
<?php
2
3
namespace Puzzle\Pieces;
4
5
class Json
6
{
7 5
    public static function decode($json, $assoc = false, $depth = 512, $option = 0)
8
    {
9 5
        $decodedJson = json_decode($json, $assoc, $depth, $option);
10
11 5
        $jsonLastError = json_last_error();
12 5
        if($jsonLastError != JSON_ERROR_NONE)
13 5
        {
14 5
            throw new Exceptions\JsonDecodeError(json_last_error_msg());
15
        }
16
17
        return $decodedJson;
18
    }
19
20 4
    public static function encode($value, $option = 0, $depth = 512)
21
    {
22 4
        $json = json_encode($value, $option, $depth);
23
24 4
        if($json === false)
25 4
        {
26 2
            $jsonLastError = json_last_error();
0 ignored issues
show
Unused Code introduced by
$jsonLastError is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27
28 2
            throw new Exceptions\JsonEncodeError(json_last_error_msg());
29
        }
30
31 2
        return $json;
32
    }
33
}
34