DoctrineAnnotationDumper   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 0
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exportValues() 0 10 3
B innerExportValues() 0 43 11
A isAssoc() 0 4 1
1
<?php
2
3
4
namespace TheCodingMachine\FluidSchema;
5
6
7
use function addslashes;
8
use function array_map;
9
use function implode;
10
use function is_array;
11
use function is_numeric;
12
use function is_string;
13
use function str_replace;
14
use function var_export;
15
16
class DoctrineAnnotationDumper
17
{
18
    /**
19
     * Write a string representing the parameter passed in.
20
     * @param mixed $item
21
     */
22
    public static function exportValues($item): string
23
    {
24
        if ($item === null) {
25
            return '';
26
        }
27
        if ($item === []) {
28
            return '({})';
29
        }
30
        return '('.self::innerExportValues($item, true).')';
31
    }
32
33
    private static function innerExportValues($item, bool $first): string
34
    {
35
        if ($item === null) {
36
            return 'null';
37
        }
38
        if (is_string($item)) {
39
            return '"'.str_replace('"', '""', $item).'"';
40
        }
41
        if (is_numeric($item)) {
42
            return $item;
43
        }
44
        if (is_bool($item)) {
45
            return $item ? 'true' : 'false';
46
        }
47
        if (is_array($item)) {
48
            if (self::isAssoc($item)) {
49
                if ($first) {
50
                    array_walk($item, function(&$value, $key) {
51
                        $value = $key.' = '.self::innerExportValues($value, false);
52
                    });
53
                } else {
54
                    array_walk($item, function(&$value, $key) {
55
                        $value = '"'.addslashes($key).'":'.self::innerExportValues($value, false);
56
                    });
57
                }
58
                $result = implode(', ', $item);
59
                if (!$first) {
60
                    $result = '{'.$result.'}';
61
                }
62
                return $result;
63
            } else {
64
                array_walk($item, function(&$value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
                    $value = self::innerExportValues($value, false);
66
                });
67
                $result = implode(', ', $item);
68
                if (!$first) {
69
                    $result = '{'.$result.'}';
70
                }
71
                return $result;
72
            }
73
        }
74
        throw new \RuntimeException('Cannot serialize value in Doctrine annotation.');
75
    }
76
77
    private static function isAssoc(array $arr): bool
78
    {
79
        return array_keys($arr) !== range(0, count($arr) - 1);
80
    }
81
}
82