Make WordPress Core

Changeset 59062


Ignore:
Timestamp:
09/18/2024 09:18:54 PM (8 months ago)
Author:
hellofromTonya
Message:

Code Modernization: Remove xml_set_object() in AtomParser::parse().

The XML Parser extension still supports a quite dated mechanism for method based callbacks, where the object is first set via xml_set_object() and the callbacks are then set by passing only the name of the method to the relevant parameters on any of the xml_set_*_handler() functions.

xml_set_object( $parser, $my_obj );
xml_set_character_data_handler( $parser, 'method_name_on_my_obj' );

Passing proper callables to the xml_set_*_handler() functions has been supported for the longest time and is cross-version compatible. So the above code is 100% equivalent to:

xml_set_character_data_handler( $parser, [$my_obj, 'method_name_on_my_obj'] );

The mechanism of setting the callbacks with xml_set_object() has now been deprecated as of PHP 8.4, in favour of passing proper callables to the xml_set_*_handler() functions. This is also means that calling the xml_set_object() function is deprecated as well.

This commit fixes this deprecation for the AtomParser::parse() method.

This change is safeguarded via the new AtomParser_Parse_Test class.

Notes:

  • Though this is "officially" an external library, this package is no longer externally maintained. The code style of the fix in the source file is in line with the existing code style for the file.
  • It appears that this class is not actually used by WP Core itself, so it could be considered to deprecate the class. However, as the class is not currently deprecated, safeguarding the change with a test seemed prudent.
  • The fixture used for the test reuses a fixture from the original package: https://code.google.com/archive/p/phpatomlib/source/default/source
  • The new test class follows the recommended test format (naming convention of the class, @covers tag at class level, only testing one method) as per Trac tickets 62004 / 53010.

Refs:

Follow-up to [5951].

Props jrf.
See #62061.

Location:
trunk
Files:
3 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/atomlib.php

    r59058 r59062  
    158158
    159159        $parser = xml_parser_create_ns();
    160         xml_set_object($parser, $this);
    161         xml_set_element_handler($parser, "start_element", "end_element");
     160        xml_set_element_handler($parser, array($this, "start_element"), array($this, "end_element"));
    162161        xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
    163162        xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
    164         xml_set_character_data_handler($parser, "cdata");
    165         xml_set_default_handler($parser, "_default");
    166         xml_set_start_namespace_decl_handler($parser, "start_ns");
    167         xml_set_end_namespace_decl_handler($parser, "end_ns");
     163        xml_set_character_data_handler($parser, array($this, "cdata"));
     164        xml_set_default_handler($parser, array($this, "_default"));
     165        xml_set_start_namespace_decl_handler($parser, array($this, "start_ns"));
     166        xml_set_end_namespace_decl_handler($parser, array($this, "end_ns"));
    168167
    169168        $this->content = '';
Note: See TracChangeset for help on using the changeset viewer.