PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
← All changes | libraries/csv-parser.class.php +24 -23 2.1.7 → 3.4 View file →
@@ -7,8 +7,10 @@
7 7 * @author Tobias Bäthge
8 8 * @since 1.0.0
9 9 */
10 10
11 +declare(strict_types=1);
12 +
11 13 // Prohibit direct script loading.
12 14 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13 15
14 16 /**
@@ -24,27 +26,24 @@
24 26 /**
25 27 * The used character for the enclosure of a cell. Defaults to quotation mark ".
26 28 *
27 29 * @since 1.0.0
28 - * @var string
29 30 */
30 - protected $enclosure = '"';
31 + protected string $enclosure = '"';
31 32
32 33 /**
33 34 * Number of rows to analyze when attempting to auto-detect the CSV delimiter.
34 35 *
35 36 * @since 1.0.0
36 - * @var int
37 37 */
38 - protected $delimiter_search_max_lines = 15;
38 + protected int $delimiter_search_max_lines = 15;
39 39
40 40 /**
41 41 * Characters to ignore when attempting to auto-detect delimiter.
42 42 *
43 43 * @since 1.0.0
44 - * @var string
45 44 */
46 - protected $non_delimiter_chars = "a-zA-Z0-9\n\r";
45 + protected string $non_delimiter_chars = "a-zA-Z0-9\n\r";
47 46
48 47 /**
49 48 * The preferred delimiter characters, only used when all filtering method return multiple possible delimiters (happens very rarely).
50 49 * There must not be more than 9 characters in the preferred delimiter character list, see `_check_delimiter_count()`.
@@ -49,19 +48,17 @@
49 48 * The preferred delimiter characters, only used when all filtering method return multiple possible delimiters (happens very rarely).
50 49 * There must not be more than 9 characters in the preferred delimiter character list, see `_check_delimiter_count()`.
51 50 *
52 51 * @since 1.0.0
53 - * @var string
54 52 */
55 - protected $preferred_delimiter_chars = ";,\t";
53 + protected string $preferred_delimiter_chars = ";,\t";
56 54
57 55 /**
58 56 * The CSV data string that shall be parsed to an array.
59 57 *
60 58 * @since 1.0.0
61 - * @var string
62 59 */
63 - protected $import_data;
60 + protected string $import_data = '';
64 61
65 62 /**
66 63 * The error state while parsing input data.
67 64 *
@@ -70,19 +67,18 @@
70 67 * 2 = The enclosure character was found in a non-enclosed field. This means the file is either corrupt,
71 68 * or does not follow the common CSV standard. Please validate the parsed data manually.
72 69 *
73 70 * @since 1.0.0
74 - * @var int
75 71 */
76 - public $error = 0;
72 + public int $error = 0;
77 73
78 74 /**
79 75 * Detailed error information.
80 76 *
81 77 * @since 1.0.0
82 - * @var array
78 + * @var array<string, array<string, int|string>>
83 79 */
84 - public $error_info = array();
80 + public array $error_info = array();
85 81
86 82 /**
87 83 * Class Constructor.
88 84 *
@@ -98,11 +94,11 @@
98 94 * @since 1.0.0
99 95 *
100 96 * @param string $data Data to be parsed.
101 97 */
102 - public function load_data( $data ) {
98 + public function load_data( string $data ): void {
103 99 // Check for mandatory trailing line break.
104 - if ( "\n" !== substr( $data, -1 ) ) {
100 + if ( ! str_ends_with( $data, "\n" ) ) {
105 101 $data .= "\n";
106 102 }
107 103 $this->import_data = $data;
108 104 }
@@ -113,9 +109,9 @@
113 109 * @since 1.0.0
114 110 *
115 111 * @return string Most probable delimiter character.
116 112 */
117 - public function find_delimiter() {
113 + public function find_delimiter(): string {
118 114 $data = &$this->import_data;
119 115
120 116 $delimiter_count = array();
121 117 $enclosed = false;
@@ -134,9 +130,9 @@
134 130 $enclosed = ! $enclosed; // Flip bool.
135 131 } elseif ( $enclosed ) {
136 132 ++$i; // Skip next character.
137 133 }
138 - } elseif ( ( "\n" === $curr_char && "\r" !== $prev_char || "\r" === $curr_char ) && ! $enclosed ) {
134 + } elseif ( ( ( "\n" === $curr_char && "\r" !== $prev_char ) || "\r" === $curr_char ) && ! $enclosed ) {
139 135 // Reached end of a line.
140 136 ++$current_line;
141 137 if ( $current_line >= $this->delimiter_search_max_lines ) {
142 138 break;
@@ -169,8 +165,13 @@
169 165 arsort( $delimiter_counts, SORT_NUMERIC );
170 166 $potential_delimiters = array_keys( $delimiter_counts );
171 167 }
172 168
169 + // If still no delimiter was found, fall back to a comma.
170 + if ( empty( $potential_delimiters ) ) {
171 + $potential_delimiters = array( ',' );
172 + }
173 +
173 174 // Return first array element, as that has the highest count.
174 175 return array_shift( $potential_delimiters );
175 176 }
176 177
@@ -179,13 +180,13 @@
179 180 *
180 181 * @since 1.0.0
181 182 *
182 183 * @param string $char Character to check.
183 - * @param array $line_counts Counts for the characters in the lines.
184 + * @param int[] $line_counts Counts for the characters in the lines.
184 185 * @param int $number_lines Number of lines.
185 186 * @return bool|string False if delimiter is not possible, string to be used as a sort key if character could be a delimiter.
186 187 */
187 - protected function _check_delimiter_count( $char, array $line_counts, $number_lines ) {
188 + protected function _check_delimiter_count( string $char, array $line_counts, int $number_lines ) /* : bool|string */ {
188 189 // Was the potential delimiter found in every line?
189 190 if ( count( $line_counts ) !== $number_lines ) {
190 191 return false;
191 192 }
@@ -226,11 +227,11 @@
226 227 *
227 228 * @since 1.0.0
228 229 *
229 230 * @param string $delimiter Delimiter character for the CSV parsing.
230 - * @return array Two-dimensional array with the data from the CSV string.
231 + * @return array<int, array<int, string>> Two-dimensional array with the data from the CSV string.
231 232 */
232 - public function parse( $delimiter ) {
233 + public function parse( string $delimiter ): array {
233 234 $data = &$this->import_data;
234 235
235 236 // Filter delimiter from the list, if it is a whitespace character.
236 237 $white_spaces = str_replace( $delimiter, '', " \t\x0B\0" );
@@ -273,9 +274,9 @@
273 274 $cell_content .= $curr_char;
274 275 ++$i; // Skip next character.
275 276 } elseif ( $next_char !== $delimiter && "\r" !== $next_char && "\n" !== $next_char ) {
276 277 // for-loop (instead of while-loop) that skips whitespace.
277 - for ( $x = ( $i + 1 ); isset( $data[ $x ] ) && '' === ltrim( $data[ $x ], $white_spaces ); $x++ ) {
278 + for ( $x = ( $i + 1 ); isset( $data[ $x ] ) && '' === ltrim( $data[ $x ], $white_spaces ); $x++ ) { // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed,Generic.CodeAnalysis.EmptyStatement.DetectedFor
278 279 // Action is in iterator check.
279 280 }
280 281 if ( $data[ $x ] === $delimiter ) {
281 282 $enclosed = false;