ArrayEncoder::supports()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Frostaly\VarExporter\Encoders;
6
7
use Frostaly\VarExporter\Contracts\EncoderInterface;
8
use Frostaly\VarExporter\Encoder;
9
10
class ArrayEncoder implements EncoderInterface
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15
    public function supports(mixed $value): bool
16
    {
17
        return is_array($value) || $value instanceof \ArrayAccess;
18
    }
19
20
    /**
21
     * {@inheritDoc}
22
     *
23
     * @param array|\ArrayAccess $value
24
     */
25
    public function encode(Encoder $encoder, mixed $value): array
26
    {
27
        $array = (array) $value;
28
        $omitKey = array_is_list($array);
1 ignored issue
show
Bug introduced by
The function array_is_list was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $omitKey = /** @scrutinizer ignore-call */ array_is_list($array);
Loading history...
29
30
        foreach ($array as $key => $value) {
31
            $encoded[] = [...$encoder->encode($value), ','];
32
            $omitKey ?: array_unshift(
33
                $encoded[array_key_last($encoded)],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $encoded seems to be defined later in this foreach loop on line 31. Are you sure it is defined here?
Loading history...
34
                var_export($key, true) . ' => ',
35
            );
36
        }
37
        return isset($encoded) ? ['[', $encoded, ']'] : ['[]'];
38
    }
39
}
40