Csv::str_putcsv()   C
last analyzed

Complexity

Conditions 12
Paths 20

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 19
c 1
b 0
f 0
nc 20
nop 4
dl 0
loc 31
ccs 0
cts 0
cp 0
crap 156
rs 6.9666

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
8
9
/**
10
 * Class Csv
11
 * @package kalanis\kw_mapper\Storage\Shared\FormatFiles
12
 * Store it in CSV table
13
 */
14
class Csv implements IFileFormat
15
{
16
    use TNl;
17
18
    protected string $delimitLines = PHP_EOL;
19
20 1
    public function setDelimiters(string $lines = PHP_EOL): self
21
    {
22 1
        $this->delimitLines = $lines;
23 1
        return $this;
24
    }
25
26 1
    public function unpack(string $content): array
27
    {
28 1
        $lines = explode($this->delimitLines, $content);
29 1
        $records = [];
30 1
        foreach ($lines as &$line) {
31 1
            if (empty($line)) {
32 1
                continue;
33
            }
34
35 1
            $records[] = array_map([$this, 'unescapeNl'], str_getcsv($line, ',', '"', '\\'));
36
        }
37 1
        return $records;
38
    }
39
40 1
    public function pack(array $content): string
41
    {
42 1
        $lines = [];
43 1
        foreach ($content as &$record) {
44 1
            $record = (array) $record;
45 1
            ksort($record);
46 1
            $record[] = ''; // separator on end
47 1
            $lines[] = $this->str_putcsv(array_map([$this, 'escapeNl'], $record));
48
        }
49 1
        return implode($this->delimitLines, $lines);
50
    }
51
52
    /**
53
     * @param array<string|int, string|int|float|array<string|int, string|int>> $array
54
     * @param string $delimiter
55
     * @param string $enclosure
56
     * @param string $terminator
57
     * @return string
58
     * @link https://www.php.net/manual/en/function.str-getcsv.php#88353
59
     * @codeCoverageIgnore better try it live
60
     */
61
    protected function str_putcsv(array $array, string $delimiter = ',', string $enclosure = '"', string $terminator = PHP_EOL): string
62
    {
63
        # First convert associative array to numeric indexed array
64
        $workArray = array_values($array);
65
        $returnString = '';                 # Initialize return string
66
        $arraySize = count($workArray);     # Get size of array
67
68
        for ($i=0; $i<$arraySize; $i++) {
69
            # Nested array, process nest item
70
            if (is_array($workArray[$i])) {
71
                $returnString .= $this->str_putcsv($workArray[$i], $delimiter, $enclosure, $terminator);
72
            } else {
73
                switch (gettype($workArray[$i])) {
74
                    # Manually set some strings
75
                    case "NULL":     $_spFormat = ''; break;
76
                    case "boolean":  $_spFormat = (true == $workArray[$i]) ? 'true': 'false'; break;
77
                    # Make sure sprintf has a good datatype to work with
78
                    case "integer":  $_spFormat = '%i'; break;
79
                    case "double":   $_spFormat = '%0.2f'; break;
80
                    case "string":   $_spFormat = '%s'; break;
81
                    # Unknown or invalid items for a csv - note: the datatype of array is already handled above, assuming the data is nested
82
                    case "object":
83
                    case "resource":
84
                    default:         $_spFormat = '';
85
                }
86
                $returnString .= sprintf('%2$s'.$_spFormat.'%2$s', $workArray[$i], $enclosure);
87
                $returnString .= ($i < ($arraySize-1)) ? $delimiter : $terminator;
88
            }
89
        }
90
        # Done the workload, return the output information
91
        return $returnString;
92
    }
93
}
94