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 | classes/class-evaluate-legacy.php +25 -24 2.0.4 → 3.4 View file →
@@ -7,8 +7,10 @@
7 7 * @author Tobias Bäthge
8 8 * @since 1.5.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 /**
@@ -26,27 +28,26 @@
26 28 /**
27 29 * Instance of the EvalMath class.
28 30 *
29 31 * @since 1.0.0
30 - * @var EvalMath
31 32 */
32 - protected $evalmath;
33 + protected \EvalMath $evalmath;
33 34
34 35 /**
35 36 * Table data in which formulas shall be evaluated.
36 37 *
37 38 * @since 1.5.0
38 - * @var array
39 + * @var array<int, array<int, string>>
39 40 */
40 - protected $table_data;
41 + protected array $table_data;
41 42
42 43 /**
43 44 * Storage for cell ranges that have been replaced in formulas.
44 45 *
45 46 * @since 1.0.0
46 - * @var array
47 + * @var array<string, string>
47 48 */
48 - protected $known_ranges = array();
49 + protected array $known_ranges = array();
49 50
50 51 /**
51 52 * Initialize the Formula Evaluation class, include the EvalMath class.
52 53 *
@@ -62,13 +63,13 @@
62 63 * Evaluate formulas in the passed table.
63 64 *
64 65 * @since 1.0.0
65 66 *
66 - * @param array $table_data Table data in which formulas shall be evaluated.
67 - * @param string $table_id ID of the passed table.
68 - * @return array Table data with evaluated formulas.
67 + * @param array<int, array<int, string>> $table_data Table data in which formulas shall be evaluated.
68 + * @param string $table_id ID of the passed table.
69 + * @return array<int, array<int, string>> Table data with evaluated formulas.
69 70 */
70 - public function evaluate_table_data( array $table_data, $table_id ) {
71 + public function evaluate_table_data( array $table_data, string $table_id ): array {
71 72 $this->table_data = $table_data;
72 73
73 74 $num_rows = count( $this->table_data );
74 75 // Exit early if there's no actual table data (e.g. after using the Row Filter module).
@@ -97,15 +98,15 @@
97 98 * Parse and evaluate the content of a cell.
98 99 *
99 100 * @since 1.0.0
100 101 *
101 - * @param string $content Content of a cell.
102 - * @param int $row_idx Row index of the cell.
103 - * @param int $col_idx Column index of the cell.
104 - * @param array $parents Optional. List of cells that depend on this cell (to prevent circle references).
102 + * @param string $content Content of a cell.
103 + * @param int $row_idx Row index of the cell.
104 + * @param int $col_idx Column index of the cell.
105 + * @param string[] $parents Optional. List of cells that depend on this cell (to prevent circle references).
105 106 * @return string Result of the parsing/evaluation.
106 107 */
107 - protected function _evaluate_cell( $content, $row_idx, $col_idx, array $parents = array() ) {
108 + protected function _evaluate_cell( string $content, int $row_idx, int $col_idx, array $parents = array() ): string {
108 109 if ( '' === $content || '=' === $content || '=' !== $content[0] ) {
109 110 return $content;
110 111 }
111 112
@@ -141,17 +142,17 @@
141 142
142 143 $replaced_ranges[] = $cell_range[0];
143 144
144 145 if ( isset( $this->known_ranges[ $cell_range[0] ] ) ) {
145 - $expression = preg_replace( '#(?<![A-Z])' . preg_quote( $cell_range[0], '#' ) . '(?![0-9])#', $this->known_ranges[ $cell_range[0] ], $expression );
146 + $expression = (string) preg_replace( '#(?<![A-Z])' . preg_quote( $cell_range[0], '#' ) . '(?![0-9])#', $this->known_ranges[ $cell_range[0] ], $expression );
146 147 continue;
147 148 }
148 149
149 150 // No -1 necessary for this transformation, as we don't actually access the table.
150 151 $first_col = TablePress::letter_to_number( $cell_range[1] );
151 - $first_row = $cell_range[2];
152 + $first_row = (int) $cell_range[2];
152 153 $last_col = TablePress::letter_to_number( $cell_range[3] );
153 - $last_row = $cell_range[4];
154 + $last_row = (int) $cell_range[4];
154 155
155 156 $col_start = min( $first_col, $last_col );
156 157 $col_end = max( $first_col, $last_col ) + 1; // +1 for loop below
157 158 $row_start = min( $first_row, $last_row );
@@ -165,9 +166,9 @@
165 166 }
166 167 }
167 168 $cell_list = implode( ',', $cell_list );
168 169
169 - $expression = preg_replace( '#(?<![A-Z])' . preg_quote( $cell_range[0], '#' ) . '(?![0-9])#', $cell_list, $expression );
170 + $expression = (string) preg_replace( '#(?<![A-Z])' . preg_quote( $cell_range[0], '#' ) . '(?![0-9])#', $cell_list, $expression );
170 171 $this->known_ranges[ $cell_range[0] ] = $cell_list;
171 172 }
172 173 }
173 174
@@ -184,9 +185,9 @@
184 185
185 186 $replaced_references[] = $cell_reference[0];
186 187
187 188 $ref_col = TablePress::letter_to_number( $cell_reference[1] ) - 1;
188 - $ref_row = $cell_reference[2] - 1;
189 + $ref_row = (int) $cell_reference[2] - 1;
189 190
190 191 if ( ! isset( $this->table_data[ $ref_row ][ $ref_col ] ) ) {
191 192 return "!ERROR! Cell {$cell_reference[0]} does not exist";
192 193 }
@@ -196,9 +197,9 @@
196 197
197 198 $result = $this->_evaluate_cell( $this->table_data[ $ref_row ][ $ref_col ], $ref_row, $ref_col, $ref_parents );
198 199 $this->table_data[ $ref_row ][ $ref_col ] = $result;
199 200 // Bail if there was an error already.
200 - if ( false !== strpos( $result, '!ERROR!' ) ) {
201 + if ( str_contains( $result, '!ERROR!' ) ) {
201 202 return $result;
202 203 }
203 204 // Remove all whitespace characters.
204 205 $result = str_replace( array( "\n", "\r", "\t", ' ' ), '', $result );
@@ -203,9 +204,9 @@
203 204 // Remove all whitespace characters.
204 205 $result = str_replace( array( "\n", "\r", "\t", ' ' ), '', $result );
205 206 // Treat empty cells as 0.
206 207 if ( '' === $result ) {
207 - $result = 0;
208 + $result = '0';
208 209 }
209 210 // Bail if the cell does not result in a number (meaning it was a number or expression before being evaluated).
210 211 if ( ! is_numeric( $result ) ) {
211 212 return "!ERROR! {$cell_reference[0]} does not contain a number or expression";
@@ -210,9 +211,9 @@
210 211 if ( ! is_numeric( $result ) ) {
211 212 return "!ERROR! {$cell_reference[0]} does not contain a number or expression";
212 213 }
213 214
214 - $expression = preg_replace( '#(?<![A-Z])' . $cell_reference[0] . '(?![0-9])#', $result, $expression );
215 + $expression = (string) preg_replace( '#(?<![A-Z])' . $cell_reference[0] . '(?![0-9])#', $result, $expression );
215 216 }
216 217 }
217 218
218 219 $result = $this->_evaluate_math_expression( $expression, $row_idx, $col_idx );
@@ -236,9 +237,9 @@
236 237 * @param int $row_idx Row index of the cell with the expression.
237 238 * @param int $col_idx Column index of the cell with the expression.
238 239 * @return string Result of the evaluation.
239 240 */
240 - protected function _evaluate_math_expression( $expression, $row_idx, $col_idx ) {
241 + protected function _evaluate_math_expression( string $expression, int $row_idx, int $col_idx ): string {
241 242 // Make current cell's name and row and column number available as variables in formulas.
242 243 $this->evalmath->variables['row'] = $row_idx + 1;
243 244 $this->evalmath->variables['column'] = $col_idx + 1;
244 245 $this->evalmath->variables['cell'] = TablePress::number_to_letter( $this->evalmath->variables['column'] ) . $this->evalmath->variables['row'];