| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | * @author Florian Schmitz, Brett Zamir, Nikolay Matsievsky, Cedric Morin, Christopher Finke, Mark Scherer, 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 | /** |
| @@ -18,65 +20,58 @@ | ||
| 18 | 20 | * |
| 19 | 21 | * @package CSSTidy |
| 20 | 22 | * @version 1.0 |
| 21 | 23 | */ |
| 22 | -class TablePress_CSSTidy_optimise { | |
| 24 | +class TablePress_CSSTidy_Optimise { | |
| 23 | 25 | |
| 24 | 26 | /** |
| 25 | 27 | * TablePress_CSSTidy instance. |
| 26 | 28 | * |
| 27 | 29 | * @since 1.0.0 |
| 28 | - * @var TablePress_CSSTidy | |
| 29 | 30 | */ |
| 30 | - public $parser; | |
| 31 | + public \TablePress_CSSTidy $parser; | |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | 34 | * The parsed CSS. |
| 34 | 35 | * |
| 35 | 36 | * @since 1.0.0 |
| 36 | - * @var array | |
| 37 | 37 | */ |
| 38 | - public $css = array(); | |
| 38 | + public array $css = array(); | |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The current sub-value. |
| 42 | 42 | * |
| 43 | 43 | * @since 1.0.0 |
| 44 | - * @var string | |
| 45 | 44 | */ |
| 46 | - public $sub_value = ''; | |
| 45 | + public string $sub_value = ''; | |
| 47 | 46 | |
| 48 | 47 | /** |
| 49 | 48 | * The current at rule (@media). |
| 50 | 49 | * |
| 51 | 50 | * @since 1.0.0 |
| 52 | - * @var string | |
| 53 | 51 | */ |
| 54 | - public $at = ''; | |
| 52 | + public string $at = ''; | |
| 55 | 53 | |
| 56 | 54 | /** |
| 57 | 55 | * The current selector. |
| 58 | 56 | * |
| 59 | 57 | * @since 1.0.0 |
| 60 | - * @var string | |
| 61 | 58 | */ |
| 62 | - public $selector = ''; | |
| 59 | + public string $selector = ''; | |
| 63 | 60 | |
| 64 | 61 | /** |
| 65 | 62 | * The current property. |
| 66 | 63 | * |
| 67 | 64 | * @since 1.0.0 |
| 68 | - * @var string | |
| 69 | 65 | */ |
| 70 | - public $property = ''; | |
| 66 | + public string $property = ''; | |
| 71 | 67 | |
| 72 | 68 | /** |
| 73 | 69 | * The current value. |
| 74 | 70 | * |
| 75 | 71 | * @since 1.0.0 |
| 76 | - * @var string | |
| 77 | 72 | */ |
| 78 | - public $value = ''; | |
| 73 | + public string $value = ''; | |
| 79 | 74 | |
| 80 | 75 | /** |
| 81 | 76 | * Constructor. |
| 82 | 77 | * |
| @@ -81,18 +76,18 @@ | ||
| 81 | 76 | * Constructor. |
| 82 | 77 | * |
| 83 | 78 | * @since 1.0.0 |
| 84 | 79 | * |
| 85 | - * @param TablePress_CSSTidy $css Instance of the TablePress_CSSTidy class. | |
| 80 | + * @param TablePress_CSSTidy $csstidy Instance of the TablePress_CSSTidy class. | |
| 86 | 81 | */ |
| 87 | - public function __construct( $css ) { | |
| 88 | - $this->parser = $css; | |
| 89 | - $this->css = &$css->css; | |
| 90 | - $this->sub_value = &$css->sub_value; | |
| 91 | - $this->at = &$css->at; | |
| 92 | - $this->selector = &$css->selector; | |
| 93 | - $this->property = &$css->property; | |
| 94 | - $this->value = &$css->value; | |
| 82 | + public function __construct( TablePress_CSSTidy $csstidy ) { | |
| 83 | + $this->parser = $csstidy; | |
| 84 | + $this->css = &$csstidy->css; | |
| 85 | + $this->sub_value = &$csstidy->sub_value; | |
| 86 | + $this->at = &$csstidy->at; | |
| 87 | + $this->selector = &$csstidy->selector; | |
| 88 | + $this->property = &$csstidy->property; | |
| 89 | + $this->value = &$csstidy->value; | |
| 95 | 90 | } |
| 96 | 91 | |
| 97 | 92 | /** |
| 98 | 93 | * Optimises $css after parsing. |
| @@ -98,9 +93,9 @@ | ||
| 98 | 93 | * Optimises $css after parsing. |
| 99 | 94 | * |
| 100 | 95 | * @since 1.0.0 |
| 101 | 96 | */ |
| 102 | - public function postparse() { | |
| 97 | + public function postparse(): void { | |
| 103 | 98 | if ( $this->parser->get_cfg( 'preserve_css' ) ) { |
| 104 | 99 | return; |
| 105 | 100 | } |
| 106 | 101 | |
| @@ -150,14 +145,14 @@ | ||
| 150 | 145 | * Optimises values |
| 151 | 146 | * |
| 152 | 147 | * @since 1.0.0 |
| 153 | 148 | */ |
| 154 | - public function value() { | |
| 149 | + public function value(): void { | |
| 155 | 150 | $shorthands = &$this->parser->data['csstidy']['shorthands']; |
| 156 | 151 | |
| 157 | 152 | // Optimise shorthand properties. |
| 158 | 153 | if ( isset( $shorthands[ $this->property ] ) && $this->parser->get_cfg( 'optimise_shorthands' ) > 0 ) { |
| 159 | - $temp = $this->shorthand( $this->value ); // FIXME - move | |
| 154 | + $temp = $this->shorthand( $this->value ); // FIXME - move. | |
| 160 | 155 | if ( $temp !== $this->value ) { |
| 161 | 156 | $this->parser->log( 'Optimised shorthand notation (' . $this->property . '): Changed "' . $this->value . '" to "' . $temp . '"', 'Information' ); |
| 162 | 157 | } |
| 163 | 158 | $this->value = $temp; |
| @@ -162,9 +157,9 @@ | ||
| 162 | 157 | } |
| 163 | 158 | $this->value = $temp; |
| 164 | 159 | } |
| 165 | 160 | |
| 166 | - // Remove whitespace at !important | |
| 161 | + // Remove whitespace at !important. | |
| 167 | 162 | if ( $this->value !== $this->compress_important( $this->value ) ) { |
| 168 | 163 | $this->parser->log( 'Optimised !important', 'Information' ); |
| 169 | 164 | } |
| 170 | 165 | } |
| @@ -173,9 +168,9 @@ | ||
| 173 | 168 | * Optimises shorthands. |
| 174 | 169 | * |
| 175 | 170 | * @since 1.0.0 |
| 176 | 171 | */ |
| 177 | - public function shorthands() { | |
| 172 | + public function shorthands(): void { | |
| 178 | 173 | $shorthands = &$this->parser->data['csstidy']['shorthands']; |
| 179 | 174 | |
| 180 | 175 | if ( ! $this->parser->get_cfg( 'optimise_shorthands' ) || $this->parser->get_cfg( 'preserve_css' ) ) { |
| 181 | 176 | return; |
| @@ -201,13 +196,13 @@ | ||
| 201 | 196 | * Optimises a sub-value. |
| 202 | 197 | * |
| 203 | 198 | * @since 1.0.0 |
| 204 | 199 | */ |
| 205 | - public function subvalue() { | |
| 200 | + public function subvalue(): void { | |
| 206 | 201 | $replace_colors = &$this->parser->data['csstidy']['replace_colors']; |
| 207 | 202 | |
| 208 | 203 | $this->sub_value = trim( $this->sub_value ); |
| 209 | - if ( '' === $this->sub_value ) { // caution : '0' | |
| 204 | + if ( '' === $this->sub_value ) { // caution : '0'. | |
| 210 | 205 | return; |
| 211 | 206 | } |
| 212 | 207 | |
| 213 | 208 | $important = ''; |
| @@ -259,9 +254,9 @@ | ||
| 259 | 254 | * |
| 260 | 255 | * @param string $value Shorthand value. |
| 261 | 256 | * @return string Compressed value. |
| 262 | 257 | */ |
| 263 | - public function shorthand( $value ) { | |
| 258 | + public function shorthand( string $value ): string { | |
| 264 | 259 | $important = ''; |
| 265 | 260 | if ( $this->parser->is_important( $value ) ) { |
| 266 | 261 | $values = $this->parser->gvw_important( $value ); |
| 267 | 262 | $important = ' !important'; |
| @@ -301,16 +296,16 @@ | ||
| 301 | 296 | * Removes unnecessary whitespace in ! important. |
| 302 | 297 | * |
| 303 | 298 | * @since 1.0.0 |
| 304 | 299 | * |
| 305 | - * @param string $string String. | |
| 300 | + * @param string $a_string String. | |
| 306 | 301 | * @return string Cleaned string. |
| 307 | 302 | */ |
| 308 | - public function compress_important( &$string ) { | |
| 309 | - if ( $this->parser->is_important( $string ) ) { | |
| 310 | - $string = $this->parser->gvw_important( $string ) . ' !important'; | |
| 303 | + public function compress_important( string &$a_string ): string { | |
| 304 | + if ( $this->parser->is_important( $a_string ) ) { | |
| 305 | + $a_string = $this->parser->gvw_important( $a_string ) . ' !important'; | |
| 311 | 306 | } |
| 312 | - return $string; | |
| 307 | + return $a_string; | |
| 313 | 308 | } |
| 314 | 309 | |
| 315 | 310 | /** |
| 316 | 311 | * Color compression function. Converts all rgb() values to #-values and uses the short-form if possible. Also replaces 4 color names by #-values. |
| @@ -319,23 +314,23 @@ | ||
| 319 | 314 | * |
| 320 | 315 | * @param string $color Color value. |
| 321 | 316 | * @return string Compressed color. |
| 322 | 317 | */ |
| 323 | - public function cut_color( $color ) { | |
| 318 | + public function cut_color( string $color ): string { | |
| 324 | 319 | $replace_colors = &$this->parser->data['csstidy']['replace_colors']; |
| 325 | 320 | |
| 326 | - // If it's a string, don't touch! | |
| 327 | - if ( 0 === strncmp( $color, "'", 1 ) || 0 === strncmp( $color, '"', 1 ) ) { | |
| 321 | + // If it's a string, don't touch it! | |
| 322 | + if ( str_starts_with( $color, "'" ) || str_starts_with( $color, '"' ) ) { | |
| 328 | 323 | return $color; |
| 329 | 324 | } |
| 330 | 325 | |
| 331 | - // Complex gradient expressions | |
| 332 | - if ( false !== strpos( $color, '(' ) && 0 !== strncasecmp( $color, 'rgb(', 4 ) ) { | |
| 333 | - // Don't touch properties within MSIE filters, those are to sensitive. | |
| 326 | + // Complex gradient expressions. | |
| 327 | + if ( str_contains( $color, '(' ) && 0 !== strncasecmp( $color, 'rgb(', 4 ) && 0 !== strncasecmp( $color, 'rgba(', 5 ) ) { | |
| 328 | + // Don't touch properties within MSIE filters, those are too sensitive. | |
| 334 | 329 | if ( false !== stripos( $color, 'progid:' ) ) { |
| 335 | 330 | return $color; |
| 336 | 331 | } |
| 337 | - preg_match_all( ',rgb\([^)]+\),i', $color, $matches, PREG_SET_ORDER ); | |
| 332 | + preg_match_all( ',rgba?\([^)]+\),i', $color, $matches, PREG_SET_ORDER ); | |
| 338 | 333 | if ( count( $matches ) ) { |
| 339 | 334 | foreach ( $matches as $m ) { |
| 340 | 335 | $color = str_replace( $m[0], $this->cut_color( $m[0] ), $color ); |
| 341 | 336 | } |
| @@ -348,28 +343,50 @@ | ||
| 348 | 343 | } |
| 349 | 344 | return $color; |
| 350 | 345 | } |
| 351 | 346 | |
| 352 | - // rgb(0,0,0) -> #000000 (or #000 in this case later) | |
| 353 | - if ( 0 === strncasecmp( $color, 'rgb(', 4 ) ) { | |
| 354 | - $color_tmp = substr( $color, 4, strlen( $color ) - 5 ); | |
| 355 | - $color_tmp = explode( ',', $color_tmp ); | |
| 356 | - $color_tmp_count = count( $color_tmp ); | |
| 357 | - for ( $i = 0; $i < $color_tmp_count; $i++ ) { | |
| 358 | - $color_tmp[ $i ] = trim( $color_tmp[ $i ] ); | |
| 359 | - if ( '%' === substr( $color_tmp[ $i ], -1 ) ) { | |
| 360 | - $color_tmp[ $i ] = round( ( 255 * $color_tmp[ $i ] ) / 100 ); | |
| 347 | + // rgb(0,0,0) -> #000000 (or #000 in this case later). | |
| 348 | + if ( | |
| 349 | + // Be sure to not corrupt a rgb with calc() value. | |
| 350 | + ( 0 === strncasecmp( $color, 'rgb(', 4 ) && false === strpos( $color, '(', 4 ) ) || ( 0 === strncasecmp( $color, 'rgba(', 5 ) && false === strpos( $color, '(', 5 ) ) | |
| 351 | + ) { | |
| 352 | + $color_tmp = explode( '(', $color, 2 ); | |
| 353 | + $color_tmp = rtrim( end( $color_tmp ), ')' ); | |
| 354 | + if ( str_contains( $color_tmp, '/' ) ) { | |
| 355 | + $color_tmp = explode( '/', $color_tmp, 2 ); | |
| 356 | + $color_parts = explode( ' ', trim( reset( $color_tmp ) ), 3 ); | |
| 357 | + while ( count( $color_parts ) < 3 ) { // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found | |
| 358 | + $color_parts[] = 0; | |
| 361 | 359 | } |
| 362 | - if ( $color_tmp[ $i ] > 255 ) { | |
| 363 | - $color_tmp[ $i ] = 255; | |
| 360 | + $color_parts[] = end( $color_tmp ); | |
| 361 | + } else { | |
| 362 | + $color_parts = explode( ',', $color_tmp, 4 ); | |
| 363 | + } | |
| 364 | + $color_parts_count = count( $color_parts ); | |
| 365 | + for ( $i = 0; $i < $color_parts_count; $i++ ) { | |
| 366 | + $color_parts[ $i ] = trim( $color_parts[ $i ] ); | |
| 367 | + if ( str_ends_with( $color_parts[ $i ], '%' ) ) { | |
| 368 | + $color_parts[ $i ] = round( ( 255 * intval( $color_parts[ $i ] ) ) / 100 ); | |
| 369 | + } elseif ( $i > 2 ) { | |
| 370 | + // 4th argument is alpha layer between 0 and 1 (if not %). | |
| 371 | + $color_parts[ $i ] = round( 255 * floatval( $color_parts[ $i ] ) ); | |
| 364 | 372 | } |
| 373 | + $color_parts[ $i ] = intval( $color_parts[ $i ] ); | |
| 374 | + if ( $color_parts[ $i ] > 255 ) { | |
| 375 | + $color_parts[ $i ] = 255; | |
| 376 | + } | |
| 365 | 377 | } |
| 366 | 378 | $color = '#'; |
| 367 | - for ( $i = 0; $i < 3; $i++ ) { | |
| 368 | - if ( $color_tmp[ $i ] < 16 ) { | |
| 369 | - $color .= '0' . dechex( $color_tmp[ $i ] ); | |
| 379 | + // 3 or 4 parts depending on alpha layer. | |
| 380 | + $nb = min( max( count( $color_parts ), 3 ), 4 ); | |
| 381 | + for ( $i = 0; $i < $nb; $i++ ) { | |
| 382 | + if ( ! isset( $color_parts[ $i ] ) ) { | |
| 383 | + $color_parts[ $i ] = 0; | |
| 384 | + } | |
| 385 | + if ( $color_parts[ $i ] < 16 ) { | |
| 386 | + $color .= '0' . dechex( $color_parts[ $i ] ); | |
| 370 | 387 | } else { |
| 371 | - $color .= dechex( $color_tmp[ $i ] ); | |
| 388 | + $color .= dechex( $color_parts[ $i ] ); | |
| 372 | 389 | } |
| 373 | 390 | } |
| 374 | 391 | } |
| 375 | 392 | |
| @@ -377,14 +394,20 @@ | ||
| 377 | 394 | if ( isset( $replace_colors[ strtolower( $color ) ] ) ) { |
| 378 | 395 | $color = $replace_colors[ strtolower( $color ) ]; |
| 379 | 396 | } |
| 380 | 397 | |
| 381 | - // #aabbcc -> #abc | |
| 382 | 398 | if ( 7 === strlen( $color ) ) { |
| 399 | + // #aabbcc -> #abc | |
| 383 | 400 | $color_temp = strtolower( $color ); |
| 384 | 401 | if ( '#' === $color_temp[0] && $color_temp[1] === $color_temp[2] && $color_temp[3] === $color_temp[4] && $color_temp[5] === $color_temp[6] ) { |
| 385 | 402 | $color = '#' . $color[1] . $color[3] . $color[5]; |
| 386 | 403 | } |
| 404 | + } elseif ( 9 === strlen( $color ) ) { | |
| 405 | + // #aabbccdd -> #abcd | |
| 406 | + $color_temp = strtolower( $color ); | |
| 407 | + if ( '#' === $color_temp[0] && $color_temp[1] === $color_temp[2] && $color_temp[3] === $color_temp[4] && $color_temp[5] === $color_temp[6] && $color_temp[7] === $color_temp[8] ) { | |
| 408 | + $color = '#' . $color[1] . $color[3] . $color[5] . $color[7]; | |
| 409 | + } | |
| 387 | 410 | } |
| 388 | 411 | |
| 389 | 412 | switch ( strtolower( $color ) ) { |
| 390 | 413 | /* color name -> hex code */ |
| @@ -430,13 +453,13 @@ | ||
| 430 | 453 | * |
| 431 | 454 | * @param string $subvalue Value. |
| 432 | 455 | * @return string Compressed value. |
| 433 | 456 | */ |
| 434 | - public function compress_numbers( $subvalue ) { | |
| 457 | + public function compress_numbers( string $subvalue ): string { | |
| 435 | 458 | $unit_values = &$this->parser->data['csstidy']['unit_values']; |
| 436 | 459 | $color_values = &$this->parser->data['csstidy']['color_values']; |
| 437 | 460 | |
| 438 | - // for font:1em/1em sans-serif...; | |
| 461 | + // for font:1em/1em sans-serif...;. | |
| 439 | 462 | if ( 'font' === $this->property ) { |
| 440 | 463 | $temp = explode( '/', $subvalue ); |
| 441 | 464 | } else { |
| 442 | 465 | $temp = array( $subvalue ); |
| @@ -444,15 +467,15 @@ | ||
| 444 | 467 | |
| 445 | 468 | $temp_count = count( $temp ); |
| 446 | 469 | for ( $l = 0; $l < $temp_count; $l++ ) { |
| 447 | 470 | // If we are not dealing with a number at this point, do not optimize anything. |
| 448 | - $number = $this->AnalyseCssNumber( $temp[ $l ] ); | |
| 471 | + $number = $this->analyse_css_number( $temp[ $l ] ); | |
| 449 | 472 | if ( false === $number ) { |
| 450 | 473 | return $subvalue; |
| 451 | 474 | } |
| 452 | 475 | |
| 453 | 476 | // Fix bad colors. |
| 454 | - if ( in_array( $this->property, $color_values ) ) { | |
| 477 | + if ( in_array( $this->property, $color_values, true ) ) { | |
| 455 | 478 | if ( 3 === strlen( $temp[ $l ] ) || 6 === strlen( $temp[ $l ] ) ) { |
| 456 | 479 | $temp[ $l ] = '#' . $temp[ $l ]; |
| 457 | 480 | } else { |
| 458 | 481 | $temp[ $l ] = '0'; |
| @@ -478,14 +501,14 @@ | ||
| 478 | 501 | * Checks if a given string is a CSS valid number. If it is, an array containing the value and unit is returned. |
| 479 | 502 | * |
| 480 | 503 | * @since 1.0.0 |
| 481 | 504 | * |
| 482 | - * @param string $string String. | |
| 483 | - * @return array|false ('unit' if unit is found or '' if no unit exists, number value) or false if no number. | |
| 505 | + * @param string $a_string String. | |
| 506 | + * @return array{int|string, string}|false ('unit' if unit is found or '' if no unit exists, number value) or false if no number. | |
| 484 | 507 | */ |
| 485 | - public function analyseCssNumber( $string ) { | |
| 486 | - // most simple checks first | |
| 487 | - if ( 0 === strlen( $string ) || ctype_alpha( $string[0] ) ) { | |
| 508 | + public function analyse_css_number( string $a_string ) /* : array|false */ { | |
| 509 | + // Most simple checks first. | |
| 510 | + if ( 0 === strlen( $a_string ) || ctype_alpha( $a_string[0] ) ) { | |
| 488 | 511 | return false; |
| 489 | 512 | } |
| 490 | 513 | |
| 491 | 514 | $units = &$this->parser->data['csstidy']['units']; |
| @@ -490,31 +513,31 @@ | ||
| 490 | 513 | |
| 491 | 514 | $units = &$this->parser->data['csstidy']['units']; |
| 492 | 515 | $return = array( 0, '' ); |
| 493 | 516 | |
| 494 | - $return[0] = (float) $string; | |
| 517 | + $return[0] = (float) $a_string; | |
| 495 | 518 | if ( abs( $return[0] ) > 0 && abs( $return[0] ) < 1 ) { |
| 496 | 519 | if ( $return[0] < 0 ) { |
| 497 | - $return[0] = '-' . ltrim( substr( $return[0], 1 ), '0' ); | |
| 520 | + $return[0] = '-' . ltrim( substr( (string) $return[0], 1 ), '0' ); | |
| 498 | 521 | } else { |
| 499 | - $return[0] = ltrim( $return[0], '0' ); | |
| 522 | + $return[0] = ltrim( (string) $return[0], '0' ); | |
| 500 | 523 | } |
| 501 | 524 | } |
| 502 | 525 | |
| 503 | - // Look for unit and split from value if exists | |
| 526 | + // Look for unit and split from value if exists. | |
| 504 | 527 | foreach ( $units as $unit ) { |
| 505 | - $expectUnitAt = strlen( $string ) - strlen( $unit ); | |
| 506 | - if ( ! ( $unitInString = stristr( $string, $unit ) ) ) { // mb_strpos() fails with "false" | |
| 528 | + $expect_unit_at = strlen( $a_string ) - strlen( $unit ); | |
| 529 | + if ( ! ( $unit_in_string = stristr( $a_string, $unit ) ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.Found,Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure | |
| 507 | 530 | continue; |
| 508 | 531 | } |
| 509 | - $actualPosition = strpos( $string, $unitInString ); | |
| 510 | - if ( $expectUnitAt === $actualPosition ) { | |
| 532 | + $actual_position = strpos( $a_string, $unit_in_string ); | |
| 533 | + if ( $expect_unit_at === $actual_position ) { | |
| 511 | 534 | $return[1] = $unit; |
| 512 | - $string = substr( $string, 0, - strlen( $unit ) ); | |
| 535 | + $a_string = substr( $a_string, 0, - strlen( $unit ) ); | |
| 513 | 536 | break; |
| 514 | 537 | } |
| 515 | 538 | } |
| 516 | - if ( ! is_numeric( $string ) ) { | |
| 539 | + if ( ! is_numeric( $a_string ) ) { | |
| 517 | 540 | return false; |
| 518 | 541 | } |
| 519 | 542 | return $return; |
| 520 | 543 | } |
| @@ -524,12 +547,12 @@ | ||
| 524 | 547 | * Very basic and has at least one bug. Hopefully there is a replacement soon. |
| 525 | 548 | * |
| 526 | 549 | * @since 1.0.0 |
| 527 | 550 | * |
| 528 | - * @param array $array List of selectors. This parameter is modified by reference! | |
| 551 | + * @param array<string, mixed> $an_array List of selectors. This parameter is modified by reference. | |
| 529 | 552 | */ |
| 530 | - public function merge_selectors( array &$array ) { | |
| 531 | - $css = $array; | |
| 553 | + public function merge_selectors( array &$an_array ): void { | |
| 554 | + $css = $an_array; | |
| 532 | 555 | foreach ( $css as $key => $value ) { |
| 533 | 556 | if ( ! isset( $css[ $key ] ) ) { |
| 534 | 557 | continue; |
| 535 | 558 | } |
| @@ -535,9 +558,9 @@ | ||
| 535 | 558 | } |
| 536 | 559 | |
| 537 | 560 | // Check if properties also exist in another selector. |
| 538 | 561 | $keys = array(); |
| 539 | - // PHP bug (?) without $css = $array; here | |
| 562 | + // PHP bug (?) without $css = $an_array; here. | |
| 540 | 563 | foreach ( $css as $selector => $vali ) { |
| 541 | 564 | if ( $selector === $key ) { |
| 542 | 565 | continue; |
| 543 | 566 | } |
| @@ -556,9 +579,9 @@ | ||
| 556 | 579 | } |
| 557 | 580 | $css[ $newsel ] = $value; |
| 558 | 581 | } |
| 559 | 582 | } |
| 560 | - $array = $css; | |
| 583 | + $an_array = $css; | |
| 561 | 584 | } |
| 562 | 585 | |
| 563 | 586 | /** |
| 564 | 587 | * Removes invalid selectors and their corresponding rule-sets as |
| @@ -567,12 +590,12 @@ | ||
| 567 | 590 | * regular expression. |
| 568 | 591 | * |
| 569 | 592 | * @since 1.0.0 |
| 570 | 593 | * |
| 571 | - * @param [type] $array [description] | |
| 594 | + * @param array<string, mixed> $an_array [description]. | |
| 572 | 595 | */ |
| 573 | - public function discard_invalid_selectors( &$array ) { | |
| 574 | - foreach ( $array as $selector => $decls ) { | |
| 596 | + public function discard_invalid_selectors( array &$an_array ): void { | |
| 597 | + foreach ( $an_array as $selector => $decls ) { | |
| 575 | 598 | $ok = true; |
| 576 | 599 | $selectors = array_map( 'trim', explode( ',', $selector ) ); |
| 577 | 600 | foreach ( $selectors as $s ) { |
| 578 | 601 | $simple_selectors = preg_split( '/\s*[+>~\s]\s*/', $s ); |
| @@ -579,14 +602,13 @@ | ||
| 579 | 602 | foreach ( $simple_selectors as $ss ) { |
| 580 | 603 | if ( '' === $ss ) { |
| 581 | 604 | $ok = false; |
| 582 | 605 | } |
| 583 | - // could also check $ss for internal structure, | |
| 584 | - // but that probably would be too slow | |
| 606 | + // Could also check $ss for internal structure, but that probably would be too slow. | |
| 585 | 607 | } |
| 586 | 608 | } |
| 587 | 609 | if ( ! $ok ) { |
| 588 | - unset( $array[ $selector ] ); | |
| 610 | + unset( $an_array[ $selector ] ); | |
| 589 | 611 | } |
| 590 | 612 | } |
| 591 | 613 | } |
| 592 | 614 | |
| @@ -594,14 +616,13 @@ | ||
| 594 | 616 | * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;... |
| 595 | 617 | * |
| 596 | 618 | * @since 1.0.0 |
| 597 | 619 | * |
| 598 | - * @param string $property [description] | |
| 599 | - * @param string $value [description] | |
| 600 | - * | |
| 601 | - * @return [type] [description] | |
| 620 | + * @param string $property [description]. | |
| 621 | + * @param string $value [description]. | |
| 622 | + * @return array [description] | |
| 602 | 623 | */ |
| 603 | - public function dissolve_4value_shorthands( $property, $value ) { | |
| 624 | + public function dissolve_4value_shorthands( string $property, string $value ): array { | |
| 604 | 625 | $return = array(); |
| 605 | 626 | |
| 606 | 627 | $shorthands = &$this->parser->data['csstidy']['shorthands']; |
| 607 | 628 | if ( ! is_array( $shorthands[ $property ] ) ) { |
| @@ -642,37 +663,37 @@ | ||
| 642 | 663 | * Explodes a string as explode() does, however, not if $sep is escaped or within a string. |
| 643 | 664 | * |
| 644 | 665 | * @since 1.0.0 |
| 645 | 666 | * |
| 646 | - * @param string $sep Separator. | |
| 647 | - * @param string $string String. | |
| 648 | - * @return array | |
| 667 | + * @param string $sep Separator. | |
| 668 | + * @param string $a_string String. | |
| 669 | + * @return array [description] | |
| 649 | 670 | */ |
| 650 | - public function explode_ws( $sep, $string ) { | |
| 671 | + public function explode_ws( string $sep, string $a_string ): array { | |
| 651 | 672 | $status = 'st'; |
| 652 | 673 | $to = ''; |
| 653 | 674 | |
| 654 | 675 | $output = array(); |
| 655 | 676 | $num = 0; |
| 656 | - for ( $i = 0, $len = strlen( $string ); $i < $len; $i++ ) { | |
| 677 | + for ( $i = 0, $len = strlen( $a_string ); $i < $len; $i++ ) { | |
| 657 | 678 | switch ( $status ) { |
| 658 | 679 | case 'st': |
| 659 | - if ( $string[ $i ] === $sep && ! $this->parser->escaped( $string, $i ) ) { | |
| 680 | + if ( $a_string[ $i ] === $sep && ! $this->parser->escaped( $a_string, $i ) ) { | |
| 660 | 681 | ++$num; |
| 661 | - } elseif ( '"' === $string[ $i ] || "'" === $string[ $i ] || '(' === $string[ $i ] && ! $this->parser->escaped( $string, $i ) ) { | |
| 682 | + } elseif ( '"' === $a_string[ $i ] || "'" === $a_string[ $i ] || ( '(' === $a_string[ $i ] && ! $this->parser->escaped( $a_string, $i ) ) ) { | |
| 662 | 683 | $status = 'str'; |
| 663 | - $to = ( '(' === $string[ $i ] ) ? ')' : $string[ $i ]; | |
| 664 | - ( isset( $output[ $num ] ) ) ? $output[ $num ] .= $string[ $i ] : $output[ $num ] = $string[ $i ]; | |
| 684 | + $to = ( '(' === $a_string[ $i ] ) ? ')' : $a_string[ $i ]; | |
| 685 | + ( isset( $output[ $num ] ) ) ? $output[ $num ] .= $a_string[ $i ] : $output[ $num ] = $a_string[ $i ]; | |
| 665 | 686 | } else { |
| 666 | - ( isset( $output[ $num ] ) ) ? $output[ $num ] .= $string[ $i ] : $output[ $num ] = $string[ $i ]; | |
| 687 | + ( isset( $output[ $num ] ) ) ? $output[ $num ] .= $a_string[ $i ] : $output[ $num ] = $a_string[ $i ]; | |
| 667 | 688 | } |
| 668 | 689 | break; |
| 669 | 690 | |
| 670 | 691 | case 'str': |
| 671 | - if ( $string[ $i ] === $to && ! $this->parser->escaped( $string, $i ) ) { | |
| 692 | + if ( $a_string[ $i ] === $to && ! $this->parser->escaped( $a_string, $i ) ) { | |
| 672 | 693 | $status = 'st'; |
| 673 | 694 | } |
| 674 | - ( isset( $output[ $num ] ) ) ? $output[ $num ] .= $string[ $i ] : $output[ $num ] = $string[ $i ]; | |
| 695 | + ( isset( $output[ $num ] ) ) ? $output[ $num ] .= $a_string[ $i ] : $output[ $num ] = $a_string[ $i ]; | |
| 675 | 696 | break; |
| 676 | 697 | } |
| 677 | 698 | } |
| 678 | 699 | |
| @@ -687,22 +708,22 @@ | ||
| 687 | 708 | * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands(). |
| 688 | 709 | * |
| 689 | 710 | * @since 1.0.0 |
| 690 | 711 | * |
| 691 | - * @param [type] $array [description] | |
| 692 | - * @return [type] [description] | |
| 712 | + * @param array<string, mixed> $an_array [description]. | |
| 713 | + * @return array<string, mixed> [description] | |
| 693 | 714 | */ |
| 694 | - public function merge_4value_shorthands( $array ) { | |
| 695 | - $return = $array; | |
| 715 | + public function merge_4value_shorthands( array $an_array ): array { | |
| 716 | + $return = $an_array; | |
| 696 | 717 | $shorthands = &$this->parser->data['csstidy']['shorthands']; |
| 697 | 718 | |
| 698 | 719 | foreach ( $shorthands as $key => $value ) { |
| 699 | - if ( 0 !== $value && isset( $array[ $value[0] ], $array[ $value[1] ], $array[ $value[2] ], $array[ $value[3] ] ) ) { | |
| 720 | + if ( 0 !== $value && isset( $an_array[ $value[0] ], $an_array[ $value[1] ], $an_array[ $value[2] ], $an_array[ $value[3] ] ) ) { | |
| 700 | 721 | $return[ $key ] = ''; |
| 701 | 722 | |
| 702 | 723 | $important = ''; |
| 703 | 724 | for ( $i = 0; $i < 4; $i++ ) { |
| 704 | - $val = $array[ $value[ $i ] ]; | |
| 725 | + $val = $an_array[ $value[ $i ] ]; | |
| 705 | 726 | if ( $this->parser->is_important( $val ) ) { |
| 706 | 727 | $important = ' !important'; |
| 707 | 728 | $return[ $key ] .= $this->parser->gvw_important( $val ) . ' '; |
| 708 | 729 | } else { |
| @@ -718,16 +739,16 @@ | ||
| 718 | 739 | |
| 719 | 740 | /** |
| 720 | 741 | * Dissolve background property. |
| 721 | 742 | * |
| 722 | - * @TODO Full CSS3 compliance. | |
| 743 | + * @todo Full CSS3 compliance. | |
| 723 | 744 | * |
| 724 | 745 | * @since 1.0.0 |
| 725 | 746 | * |
| 726 | 747 | * @param string $str_value String value. |
| 727 | - * @return array Array. | |
| 748 | + * @return array<string, string|null> Array. | |
| 728 | 749 | */ |
| 729 | - public function dissolve_short_bg( $str_value ) { | |
| 750 | + public function dissolve_short_bg( string $str_value ): array { | |
| 730 | 751 | // Don't try to explode background gradient! |
| 731 | 752 | if ( false !== stripos( $str_value, 'gradient(' ) ) { |
| 732 | 753 | return array( 'background' => $str_value ); |
| 733 | 754 | } |
| @@ -770,9 +791,9 @@ | ||
| 770 | 791 | $str_value[ $i ] = $this->explode_ws( ' ', trim( $str_value[ $i ] ) ); |
| 771 | 792 | |
| 772 | 793 | $str_value_i_count = count( $str_value[ $i ] ); |
| 773 | 794 | for ( $j = 0; $j < $str_value_i_count; $j++ ) { |
| 774 | - if ( false === $have['bg'] && ( 'url(' === substr( $str_value[ $i ][ $j ], 0, 4 ) || 'none' === $str_value[ $i ][ $j ] ) ) { | |
| 795 | + if ( false === $have['bg'] && ( str_starts_with( $str_value[ $i ][ $j ], 'url(' ) || 'none' === $str_value[ $i ][ $j ] ) ) { | |
| 775 | 796 | $return['background-image'] .= $str_value[ $i ][ $j ] . ','; |
| 776 | 797 | $have['bg'] = true; |
| 777 | 798 | } elseif ( in_array( $str_value[ $i ][ $j ], $repeat, true ) ) { |
| 778 | 799 | $return['background-repeat'] .= $str_value[ $i ][ $j ] . ','; |
| @@ -812,21 +833,21 @@ | ||
| 812 | 833 | |
| 813 | 834 | /** |
| 814 | 835 | * Merges all background properties. |
| 815 | 836 | * |
| 816 | - * @TODO Full CSS3 compliance. | |
| 837 | + * @todo Full CSS3 compliance. | |
| 817 | 838 | * |
| 818 | 839 | * @since 1.0.0 |
| 819 | 840 | * |
| 820 | - * @param array $input_css CSS. | |
| 821 | - * @return array Array. | |
| 841 | + * @param array<string, mixed> $input_css CSS. | |
| 842 | + * @return array<string, mixed> Array. | |
| 822 | 843 | */ |
| 823 | - public function merge_bg( array $input_css ) { | |
| 844 | + public function merge_bg( array $input_css ): array { | |
| 824 | 845 | $background_prop_default = &$this->parser->data['csstidy']['background_prop_default']; |
| 825 | 846 | // Max number of background images. CSS3 not yet fully implemented. |
| 826 | - $number_of_values = @max( count( $this->explode_ws( ',', $input_css['background-image'] ) ), count( $this->explode_ws( ',', $input_css['background-color'] ) ), 1 ); | |
| 847 | + $number_of_values = @max( count( $this->explode_ws( ',', $input_css['background-image'] ) ), count( $this->explode_ws( ',', $input_css['background-color'] ) ), 1 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | |
| 827 | 848 | // Array with background images to check if BG image exists. |
| 828 | - $bg_img_array = @$this->explode_ws( ',', $this->parser->gvw_important( $input_css['background-image'] ) ); | |
| 849 | + $bg_img_array = @$this->explode_ws( ',', $this->parser->gvw_important( $input_css['background-image'] ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | |
| 829 | 850 | $new_bg_value = ''; |
| 830 | 851 | $important = ''; |
| 831 | 852 | |
| 832 | 853 | // If background properties is here and not empty, don't try anything. |
| @@ -900,12 +921,12 @@ | ||
| 900 | 921 | * Dissolve font property. |
| 901 | 922 | * |
| 902 | 923 | * @since 1.0.0 |
| 903 | 924 | * |
| 904 | - * @param [type] $str_value [description] | |
| 905 | - * @return [type] [description] | |
| 925 | + * @param string $str_value [description]. | |
| 926 | + * @return array<string, string|null> [description] | |
| 906 | 927 | */ |
| 907 | - public function dissolve_short_font( $str_value ) { | |
| 928 | + public function dissolve_short_font( string $str_value ): array { | |
| 908 | 929 | $font_prop_default = &$this->parser->data['csstidy']['font_prop_default']; |
| 909 | 930 | $font_weight = array( 'normal', 'bold', 'bolder', 'lighter', 100, 200, 300, 400, 500, 600, 700, 800, 900 ); |
| 910 | 931 | $font_variant = array( 'normal', 'small-caps' ); |
| 911 | 932 | $font_style = array( 'normal', 'italic', 'oblique' ); |
| @@ -938,15 +959,15 @@ | ||
| 938 | 959 | $str_value[0] = $this->explode_ws( ' ', trim( $str_value[0] ) ); |
| 939 | 960 | |
| 940 | 961 | $str_value_0_count = count( $str_value[0] ); |
| 941 | 962 | for ( $j = 0; $j < $str_value_0_count; $j++ ) { |
| 942 | - if ( false === $have['weight'] && in_array( $str_value[0][ $j ], $font_weight ) ) { | |
| 963 | + if ( false === $have['weight'] && in_array( $str_value[0][ $j ], $font_weight, false ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.FoundNonStrictFalse | |
| 943 | 964 | $return['font-weight'] = $str_value[0][ $j ]; |
| 944 | 965 | $have['weight'] = true; |
| 945 | - } elseif ( false === $have['variant'] && in_array( $str_value[0][ $j ], $font_variant ) ) { | |
| 966 | + } elseif ( false === $have['variant'] && in_array( $str_value[0][ $j ], $font_variant, true ) ) { | |
| 946 | 967 | $return['font-variant'] = $str_value[0][ $j ]; |
| 947 | 968 | $have['variant'] = true; |
| 948 | - } elseif ( false === $have['style'] && in_array( $str_value[0][ $j ], $font_style ) ) { | |
| 969 | + } elseif ( false === $have['style'] && in_array( $str_value[0][ $j ], $font_style, true ) ) { | |
| 949 | 970 | $return['font-style'] = $str_value[0][ $j ]; |
| 950 | 971 | $have['style'] = true; |
| 951 | 972 | } elseif ( false === $have['size'] && ( is_numeric( $str_value[0][ $j ][0] ) || is_null( $str_value[0][ $j ][0] ) || '.' === $str_value[0][ $j ][0] ) ) { |
| 952 | 973 | $size = $this->explode_ws( '/', trim( $str_value[0][ $j ] ) ); |
| @@ -956,9 +977,9 @@ | ||
| 956 | 977 | } else { |
| 957 | 978 | $return['line-height'] = ''; // Don't add 'normal'! |
| 958 | 979 | } |
| 959 | 980 | $have['size'] = true; |
| 960 | - } else { | |
| 981 | + } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found | |
| 961 | 982 | if ( isset( $return['font-family'] ) ) { |
| 962 | 983 | $return['font-family'] .= ' ' . $str_value[0][ $j ]; |
| 963 | 984 | $multiwords = true; |
| 964 | 985 | } else { |
| @@ -972,9 +993,9 @@ | ||
| 972 | 993 | } |
| 973 | 994 | $i = 1; |
| 974 | 995 | while ( isset( $str_value[ $i ] ) ) { |
| 975 | 996 | $return['font-family'] .= ',' . trim( $str_value[ $i ] ); |
| 976 | - $i++; | |
| 997 | + ++$i; | |
| 977 | 998 | } |
| 978 | 999 | |
| 979 | 1000 | // Fix for font-size 100 and higher. |
| 980 | 1001 | if ( false === $have['size'] && isset( $return['font-weight'] ) && is_numeric( $return['font-weight'][0] ) ) { |
| @@ -996,12 +1017,12 @@ | ||
| 996 | 1017 | * Merges all fonts properties. |
| 997 | 1018 | * |
| 998 | 1019 | * @since 1.0.0 |
| 999 | 1020 | * |
| 1000 | - * @param [type] $input_css [description] | |
| 1001 | - * @return [type] [description] | |
| 1021 | + * @param array<string, string> $input_css [description]. | |
| 1022 | + * @return array<string, string> [description] | |
| 1002 | 1023 | */ |
| 1003 | - public function merge_font( $input_css ) { | |
| 1024 | + public function merge_font( array $input_css ): array { | |
| 1004 | 1025 | $font_prop_default = &$this->parser->data['csstidy']['font_prop_default']; |
| 1005 | 1026 | $new_font_value = ''; |
| 1006 | 1027 | $important = ''; |
| 1007 | 1028 | // Skip if no font-family and font-size set. |
| @@ -1006,23 +1027,21 @@ | ||
| 1006 | 1027 | $important = ''; |
| 1007 | 1028 | // Skip if no font-family and font-size set. |
| 1008 | 1029 | if ( isset( $input_css['font-family'], $input_css['font-size'] ) && 'inherit' !== $input_css['font-family'] ) { |
| 1009 | 1030 | // Fix several words in font-family - add quotes. |
| 1010 | - if ( isset( $input_css['font-family'] ) ) { | |
| 1011 | - $families = explode( ',', $input_css['font-family'] ); | |
| 1012 | - $result_families = array(); | |
| 1013 | - foreach ( $families as $family ) { | |
| 1014 | - $family = trim( $family ); | |
| 1015 | - $len = strlen( $family ); | |
| 1016 | - if ( strpos( $family, ' ' ) && | |
| 1017 | - ! ( ( '"' === $family[0] && '"' === $family[ $len - 1 ] ) || | |
| 1018 | - ( "'" === $family[0] && "'" === $family[ $len - 1 ] ) ) ) { | |
| 1019 | - $family = '"' . $family . '"'; | |
| 1020 | - } | |
| 1021 | - $result_families[] = $family; | |
| 1031 | + $families = explode( ',', $input_css['font-family'] ); | |
| 1032 | + $result_families = array(); | |
| 1033 | + foreach ( $families as $family ) { | |
| 1034 | + $family = trim( $family ); | |
| 1035 | + $len = strlen( $family ); | |
| 1036 | + if ( str_contains( $family, ' ' ) && | |
| 1037 | + ! ( ( '"' === $family[0] && '"' === $family[ $len - 1 ] ) || | |
| 1038 | + ( "'" === $family[0] && "'" === $family[ $len - 1 ] ) ) ) { | |
| 1039 | + $family = '"' . $family . '"'; | |
| 1022 | 1040 | } |
| 1023 | - $input_css['font-family'] = implode( ',', $result_families ); | |
| 1041 | + $result_families[] = $family; | |
| 1024 | 1042 | } |
| 1043 | + $input_css['font-family'] = implode( ',', $result_families ); | |
| 1025 | 1044 | foreach ( $font_prop_default as $font_property => $default_value ) { |
| 1026 | 1045 | // Skip if property does not exist. |
| 1027 | 1046 | if ( ! isset( $input_css[ $font_property ] ) ) { |
| 1028 | 1047 | continue; |
| @@ -1063,5 +1082,5 @@ | ||
| 1063 | 1082 | |
| 1064 | 1083 | return $input_css; |
| 1065 | 1084 | } |
| 1066 | 1085 | |
| 1067 | -} // class TablePress_CSSTidy_optimise | |
| 1086 | +} // class TablePress_CSSTidy_Optimise | |