AbstractDecoder::decode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Cerbero\JsonParser\Decoders;
4
5
use Throwable;
6
7
/**
8
 * The abstract implementation of a JSON decoder.
9
 *
10
 */
11
abstract class AbstractDecoder implements Decoder
12
{
13
    /**
14
     * Retrieve the decoded value of the given JSON
15
     *
16
     * @param string $json
17
     * @return mixed
18
     * @throws Throwable
19
     */
20
    abstract protected function decodeJson(string $json): mixed;
21
22
    /**
23
     * Decode the given JSON.
24
     *
25
     * @param string $json
26
     * @return DecodedValue
27
     */
28 266
    public function decode(string $json): DecodedValue
29
    {
30
        try {
31 266
            $value = $this->decodeJson($json);
32 8
        } catch (Throwable $e) {
33 8
            return DecodedValue::failed($e, $json);
34
        }
35
36 262
        return DecodedValue::succeeded($value);
37
    }
38
}
39