Skip to content

Includes the older toArray conversion#9

Merged
byjg merged 1 commit intobyjg:5.0from
leonardomunsa:fix/to-array
Jul 3, 2025
Merged

Includes the older toArray conversion#9
byjg merged 1 commit intobyjg:5.0from
leonardomunsa:fix/to-array

Conversation

@leonardomunsa
Copy link
Contributor

@leonardomunsa leonardomunsa commented Jul 3, 2025

Description by Korbit AI

What change is being made?

Reintroduce a simplified conversion method toArray alongside the existing toFullArray method and update related tests.

Why are these changes being made?

The changes aim to enhance functionality by reintroducing the less detailed toArray method while preserving the existing toFullArray for comprehensive XML-to-array conversion. This allows for flexibility based on the required level of detail in various use cases, facilitating performance improvements where only a basic conversion is needed. The supplementary tests ensure compatibility and expected operation of both methods.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

@leonardomunsa leonardomunsa changed the base branch from master to 5.0 July 3, 2025 15:21
Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Error Handling Missing error handling for invalid XML in toFullArray ▹ view
Performance Inefficient Recursive Type Conversions ▹ view
Files scanned
File Path Reviewed
src/XmlNode.php

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Comment on lines +263 to 267
public function toFullArray(): array
{
$sxml = simplexml_import_dom($this->DOMNode());
return [$sxml->getName() => $this->_toArray($sxml)];
return [$sxml->getName() => $this->_toFullArray($sxml)];
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling for invalid XML in toFullArray category Error Handling

Tell me more
What is the issue?

The toFullArray() method doesn't handle the case where simplexml_import_dom() returns false, which can happen with invalid XML nodes.

Why this matters

If the XML node is invalid, the method will trigger a PHP error when trying to call getName() on a false value, causing unexpected crashes.

Suggested change ∙ Feature Preview

Add null check and throw an exception for invalid XML:

public function toFullArray(): array
{
    $sxml = simplexml_import_dom($this->DOMNode());
    if ($sxml === false) {
        throw new XmlUtilException('Failed to convert DOM node to SimpleXML');
    }
    return [$sxml->getName() => $this->_toFullArray($sxml)];
}
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Comment on lines +316 to +324
protected function _toArray(SimpleXMLElement|DOMNode|array $arr, Closure|null $func = null): array
{
if ($arr instanceof SimpleXMLElement) {
return $this->_toArray((array) $arr, $func);
}

if ($arr instanceof DOMNode) {
return $this->_toArray((array) simplexml_import_dom($arr), $func);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inefficient Recursive Type Conversions category Performance

Tell me more
What is the issue?

Multiple type casting and conversions in recursive calls create unnecessary object instantiations and type conversions.

Why this matters

Each recursive iteration potentially creates new object instances and performs type conversions, leading to memory churn and slower execution, especially for deeply nested XML structures.

Suggested change ∙ Feature Preview

Normalize the input type once at the entry point and work with a consistent type throughout the recursion:

protected function _toArray($input, ?Closure $func = null): array
{
    $arr = $this->normalizeToArray($input);
    $result = [];
    foreach ($arr as $key => $value) {
        $result[$key] = is_array($value) ? $this->_toArray($value, $func) : 
            (!empty($func) ? $func($value) : $value);
    }
    return $result;
}

private function normalizeToArray($input)
{
    if ($input instanceof SimpleXMLElement) return (array)$input;
    if ($input instanceof DOMNode) return (array)simplexml_import_dom($input);
    return $input;
}
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

@byjg byjg merged commit 5880d0c into byjg:5.0 Jul 3, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants