MDL-86256 core: coding style cleanup for XML parsing classes.

This commit is contained in:
Paul Holden
2025-08-12 13:08:03 +01:00
parent 25bf66cf01
commit 0a41dacbec
2 changed files with 51 additions and 36 deletions
@@ -26,24 +26,23 @@ use stdClass;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class xml_format_exception extends moodle_exception {
/** @var string */
public $errorstring;
/** @var string */
public $char;
/**
* Constructor function
*
* @param string $errorstring Errorstring
* @param int $line Linenumber
* @param string $char Errorcharacter
* @param string|null $errorstring Error string
* @param int $line Line number
* @param int $char Character number
* @param string $link Link
*/
public function __construct($errorstring, $line, $char, $link = '') {
$this->errorstring = $errorstring;
$this->line = $line;
$this->char = $char;
public function __construct(
/** @var ?string Error string */
public ?string $errorstring,
/** @var int Line number */
public int $line,
/** @var int Character number */
public int $char,
string $link = '',
) {
$a = new stdClass();
$a->errorstring = $errorstring;
$a->errorline = $line;
+39 -23
View File
@@ -17,6 +17,7 @@
namespace core;
use core\exception\xml_format_exception;
use XMLParser;
/**
* Class for parsing xml files.
@@ -35,40 +36,44 @@ use core\exception\xml_format_exception;
*/
class xml_parser {
/** @var array resulting $xml tree */
private $xml;
private array $xml;
/** @var array stores references to constructed $xml tree */
private $current;
private array $current;
/** @var int tores the level in the XML tree */
private $level;
private int $level;
/**
* Is called when tags are opened.
*
* Note: Used by xml element handler as callback.
*
* @author Kilian Singer
* @param resource $parser The XML parser resource.
* @param XMLParser $parser The XML parser resource.
* @param string $name The XML source to parse.
* @param array $attrs Stores attributes of XML tag.
*/
private function startelement($parser, $name, $attrs) {
private function startelement(
XMLParser $parser,
string $name,
array $attrs,
): void {
$current = &$this->current;
$level = &$this->level;
if (!empty($name)) {
if ($level == 0) {
$current[$level][$name] = array();
$current[$level][$name] = [];
$current[$level][$name]["@"] = $attrs; // Attribute.
$current[$level][$name]["#"] = array(); // Other tags.
$current[$level][$name]["#"] = []; // Other tags.
$current[$level + 1] = & $current[$level][$name]["#"];
$level++;
} else {
if (empty($current[$level][$name])) {
$current[$level][$name] = array();
$current[$level][$name] = [];
}
$siz = count($current[$level][$name]);
if (!empty($attrs)) {
$current[$level][$name][$siz]["@"] = $attrs; // Attribute.
}
$current[$level][$name][$siz]["#"] = array(); // Other tags.
$current[$level][$name][$siz]["#"] = []; // Other tags.
$current[$level + 1] = & $current[$level][$name][$siz]["#"];
$level++;
}
@@ -80,11 +85,13 @@ class xml_parser {
*
* Note: Used by xml element handler as callback.
*
* @author Kilian Singer
* @param resource $parser The XML parser resource.
* @param XMLParser $parser The XML parser resource.
* @param string $name The XML source to parse.
*/
private function endelement($parser, $name) {
private function endelement(
XMLParser $parser,
string $name,
): void {
$current = &$this->current;
$level = &$this->level;
if (!empty($name)) {
@@ -104,11 +111,13 @@ class xml_parser {
*
* Note: Used by xml element handler as callback.
*
* @author Kilian Singer
* @param resource $parser The XML parser resource.
* @param XMLParser $parser The XML parser resource.
* @param string $data The XML source to parse.
*/
private function characterdata($parser, $data) {
private function characterdata(
XMLParser $parser,
string $data,
): void {
$current = &$this->current;
$level = &$this->level;
if (($data == "0") || (!empty($data) && trim($data) != "")) {
@@ -136,18 +145,23 @@ class xml_parser {
*
* Note: Interface is kept equal to previous version.
*
* @author Kilian Singer
* @param string $data the XML source to parse.
* @param int $whitespace If set to 1 allows the parser to skip "space" characters in xml document. Default is 1
* @param string $encoding Specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
* @param bool $reporterrors if set to true, then a {@link xml_format_exception}
* @param bool $reporterrors if set to true, then a {@see xml_format_exception}
* exception will be thrown if the XML is not well-formed. Otherwise errors are ignored.
* @return array|false representation of the parsed XML.
* @throws xml_format_exception
*/
public function parse($data, $whitespace = 1, $encoding = 'UTF-8', $reporterrors = false) {
public function parse(
string $data,
int $whitespace = 1,
string $encoding = 'UTF-8',
bool $reporterrors = false,
): array|false {
$data = trim($data);
$this->xml = array();
$this->current = array();
$this->xml = [];
$this->current = [];
$this->level = 0;
$this->current[0] = & $this->xml;
$parser = xml_parser_create($encoding);
@@ -164,9 +178,11 @@ class xml_parser {
if ($reporterrors) {
$errorcode = xml_get_error_code($parser);
if ($errorcode) {
$exception = new xml_format_exception(xml_error_string($errorcode),
$exception = new xml_format_exception(
xml_error_string($errorcode),
xml_get_current_line_number($parser),
xml_get_current_column_number($parser));
xml_get_current_column_number($parser),
);
xml_parser_free($parser);
throw $exception;
}