Failed Conditions
Pull Request — develop (#3536)
by Jonathan
61:12
created

FormatArray::__invoke()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
cc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Exception;
6
7
use function array_map;
8
use function bin2hex;
9
use function implode;
10
use function is_resource;
11
use function is_string;
12
use function json_encode;
13
use function preg_replace;
14
use function sprintf;
15
16
final class FormatArray
17
{
18
    /**
19
     * Returns a human-readable representation of an array of parameters.
20
     * This properly handles binary data by returning a hex representation.
21
     *
22
     * @param array<mixed, mixed> $array
23
     */
24
    public function __invoke(array $array) : string
25
    {
26
        return '[' . implode(', ', array_map(static function ($param) {
27
            if (is_resource($param)) {
28
                return (string) $param;
29
            }
30
31
            $json = @json_encode($param);
32
33
            if (! is_string($json) || $json === 'null' && is_string($param)) {
0 ignored issues
show
introduced by
The condition is_string($json) is always true.
Loading history...
34
                // JSON encoding failed, this is not a UTF-8 string.
35
                return sprintf('"%s"', preg_replace('/.{2}/', '\\x$0', bin2hex($param)));
36
            }
37
38
            return $json;
39
        }, $array)) . ']';
40
    }
41
}
42