<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56354] trunk: Code Modernization: Deprecate dynamic properties in WP_Text_Diff_Renderer_Table magic methods.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56354">56354</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56354","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>hellofromTonya</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-08-03 18:11:36 +0000 (Thu, 03 Aug 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Code Modernization: Deprecate dynamic properties in WP_Text_Diff_Renderer_Table magic methods.
The unknown use of unknown dynamic property within the `WP_Text_Diff_Renderer_Table` property magic methods is now deprecated. A descriptive deprecation notice is provided to alert developers to declare the property on the child class extending `WP_Text_Diff_Renderer_Table`.
Changes in this commit:
* Adds a deprecation notice to the `__get()`, `__set()`, `__isset()`, `__unset()` magic methods, i.e. to alert and inform developers when attempting to get/set/isset/unset a dynamic property.
* Fixes `__get()` to explicitly returns `null` when attempting to get a dynamic property.
* Fixes `__set()` by removing the value return after setting a declared property, as (a) unnecessary and (b) `__set()` should return `void` [https://www.php.net/manual/en/language.oop5.overloading.php#object.set per the PHP handbook].
* Fixes `__isset()` to return `false` if not in the `$compat_fields`, as `isset()` and `__isset()` should always return `bool`:
* [https://www.php.net/manual/en/language.oop5.overloading.php#object.isset `__isset()` in the PHP manual]
* [https://www.php.net/manual/en/function.isset.php `isset()` in the PHP manual]
* Adds a test class with happy and unhappy paths for these changes.
For backward compatibility, no changes are made to the internal declared properties listed in `$compat_fields` and accessed through the magic methods.
For example:
A child class uses a property named `$data` that is not declared as a property on the child class. When getting its value, e.g. `$user_query->data`, the `WP_Text_Diff_Renderer_Table::__get()` magic method is invoked, the following deprecation notice thrown, and `null` returned:
>The property `data` is not declared. Setting a dynamic property is deprecated since version 6.4.0! Instead, declare the property on the class.
=== Why not remove the magic methods, remove the `$compat_fields` property, and restore the properties `public`?
tl;dr Backward compatibility.
If a plugin adds a property to `$compat_fields` array, then sites using that plugin would experience (a) an `Undefined property` `Warning` (PHP 8) | `Notice` (PHP 7) and (b) a possible change in behavior.
=== Why not limit the deprecation for PHP versions >= 8.2?
tl;dr original design intent and inform
The magic methods and `$compat_fields` property were added for one purpose: to continue providing external access to internal properties declared on `WP_Text_Diff_Renderer_Table`. They were not intended to be used for dynamic properties.
Deprecating that unintended usage both alerts developers a change is needed in their child class and informs them what to change.
References:
* Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.
* A [https://www.youtube.com/live/vDZWepDQQVE?feature=share&t=10097 live open public working session] where these changes were discussed and agreed to.
* [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties.]
Follow-up to <a href="https://core.trac.wordpress.org/changeset/28525">[28525]</a>, <a href="https://core.trac.wordpress.org/changeset/31135">[31135]</a>.
Props antonvlasenko, rajinsharwar, jrf, markjaquith, hellofromTonya, SergeyBiryukov, desrosj, peterwilsoncc, audrasjb, costdev, oglekler, jeffpaul.
Fixes <a href="https://core.trac.wordpress.org/ticket/58898">#58898</a>.
See <a href="https://core.trac.wordpress.org/ticket/56034">#56034</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswptextdiffrenderertablephp">trunk/src/wp-includes/class-wp-text-diff-renderer-table.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li>trunk/tests/phpunit/tests/diff/</li>
<li><a href="#trunktestsphpunittestsdiffwpTextDiffRendererTablephp">trunk/tests/phpunit/tests/diff/wpTextDiffRendererTable.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswptextdiffrenderertablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-text-diff-renderer-table.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-text-diff-renderer-table.php 2023-08-03 16:25:25 UTC (rev 56353)
+++ trunk/src/wp-includes/class-wp-text-diff-renderer-table.php 2023-08-03 18:11:36 UTC (rev 56354)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -511,14 +511,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Make private properties readable for backward compatibility.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.4.0 Getting a dynamic property is deprecated.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $name Property to get.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return mixed Property.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return mixed A declared property's value, else null.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function __get( $name ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( in_array( $name, $this->compat_fields, true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $this->$name;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ 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;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -525,15 +533,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Make private properties settable for backward compatibility.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.4.0 Setting a dynamic property is deprecated.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $name Property to check if set.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param mixed $value Property value.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return mixed Newly-set property.
</del><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function __set( $name, $value ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( in_array( $name, $this->compat_fields, true ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $this->$name = $value;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->$name = $value;
+ return;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ 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
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -540,6 +555,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Make private properties checkable for backward compatibility.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.4.0 Checking a dynamic property is deprecated.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $name Property to check if set.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool Whether the property is set.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -548,6 +564,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( in_array( $name, $this->compat_fields, true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return isset( $this->$name );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ 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;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -554,6 +577,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Make private properties un-settable for backward compatibility.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.4.0 Unsetting a dynamic property is deprecated.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $name Property to unset.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -560,6 +584,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function __unset( $name ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( in_array( $name, $this->compat_fields, true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $this->$name );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ 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
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsdiffwpTextDiffRendererTablephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/diff/wpTextDiffRendererTable.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/diff/wpTextDiffRendererTable.php (rev 0)
+++ trunk/tests/phpunit/tests/diff/wpTextDiffRendererTable.php 2023-08-03 18:11:36 UTC (rev 56354)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,161 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests for WP_Text_Diff_Renderer_Table.
+ *
+ * @group diff
+ */
+class Tests_Diff_WpTextDiffRendererTable extends WP_UnitTestCase {
+ /**
+ * @var WP_Text_Diff_Renderer_Table
+ */
+ private $diff_renderer_table;
+
+ public static function set_up_before_class() {
+ parent::set_up_before_class();
+ require_once ABSPATH . 'wp-includes/Text/Diff/Renderer.php';
+ require_once ABSPATH . 'wp-includes/class-wp-text-diff-renderer-table.php';
+ }
+
+ public function set_up() {
+ parent::set_up();
+ $this->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,
+ ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/diff/wpTextDiffRendererTable.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>