Completed
Pull Request — master (#4)
by Laurens
02:48
created

Csv   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 14
c 5
b 0
f 0
lcom 0
cbo 0
dl 0
loc 95
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removeHeaders() 0 13 3
A removeLastLines() 0 13 2
B convertDateMDYtoYMD() 0 19 5
A arrayToCsvLine() 0 16 4
1
<?php
2
namespace Werkspot\BingAdsApiBundle\Api\Helper;
3
4
use DateTime;
5
6
class Csv
7
{
8
    /**
9
     * @param array $lines
10
     * @param bool $includingColumnHeaders
11
     * @param int $fileHeaders
12
     * @param int $columnHeaders
13
     *
14
     * @return array
15
     */
16
    public function removeHeaders(array $lines, $includingColumnHeaders = true, $fileHeaders = 10, $columnHeaders = 1)
17
    {
18
        $lines = array_values($lines);
19
20
        $removeLines = ($includingColumnHeaders) ? ($fileHeaders + $columnHeaders) : $fileHeaders;
21
        for ($i = 0; $i < ($removeLines); ++$i) {
22
            unset($lines[$i]);
23
        }
24
25
        $lines = array_values($lines);
26
27
        return $lines;
28
    }
29
30
    /**
31
     * @param array $lines
32
     * @param int   $noOfLinesToRemove
33
     *
34
     * @return array
35
     */
36
    public function removeLastLines(array $lines, $noOfLinesToRemove = 1)
37
    {
38
        $totalLines = count($lines);
39
        $removeFrom = $totalLines - $noOfLinesToRemove;
40
41
        for ($i = $removeFrom; $i < $totalLines; ++$i) {
42
            unset($lines[$i]);
43
        }
44
45
        $lines = array_values($lines);
46
47
        return $lines;
48
    }
49
50
    /**
51
     * @param array $lines
52
     * @param string $separator
53
     * @param string $enclosure
54
     *
55
     * @return array
56
     */
57
    public function convertDateMDYtoYMD(array $lines, $separator = ',', $enclosure = '"')
58
    {
59
        foreach ($lines as $key => $line) {
60
            $columns = str_getcsv($line, $separator);
61
            $isChanged = false;
62
            foreach ($columns as $columnKey => $column) {
63
                if (preg_match('/^([1-9]|1[0-2])\/([1-9]|[1-2][0-9]|3[0-1])\/[0-9]{4}$/', $column)) {
64
                    $date = DateTime::createFromFormat('m/d/Y', $column);
65
                    $columns[$columnKey] = $date->format('Y/m/d');
66
                    $isChanged = true;
67
                }
68
            }
69
            if ($isChanged) {
70
                $lines[$key] = $this->arrayToCsvLine($columns, $separator, $enclosure);
71
            }
72
        }
73
74
        return $lines;
75
    }
76
77
    /**
78
     * @param array $array
79
     * @param string $separator
80
     * @param null|string $enclosure
81
     *
82
     * @return string
83
     */
84
    public function arrayToCsvLine(array $array, $separator = ',', $enclosure = null)
85
    {
86
        $csvStr = '';
87
88
        for ($i = 0; $i < count($array); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
89
            if ($enclosure) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $enclosure of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
90
                $csvStr .= $enclosure . str_replace($enclosure, $enclosure . $enclosure, $array[$i]) . $enclosure;
91
            } else {
92
                $csvStr .= $array[$i];
93
            }
94
95
            $csvStr .= ($i < count($array) - 1) ? $separator : "\r\n";
96
        }
97
98
        return $csvStr;
99
    }
100
}
101