Completed
Pull Request — master (#173)
by
unknown
11:31
created

Reader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 9
c 5
b 0
f 1
lcom 1
cbo 3
dl 0
loc 120
ccs 28
cts 28
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setFieldDelimiter() 0 5 1
A setFieldEnclosure() 0 5 1
A setEncoding() 0 5 1
A setEndOfLineCharacter() 0 5 1
A openReader() 0 16 2
A getConcreteSheetIterator() 0 4 1
A closeReader() 0 6 2
1
<?php
2
3
namespace Box\Spout\Reader\CSV;
4
5
use Box\Spout\Reader\AbstractReader;
6
use Box\Spout\Common\Exception\IOException;
7
use Box\Spout\Common\Helper\EncodingHelper;
8
9
/**
10
 * Class Reader
11
 * This class provides support to read data from a CSV file.
12
 *
13
 * @package Box\Spout\Reader\CSV
14
 */
15
class Reader extends AbstractReader
16
{
17
    /** @var resource Pointer to the file to be written */
18
    protected $filePointer;
19
20
    /** @var SheetIterator To iterator over the CSV unique "sheet" */
21
    protected $sheetIterator;
22
23
    /** @var string Defines the character used to delimit fields (one character only) */
24
    protected $fieldDelimiter = ',';
25
26
    /** @var string Defines the character used to enclose fields (one character only) */
27
    protected $fieldEnclosure = '"';
28
29
    /** @var string Encoding of the CSV file to be read */
30
    protected $encoding = EncodingHelper::ENCODING_UTF8;
31
32
    /** @var string Defines the End of line */
33
    protected $endOfLineCharacter = "\n";    
34
35
    /**
36
     * Sets the field delimiter for the CSV.
37
     * Needs to be called before opening the reader.
38
     *
39 45
     * @param string $fieldDelimiter Character that delimits fields
40
     * @return Reader
41 45
     */
42 45
    public function setFieldDelimiter($fieldDelimiter)
43
    {
44
        $this->fieldDelimiter = $fieldDelimiter;
45
        return $this;
46
    }
47
48
    /**
49
     * Sets the field enclosure for the CSV.
50
     * Needs to be called before opening the reader.
51
     *
52 45
     * @param string $fieldEnclosure Character that enclose fields
53
     * @return Reader
54 45
     */
55 45
    public function setFieldEnclosure($fieldEnclosure)
56
    {
57
        $this->fieldEnclosure = $fieldEnclosure;
58
        return $this;
59
    }
60
61
    /**
62
     * Sets the encoding of the CSV file to be read.
63
     * Needs to be called before opening the reader.
64
     *
65 57
     * @param string $encoding Encoding of the CSV file to be read
66
     * @return Reader
67 57
     */
68 57
    public function setEncoding($encoding)
69
    {
70
        $this->encoding = $encoding;
71
        return $this;
72
    }
73
74
    /**
75
     * Sets the EOL for the CSV.
76
     * Needs to be called before opening the reader.
77
     *
78
     * @param string $fieldEnclosure Character that enclose fields
0 ignored issues
show
Bug introduced by
There is no parameter named $fieldEnclosure. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
79 63
     * @return Reader
80
     */
81 63
    public function setEndOfLineCharacter($endOfLineCharacter)
82 63
    {
83 3
        $this->endOfLineCharacter = $endOfLineCharacter;
84
        return $this;
85
    }  
86 60
87 60
    /**
88 60
     * Opens the file at the given path to make it ready to be read.
89 60
     * If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
90 60
     *
91 60
     * @param  string $filePath Path of the CSV file to be read
92 60
     * @return void
93 60
     * @throws \Box\Spout\Common\Exception\IOException
94
     */
95
    protected function openReader($filePath)
96
    {
97
        $this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r');
98
        if (!$this->filePointer) {
99
            throw new IOException("Could not open file $filePath for reading.");
100 60
        }
101
102 60
        $this->sheetIterator = new SheetIterator(
103
            $this->filePointer,
104
            $this->fieldDelimiter,
105
            $this->fieldEnclosure,
106
            $this->encoding,
107
            $this->endOfLineCharacter,
108
            $this->globalFunctionsHelper
109
        );
110
    }
111 60
112
    /**
113 60
     * Returns an iterator to iterate over sheets.
114 60
     *
115 60
     * @return SheetIterator To iterate over sheets
116 60
     */
117
    public function getConcreteSheetIterator()
118
    {
119
        return $this->sheetIterator;
120
    }
121
122
123
    /**
124
     * Closes the reader. To be used after reading the file.
125
     *
126
     * @return void
127
     */
128
    protected function closeReader()
129
    {
130
        if ($this->filePointer) {
131
            $this->globalFunctionsHelper->fclose($this->filePointer);
132
        }
133
    }
134
}
135