Skip to content

Commit ccd93c6

Browse files
PHP compatibility fixes (#789)
* Use modern default flags for htmlspecialchars(). * Remove mixed type declarations for PHP 7.4 compat. * Explicitly mark parameters as nullable. * Ignore conditional use of WeakMap.
1 parent acc63e5 commit ccd93c6

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

tests/integration/API/CacheableAPIBaseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CacheableAPIBaseTest extends \Codeception\TestCase\WPTestCase {
2525
*
2626
* @throws ReflectionException
2727
*/
28-
public function test_do_remote_request( bool $is_cacheable, bool $force_refresh = null, bool $cache_exists = null, bool $should_load_from_cache = false ) {
28+
public function test_do_remote_request( bool $is_cacheable, ?bool $force_refresh = null, ?bool $cache_exists = null, bool $should_load_from_cache = false ) {
2929

3030
$request = $this->get_new_request_instance( $is_cacheable );
3131

@@ -357,7 +357,7 @@ public function provider_get_request_cache_lifetime(): array {
357357
*
358358
* @throws ReflectionException
359359
*/
360-
public function test_get_request_data_for_broadcast( bool $is_cacheable, bool $force_refresh = null, bool $should_cache = null ) {
360+
public function test_get_request_data_for_broadcast( bool $is_cacheable, ?bool $force_refresh = null, ?bool $should_cache = null ) {
361361

362362
$request = $this->get_new_request_instance( $is_cacheable );
363363

woocommerce/Helpers/OrderHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static function get_customer_id( \WC_Order $order ) {
7878
* @param mixed $default Optional. The default value if the property doesn't exist. Default null.
7979
* @return mixed The property value if found, or the default value if not found.
8080
*/
81-
public static function get_property( \WC_Order $order, string $key, $nested_key = null, $default = null ): mixed {
81+
public static function get_property( \WC_Order $order, string $key, $nested_key = null, $default = null ) {
8282
return Dynamic_Props::get( $order, $key, $nested_key, $default );
8383
}
8484

woocommerce/payment-gateway/Dynamic_Props.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Dynamic_Props {
4242
* @since x.x.x
4343
* @var \WeakMap<object, \stdClass>|null
4444
*/
45-
private static ?\WeakMap $map = null;
45+
private static ?\WeakMap $map = null; // phpcs:ignore PHPCompatibility.Classes.NewClasses.weakmapFound -- conditionally used for PHP 8.0+
4646

4747
/**
4848
* Sets a property on the order object.
@@ -64,7 +64,7 @@ class Dynamic_Props {
6464
* Dynamic_Props::set($order, 'payment_total', '99.99');
6565
* ```
6666
*/
67-
public static function set( \WC_Order &$order, string $key, mixed $value ): void {
67+
public static function set( \WC_Order &$order, string $key, $value ): void {
6868
if ( self::use_weak_map() ) {
6969
self::init_weak_map();
7070
if ( ! isset( self::$map[ $order ] ) ) {
@@ -97,7 +97,7 @@ public static function set( \WC_Order &$order, string $key, mixed $value ): void
9797
* $token = Dynamic_Props::get($order, 'payment', 'token', 'DEFAULT_TOKEN');
9898
* ```
9999
*/
100-
public static function get( \WC_Order $order, string $key, $nested_key = null, $default = null ): mixed {
100+
public static function get( \WC_Order $order, string $key, $nested_key = null, $default = null ) {
101101
if ( self::use_weak_map() ) {
102102
self::init_weak_map();
103103
if ( is_null( $nested_key ) ) {
@@ -184,6 +184,7 @@ private static function use_weak_map(): bool {
184184
*/
185185
private static function init_weak_map(): void {
186186
if ( null === self::$map ) {
187+
// phpcs:ignore PHPCompatibility.Classes.NewClasses.weakmapFound -- conditionally used for PHP 8.0+
187188
self::$map = new \WeakMap();
188189
}
189190
}

woocommerce/payment-gateway/class-sv-wc-payment-gateway-direct.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ protected function get_notices_as_user_messages( ?string $type = null ) : array
484484
$message = $notice['notice'] ?? $notice;
485485

486486
// this will handle some log data eventually
487-
$messages[] = htmlspecialchars( is_array( $message ) ? print_r( $message, true ) : $message );
487+
$messages[] = htmlspecialchars( is_array( $message ) ? print_r( $message, true ) : $message, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
488488
}
489489
}
490490

woocommerce/payment-gateway/class-sv-wc-payment-gateway.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3753,12 +3753,12 @@ public function add_debug_message( $message, ?string $type = 'message' ) : void
37533753

37543754
if ( 'message' === $type ) {
37553755

3756-
SV_WC_Helper::wc_add_notice( str_replace( "\n", "<br/>", htmlspecialchars( $message ) ), 'notice' );
3756+
SV_WC_Helper::wc_add_notice( str_replace( "\n", "<br/>", htmlspecialchars( $message, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ), 'notice' );
37573757

37583758
} else {
37593759

37603760
// defaults to error message
3761-
SV_WC_Helper::wc_add_notice( str_replace( "\n", "<br/>", htmlspecialchars( $message ) ), 'error' );
3761+
SV_WC_Helper::wc_add_notice( str_replace( "\n", "<br/>", htmlspecialchars( $message, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ), 'error' );
37623762
}
37633763
}
37643764
}

0 commit comments

Comments
 (0)