CustomReader::findLastHeaderPos()   B
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.1315
c 0
b 0
f 0
cc 8
nc 5
nop 1
1
<?php
2
3
namespace ilateral\SilverStripe\SlightlyBetterBulkLoader;
4
5
use League\Csv\Reader;
6
7
class CustomReader extends Reader
8
{
9
    /**
10
     * Should this reader ignore column headers that are null when importing?
11
     *
12
     * Sometimes Excel adds null headers to the end of a CSV and this can result in the
13
     * CSV failing to parse.
14
     *
15
     * @var boolean
16
     */
17
    protected $ignore_empty_headers = true;
18
19
    /**
20
     * Validates the array to be used by the fetchAssoc method
21
     *
22
     * @param array $keys
23
     *
24
     * @throws InvalidArgumentException If the submitted array fails the assertion
25
     *
26
     * @return array
27
     */
28
    protected function validateKeys(array $keys)
29
    {
30
        if ($this->getIgnoreEmptyHeaders()) {
31
            $last = $this->findLastHeaderPos($keys);
32
            $new_keys = [];
33
            
34
            for ($i = 0; $i < count($keys); $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...
35
                if ($i <= $last) {
36
                    $new_keys[] = $keys[$i];
37
                }
38
            }
39
40
            $keys = $new_keys;
41
        }
42
43
        return parent::validateKeys($keys);
44
    }
45
46
    /**
47
     * Attempt to find the position of the last legitimate
48
     * header column (a column that is not null and is the last
49
     * item in the list that is not null).
50
     *
51
     * @param array $keys
52
     *
53
     * @return int
54
     */
55
    protected function findLastHeaderPos(array $keys)
56
    {
57
        $last = 0;
58
        $count = count($keys);
59
60
        for ($i = 0; $i < $count; $i++) {
61
            $curr = trim($keys[$i]);
62
            $j = $i;
0 ignored issues
show
Unused Code introduced by
$j is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
64
            // If the current field isn't empty, skip
65
            if (!empty($curr)) {
66
                $last = $i;
67
                continue;
68
            }
69
70
            // If the column is empty, the previous one isn't
71
            // and the next few are or don't exist, assume
72
            // we have hit the last item. Set and break.
73
            if (empty($curr) && isset($keys[$i-1])) {
74
                $temp = 0;
75
                for ($j = $i; $j < $count; $j++) {
76
                    if (!empty($keys[$j])) {
77
                        $temp = $keys[$j];
78
                    }
79
                }
80
81
                if ($temp > 0) {
82
                    $last = $i;
83
                }
84
            }
85
        }
86
87
        return $last;
88
    }
89
90
    /**
91
     * Get the ignore_empty_headers param
92
     *
93
     * @return boolean
94
     */
95
    public function getIgnoreEmptyHeaders()
96
    {
97
        return $this->ignore_empty_headers;
98
    }
99
100
    /**
101
     * Set the ignore_empty_headers param
102
     *
103
     * @param boolean $ignore_empty_headers Ignore header keys that are blank?
104
     *
105
     * @return self
106
     */
107
    public function setIgnoreEmptyHeaders(boolean $ignore_empty_headers)
108
    {
109
        $this->ignore_empty_headers = $ignore_empty_headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ignore_empty_headers of type object<ilateral\SilverSt...tterBulkLoader\boolean> is incompatible with the declared type boolean of property $ignore_empty_headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
110
        return $this;
111
    }
112
}
113