Passed
Push — master ( 6139d1...678a4c )
by Petr
13:22 queued 10:27
created

Ini::write_ini_string()   C

Complexity

Conditions 15
Paths 6

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 23.3308

Importance

Changes 0
Metric Value
cc 15
eloc 17
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 28
ccs 10
cts 15
cp 0.6667
crap 23.3308
rs 5.9166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Shared\FormatFiles;
4
5
6
use kalanis\kw_mapper\Interfaces\IFileFormat;
7
use kalanis\kw_mapper\MapperException;
8
9
10
/**
11
 * Class Ini
12
 * @package kalanis\kw_mapper\Storage\Shared\FormatFiles
13
 * Formats/unpack content into/from table created by separated elements in ini/inf file
14
 */
15
class Ini implements IFileFormat
16
{
17
    use TNl;
18
19 1
    public function unpack(string $content): array
20
    {
21 1
        $lines = @parse_ini_string($content, true);
22 1
        if (false === $lines) {
23 1
            throw new MapperException('Cannot parse INI input');
24
        }
25 1
        $records = [];
26 1
        foreach ($lines as $key => &$line) {
27 1
            $records[$key] = array_map([$this, 'unescapeNl'], $line);
28
        }
29 1
        return $records;
30
    }
31
32 1
    public function pack(array $records): string
33
    {
34 1
        $lines = [];
35 1
        foreach ($records as $key => &$record) {
36 1
            $lines[$key] = array_map([$this, 'escapeNl'], (array) $record);
37
        }
38 1
        return $this->write_ini_string($lines);
39
    }
40
41
    /**
42
     * Write an ini configuration file
43
     * @param array<string|int, string|int|float|array<string|int, string|int|array<string|int, string|int>>> $array
44
     * @return string
45
     * @link https://stackoverflow.com/questions/5695145/how-to-read-and-write-to-an-ini-file-with-php
46
     * @codeCoverageIgnore I have zero morale to check this one
47
     */
48 1
    protected function write_ini_string(array $array): string
49
    {
50
        // process array
51 1
        $data = array();
52 1
        foreach ($array as $key => $val) {
53 1
            if (is_array($val)) {
54 1
                $data[] = "[$key]";
55 1
                foreach ($val as $skey => $sval) {
56 1
                    if (is_array($sval)) {
57
                        foreach ($sval as $_skey => $_sval) {
58
                            if (is_numeric($_skey)) {
59
                                $data[] = $skey.'[] = '.(is_numeric($_sval) ? $_sval : (ctype_upper($_sval) ? $_sval : '"'.$_sval.'"'));
60
                            } else {
61
                                $data[] = $skey.'['.$_skey.'] = '.(is_numeric($_sval) ? $_sval : (ctype_upper($_sval) ? $_sval : '"'.$_sval.'"'));
62
                            }
63
                        }
64
                    } else {
65 1
                        $data[] = $skey.' = '.(is_numeric($sval) ? $sval : (ctype_upper($sval) ? $sval : '"'.$sval.'"'));
66
                    }
67
                }
68
            } else {
69
                $data[] = $key.' = '.(is_numeric($val) ? $val : (ctype_upper($val) ? $val : '"'.$val.'"'));
70
            }
71
            // empty line
72 1
            $data[] = null;
73
        }
74
75 1
        return implode(PHP_EOL, $data) . PHP_EOL;
76
    }
77
}
78