Passed
Push — master ( f13532...bf7dba )
by Andrea Marco
02:44 queued 12s
created

ConfigurableDecoder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
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 decode() 0 13 3
A __construct() 0 2 1
1
<?php
2
3
namespace Cerbero\JsonParser\Decoders;
4
5
use Cerbero\JsonParser\Tokens\Parser;
6
use Cerbero\JsonParser\ValueObjects\Config;
7
8
/**
9
 * The configurable decoder.
10
 *
11
 */
12
final class ConfigurableDecoder
13
{
14
    /**
15
     * Instantiate the class.
16
     *
17
     * @param Config $config
18
     */
19 371
    public function __construct(private readonly Config $config)
20
    {
21 371
    }
22
23
    /**
24
     * Decode the given value.
25
     *
26
     * @param Parser|string|int $value
27
     * @return mixed
28
     */
29 263
    public function decode(Parser|string|int $value): mixed
30
    {
31 263
        if (!is_string($value)) {
32 137
            return $value;
33
        }
34
35 258
        $decoded = $this->config->decoder->decode($value);
36
37 258
        if (!$decoded->succeeded) {
38 6
            ($this->config->onDecodingError)($decoded);
39
        }
40
41 257
        return $decoded->value;
42
    }
43
}
44