Skip to content

Commit cbb79ca

Browse files
committed
Code Modernization: Address reflection no-op function deprecations in PHP 8.5.
`Reflection*::setAccessible()` methods are no-ops since PHP 8.1. This commit adds conditional checks to only call these functions on older PHP versions. Reference: [https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible PHP RFC: Deprecations for PHP 8.5: Deprecate `Reflection*::setAccessible()`]. Props rishabhwp, swissspidy. Fixes #63956. See #63061. git-svn-id: https://develop.svn.wordpress.org/trunk@60729 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c5909c4 commit cbb79ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+406
-136
lines changed

tests/phpunit/tests/admin/plugin-dependencies/base.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,13 @@ public function set_property_value( $property, $value ) {
8282
self::$reflected_members[ $property ] = new ReflectionProperty( self::$instance, $property );
8383
}
8484

85-
self::$reflected_members[ $property ]->setAccessible( true );
85+
if ( PHP_VERSION_ID < 80100 ) {
86+
self::$reflected_members[ $property ]->setAccessible( true );
87+
}
8688
self::$reflected_members[ $property ]->setValue( self::$instance, $value );
87-
self::$reflected_members[ $property ]->setAccessible( false );
89+
if ( PHP_VERSION_ID < 80100 ) {
90+
self::$reflected_members[ $property ]->setAccessible( false );
91+
}
8892
}
8993

9094
/**
@@ -98,9 +102,13 @@ public function get_property_value( $property ) {
98102
self::$reflected_members[ $property ] = new ReflectionProperty( self::$instance, $property );
99103
}
100104

101-
self::$reflected_members[ $property ]->setAccessible( true );
105+
if ( PHP_VERSION_ID < 80100 ) {
106+
self::$reflected_members[ $property ]->setAccessible( true );
107+
}
102108
$value = self::$reflected_members[ $property ]->getValue( self::$instance );
103-
self::$reflected_members[ $property ]->setAccessible( false );
109+
if ( PHP_VERSION_ID < 80100 ) {
110+
self::$reflected_members[ $property ]->setAccessible( false );
111+
}
104112

105113
return $value;
106114
}
@@ -118,9 +126,13 @@ protected function call_method( $method, ...$args ) {
118126
self::$reflected_members[ $method ] = new ReflectionMethod( self::$instance, $method );
119127
}
120128

121-
self::$reflected_members[ $method ]->setAccessible( true );
129+
if ( PHP_VERSION_ID < 80100 ) {
130+
self::$reflected_members[ $method ]->setAccessible( true );
131+
}
122132
$value = self::$reflected_members[ $method ]->invokeArgs( self::$instance, $args );
123-
self::$reflected_members[ $method ]->setAccessible( false );
133+
if ( PHP_VERSION_ID < 80100 ) {
134+
self::$reflected_members[ $method ]->setAccessible( false );
135+
}
124136

125137
return $value;
126138
}

tests/phpunit/tests/admin/wpAutomaticUpdater.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
3030
self::$updater = new WP_Automatic_Updater();
3131

3232
self::$send_plugin_theme_email = new ReflectionMethod( self::$updater, 'send_plugin_theme_email' );
33-
self::$send_plugin_theme_email->setAccessible( true );
33+
if ( PHP_VERSION_ID < 80100 ) {
34+
self::$send_plugin_theme_email->setAccessible( true );
35+
}
3436
}
3537

3638
public function set_up() {

tests/phpunit/tests/admin/wpCommunityEvents.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ protected function get_valid_events() {
299299
*/
300300
public function test_trim_expired_events() {
301301
$trim_events = new ReflectionMethod( $this->instance, 'trim_events' );
302-
$trim_events->setAccessible( true );
302+
if ( PHP_VERSION_ID < 80100 ) {
303+
$trim_events->setAccessible( true );
304+
}
303305

304306
$events = $this->get_valid_events();
305307

@@ -328,7 +330,9 @@ public function test_trim_expired_events() {
328330
*/
329331
public function test_trim_events_pin_wordcamp() {
330332
$trim_events = new ReflectionMethod( $this->instance, 'trim_events' );
331-
$trim_events->setAccessible( true );
333+
if ( PHP_VERSION_ID < 80100 ) {
334+
$trim_events->setAccessible( true );
335+
}
332336

333337
$actual = $trim_events->invoke( $this->instance, $this->_events_with_unpinned_wordcamp() );
334338

@@ -433,7 +437,9 @@ public function _events_with_unpinned_wordcamp() {
433437
*/
434438
public function test_trim_events_dont_pin_multiple_wordcamps() {
435439
$trim_events = new ReflectionMethod( $this->instance, 'trim_events' );
436-
$trim_events->setAccessible( true );
440+
if ( PHP_VERSION_ID < 80100 ) {
441+
$trim_events->setAccessible( true );
442+
}
437443

438444
$actual = $trim_events->invoke( $this->instance, $this->_events_with_multiple_wordcamps() );
439445

tests/phpunit/tests/admin/wpListTable.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ public function test_should_only_add_primary_column_when_needed( $list_class, $h
7070
$list_table = _get_list_table( $list_class );
7171

7272
$column_headers = new ReflectionProperty( $list_table, '_column_headers' );
73-
$column_headers->setAccessible( true );
73+
if ( PHP_VERSION_ID < 80100 ) {
74+
$column_headers->setAccessible( true );
75+
}
7476
$column_headers->setValue( $list_table, $headers );
7577

7678
$column_info = new ReflectionMethod( $list_table, 'get_column_info' );
77-
$column_info->setAccessible( true );
79+
if ( PHP_VERSION_ID < 80100 ) {
80+
$column_info->setAccessible( true );
81+
}
7882

7983
$this->assertSame( $expected, $column_info->invoke( $list_table ), 'The actual columns did not match the expected columns' );
8084
$this->assertSame( $expected_hook_count, $hook->get_call_count(), 'The hook was not called the expected number of times' );
@@ -160,7 +164,9 @@ public function data_should_only_add_primary_column_when_needed() {
160164
*/
161165
public function test_get_views_links( $link_data, $expected ) {
162166
$get_views_links = new ReflectionMethod( $this->list_table, 'get_views_links' );
163-
$get_views_links->setAccessible( true );
167+
if ( PHP_VERSION_ID < 80100 ) {
168+
$get_views_links->setAccessible( true );
169+
}
164170

165171
$actual = $get_views_links->invokeArgs( $this->list_table, array( $link_data ) );
166172

@@ -275,7 +281,9 @@ public function data_get_views_links() {
275281
*/
276282
public function test_get_views_links_doing_it_wrong( $link_data ) {
277283
$get_views_links = new ReflectionMethod( $this->list_table, 'get_views_links' );
278-
$get_views_links->setAccessible( true );
284+
if ( PHP_VERSION_ID < 80100 ) {
285+
$get_views_links->setAccessible( true );
286+
}
279287
$get_views_links->invokeArgs( $this->list_table, array( $link_data ) );
280288
}
281289

tests/phpunit/tests/admin/wpMediaListTable.php

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,21 @@ public static function set_up_before_class() {
7676
self::$is_trash = new ReflectionProperty( self::$list_table, 'is_trash' );
7777
self::$detached = new ReflectionProperty( self::$list_table, 'detached' );
7878

79-
self::$is_trash->setAccessible( true );
79+
if ( PHP_VERSION_ID < 80100 ) {
80+
self::$is_trash->setAccessible( true );
81+
}
8082
self::$is_trash_original = self::$is_trash->getValue( self::$list_table );
81-
self::$is_trash->setAccessible( false );
83+
if ( PHP_VERSION_ID < 80100 ) {
84+
self::$is_trash->setAccessible( false );
85+
}
8286

83-
self::$detached->setAccessible( true );
87+
if ( PHP_VERSION_ID < 80100 ) {
88+
self::$detached->setAccessible( true );
89+
}
8490
self::$detached_original = self::$detached->getValue( self::$list_table );
85-
self::$detached->setAccessible( false );
91+
if ( PHP_VERSION_ID < 80100 ) {
92+
self::$detached->setAccessible( false );
93+
}
8694

8795
// Create users.
8896
self::$admin = self::factory()->user->create( array( 'role' => 'administrator' ) );
@@ -181,9 +189,13 @@ public function test_get_row_actions_should_include_action( $action, $role, $tra
181189
}
182190

183191
$_get_row_actions = new ReflectionMethod( self::$list_table, '_get_row_actions' );
184-
$_get_row_actions->setAccessible( true );
192+
if ( PHP_VERSION_ID < 80100 ) {
193+
$_get_row_actions->setAccessible( true );
194+
}
185195
$actions = $_get_row_actions->invoke( self::$list_table, self::$post, 'att_title' );
186-
$_get_row_actions->setAccessible( false );
196+
if ( PHP_VERSION_ID < 80100 ) {
197+
$_get_row_actions->setAccessible( false );
198+
}
187199

188200
$this->assertIsArray( $actions, 'An array was not returned.' );
189201
$this->assertArrayHasKey( $action, $actions, "'$action' was not included in the actions." );
@@ -262,9 +274,13 @@ public function test_get_row_actions_should_not_include_action( $action, $role,
262274
}
263275

264276
$_get_row_actions = new ReflectionMethod( self::$list_table, '_get_row_actions' );
265-
$_get_row_actions->setAccessible( true );
277+
if ( PHP_VERSION_ID < 80100 ) {
278+
$_get_row_actions->setAccessible( true );
279+
}
266280
$actions = $_get_row_actions->invoke( self::$list_table, self::$post, 'att_title' );
267-
$_get_row_actions->setAccessible( false );
281+
if ( PHP_VERSION_ID < 80100 ) {
282+
$_get_row_actions->setAccessible( false );
283+
}
268284

269285
$this->assertIsArray( $actions, 'An array was not returned.' );
270286
$this->assertArrayNotHasKey( $action, $actions, "'$action' was included in the actions." );
@@ -355,9 +371,13 @@ public function test_get_row_actions_should_not_include_view_without_a_permalink
355371
add_filter( 'post_link', '__return_false', 10, 0 );
356372

357373
$_get_row_actions = new ReflectionMethod( self::$list_table, '_get_row_actions' );
358-
$_get_row_actions->setAccessible( true );
374+
if ( PHP_VERSION_ID < 80100 ) {
375+
$_get_row_actions->setAccessible( true );
376+
}
359377
$actions = $_get_row_actions->invoke( self::$list_table, self::$post, 'att_title' );
360-
$_get_row_actions->setAccessible( false );
378+
if ( PHP_VERSION_ID < 80100 ) {
379+
$_get_row_actions->setAccessible( false );
380+
}
361381

362382
$this->assertIsArray( $actions, 'An array was not returned.' );
363383
$this->assertArrayNotHasKey( 'view', $actions, '"view" was included in the actions.' );
@@ -374,9 +394,13 @@ public function test_get_row_actions_should_include_copy() {
374394
self::set_is_trash( false );
375395

376396
$_get_row_actions = new ReflectionMethod( self::$list_table, '_get_row_actions' );
377-
$_get_row_actions->setAccessible( true );
397+
if ( PHP_VERSION_ID < 80100 ) {
398+
$_get_row_actions->setAccessible( true );
399+
}
378400
$actions = $_get_row_actions->invoke( self::$list_table, self::$attachment, 'att_title' );
379-
$_get_row_actions->setAccessible( false );
401+
if ( PHP_VERSION_ID < 80100 ) {
402+
$_get_row_actions->setAccessible( false );
403+
}
380404

381405
$this->assertIsArray( $actions, 'An array was not returned.' );
382406
$this->assertArrayHasKey( 'copy', $actions, '"copy" was not included in the actions.' );
@@ -397,9 +421,13 @@ public function test_get_row_actions_should_not_include_copy_without_an_attachme
397421
add_filter( 'wp_get_attachment_url', '__return_false', 10, 0 );
398422

399423
$_get_row_actions = new ReflectionMethod( self::$list_table, '_get_row_actions' );
400-
$_get_row_actions->setAccessible( true );
424+
if ( PHP_VERSION_ID < 80100 ) {
425+
$_get_row_actions->setAccessible( true );
426+
}
401427
$actions = $_get_row_actions->invoke( self::$list_table, self::$attachment, 'att_title' );
402-
$_get_row_actions->setAccessible( false );
428+
if ( PHP_VERSION_ID < 80100 ) {
429+
$_get_row_actions->setAccessible( false );
430+
}
403431

404432
$this->assertIsArray( $actions, 'An array was not returned.' );
405433
$this->assertArrayNotHasKey( 'copy', $actions, '"copy" was included in the actions.' );
@@ -414,9 +442,13 @@ public function test_get_row_actions_should_not_include_copy_without_an_attachme
414442
*/
415443
public function test_get_row_actions_should_include_download() {
416444
$_get_row_actions = new ReflectionMethod( self::$list_table, '_get_row_actions' );
417-
$_get_row_actions->setAccessible( true );
445+
if ( PHP_VERSION_ID < 80100 ) {
446+
$_get_row_actions->setAccessible( true );
447+
}
418448
$actions = $_get_row_actions->invoke( self::$list_table, self::$attachment, 'att_title' );
419-
$_get_row_actions->setAccessible( false );
449+
if ( PHP_VERSION_ID < 80100 ) {
450+
$_get_row_actions->setAccessible( false );
451+
}
420452

421453
$this->assertIsArray( $actions, 'An array was not returned.' );
422454
$this->assertArrayHasKey( 'download', $actions, '"download" was not included in the actions.' );
@@ -435,9 +467,13 @@ public function test_get_row_actions_should_not_include_download_without_an_atta
435467
add_filter( 'wp_get_attachment_url', '__return_false', 10, 0 );
436468

437469
$_get_row_actions = new ReflectionMethod( self::$list_table, '_get_row_actions' );
438-
$_get_row_actions->setAccessible( true );
470+
if ( PHP_VERSION_ID < 80100 ) {
471+
$_get_row_actions->setAccessible( true );
472+
}
439473
$actions = $_get_row_actions->invoke( self::$list_table, self::$attachment, 'att_title' );
440-
$_get_row_actions->setAccessible( false );
474+
if ( PHP_VERSION_ID < 80100 ) {
475+
$_get_row_actions->setAccessible( false );
476+
}
441477

442478
$this->assertIsArray( $actions, 'An array was not returned.' );
443479
$this->assertArrayNotHasKey( 'download', $actions, '"download" was included in the actions.' );
@@ -451,9 +487,13 @@ public function test_get_row_actions_should_not_include_download_without_an_atta
451487
* @param bool $is_trash Whether the attachment filter is currently 'trash'.
452488
*/
453489
private static function set_is_trash( $is_trash ) {
454-
self::$is_trash->setAccessible( true );
490+
if ( PHP_VERSION_ID < 80100 ) {
491+
self::$is_trash->setAccessible( true );
492+
}
455493
self::$is_trash->setValue( self::$list_table, $is_trash );
456-
self::$is_trash->setAccessible( false );
494+
if ( PHP_VERSION_ID < 80100 ) {
495+
self::$is_trash->setAccessible( false );
496+
}
457497
}
458498

459499
/**
@@ -464,8 +504,12 @@ private static function set_is_trash( $is_trash ) {
464504
* @param bool $detached Whether the attachment filter is currently 'detached'.
465505
*/
466506
private static function set_detached( $detached ) {
467-
self::$detached->setAccessible( true );
507+
if ( PHP_VERSION_ID < 80100 ) {
508+
self::$detached->setAccessible( true );
509+
}
468510
self::$detached->setValue( self::$list_table, $detached );
469-
self::$detached->setAccessible( false );
511+
if ( PHP_VERSION_ID < 80100 ) {
512+
self::$detached->setAccessible( false );
513+
}
470514
}
471515
}

tests/phpunit/tests/admin/wpPluginsListTable.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,13 @@ public function test_construct_should_not_set_show_autoupdates_to_false_for_must
149149
$list_table = new WP_Plugins_List_Table();
150150
$show_autoupdates = new ReflectionProperty( $list_table, 'show_autoupdates' );
151151

152-
$show_autoupdates->setAccessible( true );
152+
if ( PHP_VERSION_ID < 80100 ) {
153+
$show_autoupdates->setAccessible( true );
154+
}
153155
$actual = $show_autoupdates->getValue( $list_table );
154-
$show_autoupdates->setAccessible( false );
156+
if ( PHP_VERSION_ID < 80100 ) {
157+
$show_autoupdates->setAccessible( false );
158+
}
155159

156160
$_REQUEST['plugin_status'] = $original_status;
157161

tests/phpunit/tests/admin/wpPrivacyRequestsTable.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@ public function get_mocked_class_instance() {
5252

5353
// Set the request type as 'export_personal_data'.
5454
$reflection_property = $reflection->getProperty( 'request_type' );
55-
$reflection_property->setAccessible( true );
55+
if ( PHP_VERSION_ID < 80100 ) {
56+
$reflection_property->setAccessible( true );
57+
}
5658
$reflection_property->setValue( $instance, 'export_personal_data' );
5759

5860
// Set the post type as 'user_request'.
5961
$reflection_property = $reflection->getProperty( 'post_type' );
60-
$reflection_property->setAccessible( true );
62+
if ( PHP_VERSION_ID < 80100 ) {
63+
$reflection_property->setAccessible( true );
64+
}
6165
$reflection_property->setValue( $instance, 'user_request' );
6266

6367
return $instance;

tests/phpunit/tests/admin/wpSiteHealth.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public function set_up() {
4040
public function test_mysql_recommended_version_matches_readme_html() {
4141
$reflection = new ReflectionClass( $this->instance );
4242
$reflection_property = $reflection->getProperty( 'mysql_recommended_version' );
43-
$reflection_property->setAccessible( true );
44-
43+
if ( PHP_VERSION_ID < 80100 ) {
44+
$reflection_property->setAccessible( true );
45+
}
4546
$readme = file_get_contents( ABSPATH . 'readme.html' );
4647

4748
preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]*)#s', $readme, $matches );
@@ -56,7 +57,9 @@ public function test_mysql_recommended_version_matches_readme_html() {
5657
public function test_mariadb_recommended_version_matches_readme_html() {
5758
$reflection = new ReflectionClass( $this->instance );
5859
$reflection_property = $reflection->getProperty( 'mariadb_recommended_version' );
59-
$reflection_property->setAccessible( true );
60+
if ( PHP_VERSION_ID < 80100 ) {
61+
$reflection_property->setAccessible( true );
62+
}
6063

6164
$readme = file_get_contents( ABSPATH . 'readme.html' );
6265

tests/phpunit/tests/admin/wpTermsListTable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public function set_up() {
4949
*/
5050
private function call_inaccessible_method( $instance, $method_name, $args = array() ) {
5151
$method = ( new ReflectionClass( $instance ) )->getMethod( $method_name );
52-
$method->setAccessible( true );
52+
if ( PHP_VERSION_ID < 80100 ) {
53+
$method->setAccessible( true );
54+
}
5355
return $method->invokeArgs( $instance, $args );
5456
}
5557

tests/phpunit/tests/admin/wpUpgrader.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,13 @@ public function data_init_should_initialize_strings() {
170170
*/
171171
public function test_flatten_dirlist_should_flatten_the_provided_directory_list( $expected, $nested_files, $path = '' ) {
172172
$flatten_dirlist = new ReflectionMethod( self::$instance, 'flatten_dirlist' );
173-
$flatten_dirlist->setAccessible( true );
173+
if ( PHP_VERSION_ID < 80100 ) {
174+
$flatten_dirlist->setAccessible( true );
175+
}
174176
$actual = $flatten_dirlist->invoke( self::$instance, $nested_files, $path );
175-
$flatten_dirlist->setAccessible( false );
177+
if ( PHP_VERSION_ID < 80100 ) {
178+
$flatten_dirlist->setAccessible( false );
179+
}
176180

177181
$this->assertSameSetsWithIndex( $expected, $actual );
178182
}

0 commit comments

Comments
 (0)