diff --git a/src/wp-includes/class-wp-text-diff-renderer-table.php b/src/wp-includes/class-wp-text-diff-renderer-table.php index 0174fed359..b54aa6c368 100644 --- a/src/wp-includes/class-wp-text-diff-renderer-table.php +++ b/src/wp-includes/class-wp-text-diff-renderer-table.php @@ -511,35 +511,51 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer { * Make private properties readable for backward compatibility. * * @since 4.0.0 + * @since 6.4.0 Getting a dynamic property is deprecated. * * @param string $name Property to get. - * @return mixed Property. + * @return mixed A declared property's value, else null. */ public function __get( $name ) { if ( in_array( $name, $this->compat_fields, true ) ) { return $this->$name; } + + trigger_error( + "The property `{$name}` is not declared. Getting a dynamic property is " . + 'deprecated since version 6.4.0! Instead, declare the property on the class.', + E_USER_DEPRECATED + ); + return null; } /** * Make private properties settable for backward compatibility. * * @since 4.0.0 + * @since 6.4.0 Setting a dynamic property is deprecated. * * @param string $name Property to check if set. * @param mixed $value Property value. - * @return mixed Newly-set property. */ public function __set( $name, $value ) { if ( in_array( $name, $this->compat_fields, true ) ) { - return $this->$name = $value; + $this->$name = $value; + return; } + + trigger_error( + "The property `{$name}` is not declared. Setting a dynamic property is " . + 'deprecated since version 6.4.0! Instead, declare the property on the class.', + E_USER_DEPRECATED + ); } /** * Make private properties checkable for backward compatibility. * * @since 4.0.0 + * @since 6.4.0 Checking a dynamic property is deprecated. * * @param string $name Property to check if set. * @return bool Whether the property is set. @@ -548,18 +564,33 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer { if ( in_array( $name, $this->compat_fields, true ) ) { return isset( $this->$name ); } + + trigger_error( + "The property `{$name}` is not declared. Checking `isset()` on a dynamic property " . + 'is deprecated since version 6.4.0! Instead, declare the property on the class.', + E_USER_DEPRECATED + ); + return false; } /** * Make private properties un-settable for backward compatibility. * * @since 4.0.0 + * @since 6.4.0 Unsetting a dynamic property is deprecated. * * @param string $name Property to unset. */ public function __unset( $name ) { if ( in_array( $name, $this->compat_fields, true ) ) { unset( $this->$name ); + return; } + + trigger_error( + "A property `{$name}` is not declared. Unsetting a dynamic property is " . + 'deprecated since version 6.4.0! Instead, declare the property on the class.', + E_USER_DEPRECATED + ); } } diff --git a/tests/phpunit/tests/diff/wpTextDiffRendererTable.php b/tests/phpunit/tests/diff/wpTextDiffRendererTable.php new file mode 100644 index 0000000000..4dbbdbbefe --- /dev/null +++ b/tests/phpunit/tests/diff/wpTextDiffRendererTable.php @@ -0,0 +1,161 @@ +diff_renderer_table = new WP_Text_Diff_Renderer_Table(); + } + + /** + * @dataProvider data_compat_fields + * @ticket 58898 + * + * @covers WP_Text_Diff_Renderer_Table::__get() + * + * @param string $property_name Property name to get. + * @param mixed $expected Expected value. + */ + public function test_should_get_compat_fields( $property_name, $expected ) { + $this->assertSame( $expected, $this->diff_renderer_table->$property_name ); + } + + /** + * @ticket 58898 + * + * @covers WP_Text_Diff_Renderer_Table::__get() + */ + public function test_should_throw_deprecation_when_getting_dynamic_property() { + $this->expectDeprecation(); + $this->expectDeprecationMessage( + 'The property `undeclared_property` is not declared. Getting a dynamic property is ' . + 'deprecated since version 6.4.0! Instead, declare the property on the class.' + ); + $this->assertNull( $this->diff_renderer_table->undeclared_property, 'Getting a dynamic property should return null from WP_Text_Diff_Renderer_Table::__get()' ); + } + + /** + * @dataProvider data_compat_fields + * @ticket 58898 + * + * @covers WP_Text_Diff_Renderer_Table::__set() + * + * @param string $property_name Property name to set. + */ + public function test_should_set_compat_fields( $property_name ) { + $value = uniqid(); + $this->diff_renderer_table->$property_name = $value; + + $this->assertSame( $value, $this->diff_renderer_table->$property_name ); + } + + /** + * @ticket 58898 + * + * @covers WP_Text_Diff_Renderer_Table::__set() + */ + public function test_should_throw_deprecation_when_setting_dynamic_property() { + $this->expectDeprecation(); + $this->expectDeprecationMessage( + 'The property `undeclared_property` is not declared. Setting a dynamic property is ' . + 'deprecated since version 6.4.0! Instead, declare the property on the class.' + ); + $this->diff_renderer_table->undeclared_property = 'some value'; + } + + /** + * @dataProvider data_compat_fields + * @ticket 58898 + * + * @covers WP_Text_Diff_Renderer_Table::__isset() + * + * @param string $property_name Property name to check. + * @param mixed $expected Expected value. + */ + public function test_should_isset_compat_fields( $property_name, $expected ) { + $actual = isset( $this->diff_renderer_table->$property_name ); + if ( is_null( $expected ) ) { + $this->assertFalse( $actual ); + } else { + $this->assertTrue( $actual ); + } + } + + /** + * @ticket 58898 + * + * @covers WP_Text_Diff_Renderer_Table::__isset() + */ + public function test_should_throw_deprecation_when_isset_of_dynamic_property() { + $this->expectDeprecation(); + $this->expectDeprecationMessage( + 'The property `undeclared_property` is not declared. Checking `isset()` on a dynamic property ' . + 'is deprecated since version 6.4.0! Instead, declare the property on the class.' + ); + $this->assertFalse( isset( $this->diff_renderer_table->undeclared_property ), 'Checking a dynamic property should return false from WP_Text_Diff_Renderer_Table::__isset()' ); + } + + /** + * @dataProvider data_compat_fields + * @ticket 58898 + * + * @covers WP_Text_Diff_Renderer_Table::__unset() + * + * @param string $property_name Property name to unset. + */ + public function test_should_unset_compat_fields( $property_name ) { + unset( $this->diff_renderer_table->$property_name ); + $this->assertFalse( isset( $this->diff_renderer_table->$property_name ) ); + } + + /** + * @ticket 58898 + * + * @covers WP_Text_Diff_Renderer_Table::__unset() + */ + public function test_should_throw_deprecation_when_unset_of_dynamic_property() { + $this->expectDeprecation(); + $this->expectDeprecationMessage( + 'A property `undeclared_property` is not declared. Unsetting a dynamic property is ' . + 'deprecated since version 6.4.0! Instead, declare the property on the class.' + ); + unset( $this->diff_renderer_table->undeclared_property ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_compat_fields() { + return array( + '_show_split_view' => array( + 'property_name' => '_show_split_view', + 'expected' => true, + ), + 'inline_diff_renderer' => array( + 'property_name' => 'inline_diff_renderer', + 'expected' => 'WP_Text_Diff_Renderer_inline', + ), + '_diff_threshold' => array( + 'property_name' => '_diff_threshold', + 'expected' => 0.6, + ), + ); + } +}