FileCsvIterator::__construct()   B
last analyzed

Complexity

Conditions 9
Paths 48

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 7.6764
c 0
b 0
f 0
cc 9
nc 48
nop 2
1
<?php
2
3
namespace itertools;
4
5
use Exception;
6
use InvalidArgumentException;
7
use SplFileInfo;
8
9
class FileCsvIterator extends AbstractCsvIterator
10
{
11
    protected $fileHandle;
12
    protected $closeFileHandleOnDestruct;
13
    protected $guzzleStream;
14
15
    public function __construct($file, array $options = array())
16
    {
17
        $defaultOptions = array(
18
            'length' => 0,
19
            'fromEncoding' => null,
20
        );
21
        $this->options = array_merge($defaultOptions, $options);
22
23
        if ($file instanceof SplFileInfo) {
24
            $file = $file->getPathname();
25
        }
26
27
        if ($file instanceof \GuzzleHttp\Stream\StreamInterface) {
28
            $this->guzzleStream = $file;
29
            $file = \GuzzleHttp\Stream\GuzzleStreamWrapper::getResource($file);
30
        }
31
32
        if ($file instanceof \Guzzle\Stream\StreamInterface) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Stream\StreamInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
33
            $this->guzzleStream = $file;
34
            $file = $file->getStream();
35
        }
36
37
        if (is_resource($file)) {
38
            $this->fileHandle = $file;
39
            $this->closeFileHandleOnDestruct = false;
40
            if (null !== $this->options['fromEncoding']) {
41
                throw new Exception('Source encoding can only be specified if constructed with file path');
42
            }
43
        } elseif (is_string($file)) {
44
            $this->fileHandle = @fopen($file, 'r');
45
            if ($this->fileHandle === false) {
46
                throw new InvalidArgumentException("Could not open csv file with path: '$file'");
47
            }
48
            if (null !== $this->options['fromEncoding']) {
49
                stream_filter_append($this->fileHandle, 'convert.iconv.' . $this->options['fromEncoding'] . '/UTF-8');
50
            }
51
            $this->closeFileHandleOnDestruct = true;
52
        } else {
53
            throw new InvalidArgumentException('You must provide either a stream or filename to the csv iterator, you provided a ' . gettype($file));
54
        }
55
56
        parent::__construct(array_diff_key($options, $defaultOptions));
57
    }
58
59
    public function retrieveNextCsvRow()
60
    {
61
        return fgetcsv($this->fileHandle, $this->options['length'], $this->options['delimiter'], $this->options['enclosure'], $this->options['escape']);
62
    }
63
64
    public function __destruct()
65
    {
66
        if (null !== $this->fileHandle && $this->closeFileHandleOnDestruct) {
67
            fclose($this->fileHandle);
68
            $this->fileHandle = null;
69
        }
70
    }
71
}
72
73