1 | <?php |
||
36 | class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable |
||
37 | { |
||
38 | /** |
||
39 | * header offset |
||
40 | * |
||
41 | * @var int|null |
||
42 | */ |
||
43 | protected $header_offset; |
||
44 | |||
45 | /** |
||
46 | * header record |
||
47 | * |
||
48 | * @var string[] |
||
49 | */ |
||
50 | protected $header = []; |
||
51 | |||
52 | /** |
||
53 | * records count |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | protected $nb_records = -1; |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 2 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null): AbstractCsv |
|
71 | |||
72 | /** |
||
73 | * Returns the header offset |
||
74 | * |
||
75 | * If no CSV header offset is set this method MUST return null |
||
76 | * |
||
77 | * @return int|null |
||
78 | */ |
||
79 | 10 | public function getHeaderOffset() |
|
83 | |||
84 | /** |
||
85 | * Returns the CSV record used as header |
||
86 | * |
||
87 | * The returned header is represented as an array of string values |
||
88 | * |
||
89 | * @return string[] |
||
90 | */ |
||
91 | 10 | public function getHeader(): array |
|
105 | |||
106 | /** |
||
107 | * Determine the CSV record header |
||
108 | * |
||
109 | * @param int $offset |
||
110 | * |
||
111 | * @throws Exception If the header offset is set and no record is found or is the empty array |
||
112 | * |
||
113 | * @return string[] |
||
114 | */ |
||
115 | 8 | protected function setHeader(int $offset): array |
|
143 | |||
144 | /** |
||
145 | * Strip the BOM sequence from a record |
||
146 | * |
||
147 | * @param string[] $record |
||
148 | * @param int $bom_length |
||
149 | * @param string $enclosure |
||
150 | * |
||
151 | * @return string[] |
||
152 | */ |
||
153 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | 6 | public function __call($method, array $arguments) |
|
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | 2 | public function count(): int |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 2 | public function getIterator(): Iterator |
|
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | 2 | public function jsonSerialize(): array |
|
209 | |||
210 | /** |
||
211 | * Returns the CSV records as an iterator object. |
||
212 | * |
||
213 | * Each CSV record is represented as a simple array containig strings or null values. |
||
214 | * |
||
215 | * If the CSV document has a header record then each record is combined |
||
216 | * to the header record and the header record is removed from the iterator. |
||
217 | * |
||
218 | * If the CSV document is inconsistent. Missing record fields are |
||
219 | * filled with null values while extra record fields are strip from |
||
220 | * the returned object. |
||
221 | * |
||
222 | * @param string[] $header an optional header to use instead of the CSV document header |
||
223 | * |
||
224 | * @return Iterator |
||
225 | */ |
||
226 | 12 | public function getRecords(array $header = []): Iterator |
|
245 | |||
246 | /** |
||
247 | * Returns the header to be used for iteration |
||
248 | * |
||
249 | * @param string[] $header |
||
250 | * |
||
251 | * @throws Exception If the header contains non unique column name |
||
252 | * |
||
253 | * @return string[] |
||
254 | */ |
||
255 | 16 | protected function computeHeader(array $header) |
|
267 | |||
268 | /** |
||
269 | * Combine the CSV header to each record if present |
||
270 | * |
||
271 | * @param Iterator $iterator |
||
272 | * @param string[] $header |
||
273 | * |
||
274 | * @return Iterator |
||
275 | */ |
||
276 | 20 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
293 | |||
294 | /** |
||
295 | * Strip the BOM sequence from the returned records if necessary |
||
296 | * |
||
297 | * @param Iterator $iterator |
||
298 | * @param string $bom |
||
299 | * |
||
300 | * @return Iterator |
||
301 | */ |
||
302 | 16 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
319 | |||
320 | /** |
||
321 | * Selects the record to be used as the CSV header |
||
322 | * |
||
323 | * Because the header is represented as an array, to be valid |
||
324 | * a header MUST contain only unique string value. |
||
325 | * |
||
326 | * @param int|null $offset the header record offset |
||
327 | * |
||
328 | * @throws Exception if the offset is a negative integer |
||
329 | * |
||
330 | * @return static |
||
331 | */ |
||
332 | 14 | public function setHeaderOffset($offset): self |
|
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | 8 | protected function resetProperties() |
|
360 | } |
||
361 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.