Passed
Branch feature/first-release (43e0cc)
by Andrea Marco
10:48
created

ConfigurableDecoder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A decode() 0 13 3
1
<?php
2
3
namespace Cerbero\JsonParser\Decoders;
4
5
use Cerbero\JsonParser\Config;
6
7
use function call_user_func;
8
9
/**
10
 * The configurable decoder.
11
 *
12
 */
13
final class ConfigurableDecoder
14
{
15
    /**
16
     * Instantiate the class.
17
     *
18
     * @param Config $config
19
     */
20 134
    public function __construct(private Config $config)
21
    {
22 134
    }
23
24
    /**
25
     * Decode the given value.
26
     *
27
     * @param string|int $value
28
     * @return mixed
29
     */
30 92
    public function decode(string|int $value): mixed
31
    {
32 92
        if (is_int($value)) {
33 46
            return $value;
34
        }
35
36 92
        $decoded = $this->config->decoder->decode($value);
37
38 92
        if (!$decoded->succeeded) {
39 6
            call_user_func($this->config->onDecodingError, $decoded);
40
        }
41
42 91
        return $decoded->value;
43
    }
44
}
45