Code Modernization: Add `AllowDynamicProperties` attribute to all (parent) classes.

Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.

There are a number of ways to mitigate this:
* If it is an accidental typo for a declared property: fix the typo.
* For known properties: declare them on the class.
* For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in.
* For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes.

Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.

To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP.

The logic used for this commit is as follows:
* If a class already has the attribute: no action needed.
* If a class does not `extend`: add the attribute.
* If a class does `extend`:
 - If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties).
 - If it extends a PHP native class: add the attribute.
 - If it extends a class from one of WP's external dependencies: add the attribute.
* In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.

Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details.

This commit only affects classes in the `src` directory of WordPress core.
* Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
* While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.

Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties].

Follow-up to [53922].

Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.
Built from https://develop.svn.wordpress.org/trunk@54133


git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-09-12 15:47:14 +00:00
parent b0f4c2c37e
commit c03305852e
125 changed files with 126 additions and 1 deletions

View File

@ -11,6 +11,7 @@
*
* @since 3.0.0
*/
#[AllowDynamicProperties]
class Custom_Background {
/**

View File

@ -11,6 +11,7 @@
*
* @since 2.1.0
*/
#[AllowDynamicProperties]
class Custom_Image_Header {
/**

View File

@ -16,6 +16,7 @@
* @since 2.8.0
* @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
*/
#[AllowDynamicProperties]
class File_Upload_Upgrader {
/**

View File

@ -13,6 +13,7 @@
* @since 3.7.0
* @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
*/
#[AllowDynamicProperties]
class WP_Automatic_Updater {
/**

View File

@ -14,6 +14,7 @@
*
* @since 4.8.0
*/
#[AllowDynamicProperties]
class WP_Community_Events {
/**
* ID for a WordPress user account.

View File

@ -7,6 +7,7 @@
* @since 5.2.0
*/
#[AllowDynamicProperties]
class WP_Debug_Data {
/**
* Calls all core functions to check for updates.

View File

@ -11,6 +11,7 @@
*
* @since 2.5.0
*/
#[AllowDynamicProperties]
class WP_Filesystem_Base {
/**

View File

@ -2,6 +2,7 @@
/**
* WP_Importer base class
*/
#[AllowDynamicProperties]
class WP_Importer {
/**
* Class Constructor

View File

@ -12,6 +12,7 @@
*
* @since 3.3.0
*/
#[AllowDynamicProperties]
final class WP_Internal_Pointers {
/**
* Initializes the new feature pointers.

View File

@ -13,6 +13,7 @@
* @since 3.1.0
* @access private
*/
#[AllowDynamicProperties]
class WP_List_Table {
/**

View File

@ -7,6 +7,7 @@
* @since 4.9.6
*/
#[AllowDynamicProperties]
final class WP_Privacy_Policy_Content {
private static $policy_content = array();

View File

@ -12,6 +12,7 @@
*
* @since 3.3.0
*/
#[AllowDynamicProperties]
final class WP_Screen {
/**
* Any action associated with the screen.

View File

@ -7,6 +7,7 @@
* @since 5.2.0
*/
#[AllowDynamicProperties]
class WP_Site_Health_Auto_Updates {
/**
* WP_Site_Health_Auto_Updates constructor.

View File

@ -7,6 +7,7 @@
* @since 5.2.0
*/
#[AllowDynamicProperties]
class WP_Site_Health {
private static $instance = null;

View File

@ -12,6 +12,7 @@
*
* @since 4.3.0
*/
#[AllowDynamicProperties]
class WP_Site_Icon {
/**

View File

@ -13,6 +13,7 @@
* @since 2.8.0
* @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader-skins.php.
*/
#[AllowDynamicProperties]
class WP_Upgrader_Skin {
/**

View File

@ -48,6 +48,7 @@ require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
*
* @since 2.8.0
*/
#[AllowDynamicProperties]
class WP_Upgrader {
/**

View File

@ -12,6 +12,7 @@
*
* @since 3.1.0
*/
#[AllowDynamicProperties]
class WP_Admin_Bar {
private $nodes = array();
private $bound = false;

View File

@ -5,6 +5,7 @@
* @package WordPress
* @since 2.1.0
*/
#[AllowDynamicProperties]
class WP_Ajax_Response {
/**
* Store XML responses to send.

View File

@ -11,6 +11,7 @@
*
* @package WordPress
*/
#[AllowDynamicProperties]
class WP_Application_Passwords {
/**

View File

@ -11,6 +11,7 @@
*
* @since 5.8.0
*/
#[AllowDynamicProperties]
final class WP_Block_Editor_Context {
/**
* String that identifies the block editor being rendered. Can be one of:

View File

@ -11,6 +11,7 @@
*
* @since 5.5.0
*/
#[AllowDynamicProperties]
class WP_Block_List implements Iterator, ArrayAccess, Countable {
/**

View File

@ -10,6 +10,7 @@
/**
* Class used for interacting with block pattern categories.
*/
#[AllowDynamicProperties]
final class WP_Block_Pattern_Categories_Registry {
/**
* Registered block pattern categories array.

View File

@ -12,6 +12,7 @@
*
* @since 5.5.0
*/
#[AllowDynamicProperties]
final class WP_Block_Patterns_Registry {
/**
* Registered block patterns array.

View File

@ -12,6 +12,7 @@
*
* @since 5.3.0
*/
#[AllowDynamicProperties]
final class WP_Block_Styles_Registry {
/**
* Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays.

View File

@ -14,6 +14,7 @@
*
* @access private
*/
#[AllowDynamicProperties]
class WP_Block_Supports {
/**

View File

@ -11,6 +11,7 @@
*
* @since 5.8.0
*/
#[AllowDynamicProperties]
class WP_Block_Template {
/**

View File

@ -12,6 +12,7 @@
*
* @since 5.0.0
*/
#[AllowDynamicProperties]
final class WP_Block_Type_Registry {
/**
* Registered block types, as `$name => $instance` pairs.

View File

@ -14,6 +14,7 @@
*
* @see register_block_type()
*/
#[AllowDynamicProperties]
class WP_Block_Type {
/**

View File

@ -12,6 +12,7 @@
* @since 5.5.0
* @property array $attributes
*/
#[AllowDynamicProperties]
class WP_Block {
/**

View File

@ -14,6 +14,7 @@
*
* @see WP_Comment_Query::__construct() for accepted arguments.
*/
#[AllowDynamicProperties]
class WP_Comment_Query {
/**

View File

@ -12,6 +12,7 @@
*
* @since 4.4.0
*/
#[AllowDynamicProperties]
final class WP_Comment {
/**

View File

@ -12,6 +12,7 @@
*
* @since 3.4.0
*/
#[AllowDynamicProperties]
class WP_Customize_Control {
/**

View File

@ -20,6 +20,7 @@
*
* @since 3.4.0
*/
#[AllowDynamicProperties]
final class WP_Customize_Manager {
/**
* An instance of the theme being previewed.

View File

@ -16,6 +16,7 @@
*
* @see WP_Customize_Manager
*/
#[AllowDynamicProperties]
final class WP_Customize_Nav_Menus {
/**

View File

@ -16,6 +16,7 @@
*
* @see WP_Customize_Manager
*/
#[AllowDynamicProperties]
class WP_Customize_Panel {
/**

View File

@ -16,6 +16,7 @@
*
* @see WP_Customize_Manager
*/
#[AllowDynamicProperties]
class WP_Customize_Section {
/**

View File

@ -17,6 +17,7 @@
* @see WP_Customize_Manager
* @link https://developer.wordpress.org/themes/customize-api
*/
#[AllowDynamicProperties]
class WP_Customize_Setting {
/**
* Customizer bootstrap instance.

View File

@ -16,6 +16,7 @@
*
* @see WP_Customize_Manager
*/
#[AllowDynamicProperties]
final class WP_Customize_Widgets {
/**

View File

@ -14,6 +14,7 @@
*
* @since 3.7.0
*/
#[AllowDynamicProperties]
class WP_Date_Query {
/**
* Array of date queries.

View File

@ -16,6 +16,7 @@
* @access private
* @since 2.6.0
*/
#[AllowDynamicProperties]
class _WP_Dependency {
/**
* The handle name.

View File

@ -8,6 +8,7 @@
* Private, not included by default. See wp_editor() in wp-includes/general-template.php.
*/
#[AllowDynamicProperties]
final class _WP_Editors {
public static $mce_locale;

View File

@ -6,6 +6,7 @@
* @subpackage Embed
* @since 2.9.0
*/
#[AllowDynamicProperties]
class WP_Embed {
public $handlers = array();
public $post_ID;

View File

@ -15,6 +15,7 @@
*
* @since 2.1.0
*/
#[AllowDynamicProperties]
class WP_Error {
/**
* Stores the list of errors.

View File

@ -16,6 +16,7 @@
*
* @since 5.2.0
*/
#[AllowDynamicProperties]
class WP_Fatal_Error_Handler {
/**

View File

@ -12,6 +12,7 @@
*
* @since 2.8.0
*/
#[AllowDynamicProperties]
class WP_Feed_Cache_Transient {
/**

View File

@ -22,6 +22,7 @@ _deprecated_file(
*
* @see SimplePie_Cache
*/
#[AllowDynamicProperties]
class WP_Feed_Cache extends SimplePie_Cache {
/**

View File

@ -15,6 +15,7 @@
* @see Iterator
* @see ArrayAccess
*/
#[AllowDynamicProperties]
final class WP_Hook implements Iterator, ArrayAccess {
/**

View File

@ -18,6 +18,7 @@
*
* @since 2.8.0
*/
#[AllowDynamicProperties]
class WP_Http_Cookie {
/**

View File

@ -16,6 +16,7 @@
*
* @since 2.7.0
*/
#[AllowDynamicProperties]
class WP_Http_Curl {
/**

View File

@ -14,6 +14,7 @@
*
* @since 2.8.0
*/
#[AllowDynamicProperties]
class WP_Http_Encoding {
/**

View File

@ -5,6 +5,7 @@
* @package WordPress
* @since 3.1.0
*/
#[AllowDynamicProperties]
class WP_HTTP_IXR_Client extends IXR_Client {
public $scheme;
/**

View File

@ -39,6 +39,7 @@
*
* @since 2.8.0
*/
#[AllowDynamicProperties]
class WP_HTTP_Proxy {
/**

View File

@ -14,6 +14,7 @@
*
* @see Requests_Hooks
*/
#[AllowDynamicProperties]
class WP_HTTP_Requests_Hooks extends Requests_Hooks {
/**
* Requested URL.

View File

@ -12,6 +12,7 @@
*
* @since 4.4.0
*/
#[AllowDynamicProperties]
class WP_HTTP_Response {
/**

View File

@ -13,6 +13,7 @@
* @since 2.7.0
* @since 3.7.0 Combined with the fsockopen transport and switched to `stream_socket_client()`.
*/
#[AllowDynamicProperties]
class WP_Http_Streams {
/**
* Send a HTTP request to a URI using PHP Streams.

View File

@ -25,6 +25,7 @@ if ( ! class_exists( 'Requests' ) ) {
*
* @since 2.7.0
*/
#[AllowDynamicProperties]
class WP_Http {
// Aliases for HTTP response codes.

View File

@ -11,6 +11,7 @@
*
* @since 3.5.0
*/
#[AllowDynamicProperties]
abstract class WP_Image_Editor {
protected $file = null;
protected $size = null;

View File

@ -13,6 +13,7 @@
*
* @since 4.7.0
*/
#[AllowDynamicProperties]
class WP_List_Util {
/**
* The input array.

View File

@ -12,6 +12,7 @@
*
* @since 4.7.0
*/
#[AllowDynamicProperties]
class WP_Locale_Switcher {
/**
* Locale stack.

View File

@ -13,6 +13,7 @@
* @since 2.1.0
* @since 4.6.0 Moved to its own file from wp-includes/locale.php.
*/
#[AllowDynamicProperties]
class WP_Locale {
/**
* Stores the translated strings for the full weekday names.

View File

@ -11,6 +11,7 @@
*
* @since 2.9.0
*/
#[AllowDynamicProperties]
class WP_MatchesMapRegex {
/**
* store for matches

View File

@ -19,6 +19,7 @@
*
* @since 3.2.0
*/
#[AllowDynamicProperties]
class WP_Meta_Query {
/**
* Array of metadata queries.

View File

@ -28,6 +28,7 @@
*
* @since 4.5.0
*/
#[AllowDynamicProperties]
class WP_Metadata_Lazyloader {
/**
* Pending objects queue.

View File

@ -14,6 +14,7 @@
*
* @see WP_Network_Query::__construct() for accepted arguments.
*/
#[AllowDynamicProperties]
class WP_Network_Query {
/**

View File

@ -21,6 +21,7 @@
* @property int $id
* @property int $site_id
*/
#[AllowDynamicProperties]
class WP_Network {
/**

View File

@ -21,6 +21,7 @@
*
* @since 2.0.0
*/
#[AllowDynamicProperties]
class WP_Object_Cache {
/**

View File

@ -15,6 +15,7 @@
*
* @since 4.4.0
*/
#[AllowDynamicProperties]
final class WP_oEmbed_Controller {
/**
* Register the oEmbed REST API route.

View File

@ -16,6 +16,7 @@
*
* @since 2.9.0
*/
#[AllowDynamicProperties]
class WP_oEmbed {
/**

View File

@ -11,6 +11,7 @@
*
* @since 5.2.0
*/
#[AllowDynamicProperties]
class WP_Paused_Extensions_Storage {
/**

View File

@ -14,6 +14,7 @@
*
* @see register_post_type()
*/
#[AllowDynamicProperties]
final class WP_Post_Type {
/**
* Post type key.

View File

@ -18,6 +18,7 @@
* @property-read int[] $post_category
* @property-read string[] $tags_input
*/
#[AllowDynamicProperties]
final class WP_Post {
/**

View File

@ -15,6 +15,7 @@
* @since 1.5.0
* @since 4.5.0 Removed the `$comments_popup` property.
*/
#[AllowDynamicProperties]
class WP_Query {
/**

View File

@ -11,6 +11,7 @@
*
* @since 5.2.0
*/
#[AllowDynamicProperties]
final class WP_Recovery_Mode_Cookie_Service {
/**

View File

@ -11,6 +11,7 @@
*
* @since 5.2.0
*/
#[AllowDynamicProperties]
final class WP_Recovery_Mode_Email_Service {
const RATE_LIMIT_OPTION = 'recovery_mode_email_last_sent';

View File

@ -11,6 +11,7 @@
*
* @since 5.2.0
*/
#[AllowDynamicProperties]
final class WP_Recovery_Mode_Key_Service {
/**

View File

@ -11,6 +11,7 @@
*
* @since 5.2.0
*/
#[AllowDynamicProperties]
class WP_Recovery_Mode_Link_Service {
const LOGIN_ACTION_ENTER = 'enter_recovery_mode';
const LOGIN_ACTION_ENTERED = 'entered_recovery_mode';

View File

@ -11,6 +11,7 @@
*
* @since 5.2.0
*/
#[AllowDynamicProperties]
class WP_Recovery_Mode {
const EXIT_ACTION = 'exit_recovery_mode';

View File

@ -22,6 +22,7 @@
*
* @since 1.5.0
*/
#[AllowDynamicProperties]
class WP_Rewrite {
/**
* Permalink structure for posts.

View File

@ -12,6 +12,7 @@
*
* @since 2.0.0
*/
#[AllowDynamicProperties]
class WP_Role {
/**
* Role name.

View File

@ -23,6 +23,7 @@
*
* @since 2.0.0
*/
#[AllowDynamicProperties]
class WP_Roles {
/**
* List of roles and capabilities.

View File

@ -12,6 +12,7 @@
*
* @since 4.0.0
*/
#[AllowDynamicProperties]
abstract class WP_Session_Tokens {
/**

View File

@ -17,6 +17,7 @@
*
* @see SimplePie_File
*/
#[AllowDynamicProperties]
class WP_SimplePie_File extends SimplePie_File {
/**

View File

@ -17,6 +17,7 @@
*
* @see SimplePie_Sanitize
*/
#[AllowDynamicProperties]
class WP_SimplePie_Sanitize_KSES extends SimplePie_Sanitize {
/**

View File

@ -14,6 +14,7 @@
*
* @see WP_Site_Query::__construct() for accepted arguments.
*/
#[AllowDynamicProperties]
class WP_Site_Query {
/**

View File

@ -22,6 +22,7 @@
* @property int $post_count
* @property string $home
*/
#[AllowDynamicProperties]
final class WP_Site {
/**

View File

@ -19,6 +19,7 @@
*
* @since 3.1.0
*/
#[AllowDynamicProperties]
class WP_Tax_Query {
/**

View File

@ -12,6 +12,7 @@
*
* @since 4.7.0
*/
#[AllowDynamicProperties]
final class WP_Taxonomy {
/**
* Taxonomy key.

View File

@ -15,6 +15,7 @@
*
* @see WP_Term_Query::__construct() for accepted arguments.
*/
#[AllowDynamicProperties]
class WP_Term_Query {
/**

View File

@ -14,6 +14,7 @@
*
* @property-read object $data Sanitized term data.
*/
#[AllowDynamicProperties]
final class WP_Term {
/**

View File

@ -13,6 +13,7 @@
* @since 2.6.0
* @uses Text_Diff_Renderer_inline Extends
*/
#[AllowDynamicProperties]
class WP_Text_Diff_Renderer_inline extends Text_Diff_Renderer_inline {
/**

View File

@ -13,6 +13,7 @@
* @since 2.6.0
* @uses Text_Diff_Renderer Extends
*/
#[AllowDynamicProperties]
class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
/**

View File

@ -12,6 +12,7 @@
*
* @since 6.1.0
*/
#[AllowDynamicProperties]
class WP_Textdomain_Registry {
/**
* List of domains and all their language directory paths for each locale.

View File

@ -17,6 +17,7 @@
*
* @access private
*/
#[AllowDynamicProperties]
class WP_Theme_JSON_Resolver {
/**

View File

@ -17,6 +17,7 @@
* @since 5.9.0
* @access private
*/
#[AllowDynamicProperties]
class WP_Theme_JSON_Schema {
/**

View File

@ -16,6 +16,7 @@
*
* @access private
*/
#[AllowDynamicProperties]
class WP_Theme_JSON {
/**

View File

@ -6,6 +6,7 @@
* @subpackage Theme
* @since 3.4.0
*/
#[AllowDynamicProperties]
final class WP_Theme implements ArrayAccess {
/**

View File

@ -14,6 +14,7 @@
*
* @see WP_User_Query::prepare_query() for information on accepted arguments.
*/
#[AllowDynamicProperties]
class WP_User_Query {
/**

View File

@ -6,6 +6,7 @@
*
* @since 4.9.6
*/
#[AllowDynamicProperties]
final class WP_User_Request {
/**
* Request ID.

View File

@ -36,6 +36,7 @@
* @property string $syntax_highlighting
* @property string $use_ssl
*/
#[AllowDynamicProperties]
class WP_User {
/**
* User data container.

Some files were not shown because too many files have changed in this diff Show More