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