This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Linio\Component\SpreadsheetParser\Parser; |
||
4 | |||
5 | class CsvParser implements ParserInterface |
||
6 | { |
||
7 | const OPTION_HAS_COLUMN_NAMES = 'has_column_names'; |
||
8 | const OPTION_LENGTH = 'length'; |
||
9 | const OPTION_DELIMITER = 'delimiter'; |
||
10 | const OPTION_ENCLOSURE = 'enclosure'; |
||
11 | const OPTION_ESCAPE = 'escape'; |
||
12 | |||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $filePath; |
||
17 | |||
18 | /** |
||
19 | * @var resource |
||
20 | */ |
||
21 | protected $handle; |
||
22 | |||
23 | /** |
||
24 | * @var bool |
||
25 | */ |
||
26 | protected $hasColumnNames; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $length; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $delimiter; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $enclosure; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $escape; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $columnNames; |
||
52 | |||
53 | /** |
||
54 | * @param string $filePath |
||
55 | */ |
||
56 | public function __construct($filePath, array $options = []) |
||
57 | { |
||
58 | $this->filePath = $filePath; |
||
59 | |||
60 | // default options |
||
61 | $this->hasColumnNames = true; |
||
62 | $this->length = 0; |
||
63 | $this->delimiter = ','; |
||
64 | $this->enclosure = '"'; |
||
65 | $this->escape = '\\'; |
||
66 | |||
67 | $this->loadParserOptions($options); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function open() |
||
74 | { |
||
75 | if ($this->handle) { |
||
76 | return true; |
||
77 | } |
||
78 | |||
79 | $this->handle = fopen($this->filePath, 'r'); |
||
80 | |||
81 | return true; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
86 | * |
||
87 | * @return array|false |
||
88 | */ |
||
89 | public function getColumnNames() |
||
90 | { |
||
91 | if (!$this->hasColumnNames) { |
||
92 | return false; |
||
93 | } |
||
94 | |||
95 | if ($this->columnNames) { |
||
0 ignored issues
–
show
|
|||
96 | return $this->columnNames; |
||
97 | } |
||
98 | |||
99 | $this->open(); |
||
100 | $this->columnNames = fgetcsv($this->handle, $this->length, $this->delimiter, $this->enclosure, $this->escape); |
||
101 | |||
102 | return $this->columnNames; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param int $numRows |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | View Code Duplication | public function getData($numRows = 0) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
111 | { |
||
112 | $this->close(); |
||
113 | $this->open(); |
||
114 | |||
115 | $skipLine = false; |
||
116 | if ($this->hasColumnNames) { |
||
117 | $skipLine = true; |
||
118 | } |
||
119 | |||
120 | $data = $this->readDataFromFile($numRows, $skipLine); |
||
121 | |||
122 | return $data; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function close() |
||
129 | { |
||
130 | if ($this->handle) { |
||
131 | fclose($this->handle); |
||
132 | $this->handle = null; |
||
133 | } |
||
134 | |||
135 | return true; |
||
136 | } |
||
137 | |||
138 | public function __destruct() |
||
139 | { |
||
140 | $this->close(); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param array $options |
||
145 | * |
||
146 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
147 | */ |
||
148 | protected function loadParserOptions(array $options) |
||
149 | { |
||
150 | if (isset($options[static::OPTION_HAS_COLUMN_NAMES])) { |
||
151 | $this->hasColumnNames = $options[static::OPTION_HAS_COLUMN_NAMES]; |
||
152 | } |
||
153 | |||
154 | if (isset($options[static::OPTION_LENGTH])) { |
||
155 | $this->length = $options[static::OPTION_LENGTH]; |
||
156 | } |
||
157 | |||
158 | if (isset($options[static::OPTION_DELIMITER])) { |
||
159 | $this->delimiter = $options[static::OPTION_DELIMITER]; |
||
160 | } |
||
161 | |||
162 | if (isset($options[static::OPTION_ENCLOSURE])) { |
||
163 | $this->enclosure = $options[static::OPTION_ENCLOSURE]; |
||
164 | } |
||
165 | |||
166 | if (isset($options[static::OPTION_ESCAPE])) { |
||
167 | $this->escape = $options[static::OPTION_ESCAPE]; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param $numRows |
||
173 | * @param $skipLine |
||
174 | * |
||
175 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | protected function readDataFromFile($numRows, $skipLine) |
||
180 | { |
||
181 | $data = []; |
||
182 | while (($row = fgetcsv($this->handle, $this->length, $this->delimiter, $this->enclosure, $this->escape)) !== false) { |
||
183 | if ($skipLine) { |
||
184 | $skipLine = false; |
||
185 | continue; |
||
186 | } |
||
187 | |||
188 | $data[] = $row; |
||
189 | |||
190 | $numRows--; |
||
191 | if ($numRows == 0) { |
||
192 | break; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | return $data; |
||
197 | } |
||
198 | } |
||
199 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.