ParamsFormatter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A format() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Swagger\Formatter;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Html\Html;
9
10
use function array_keys;
11
use function array_map;
12
use function implode;
13
use function is_array;
14
15
final class ParamsFormatter
16
{
17 1
    public function format(array $params): string
18
    {
19 1
        if (ArrayHelper::isAssociative($params)) {
20 1
            $keys = array_keys($params);
21 1
            $result = [];
22
23 1
            foreach ($keys as $key) {
24 1
                $element = Html::encode($key) . ':';
25
26 1
                if (is_array($params[$key])) {
27 1
                    $result[] = $element . $this->format($params[$key]);
28 1
                    continue;
29
                }
30
31 1
                $result[] = $element . '"' . Html::encode((string)$params[$key]) . '"';
32
            }
33
34 1
            return '{' . implode(', ', $result) . '}';
35
        }
36
37 1
        $result = array_map([Html::class, 'encode'], $params);
38
39 1
        return '[' . implode(', ', $result) . ']';
40
    }
41
}
42