Skip to content

Add toArrayFull()#10

Merged
byjg merged 2 commits intomasterfrom
5.0
Jul 3, 2025
Merged

Add toArrayFull()#10
byjg merged 2 commits intomasterfrom
5.0

Conversation

@byjg
Copy link
Owner

@byjg byjg commented Jul 3, 2025

Description by Korbit AI

What change is being made?

Add a new toFullArray() method to the XmlNode class while also modifying the existing toArray() method to handle both DOMNode and SimpleXMLElement inputs with an optional transformation function.

Why are these changes being made?

The changes provide more flexible and comprehensive XML-to-array conversion options. The toFullArray() captures all attributes and text nodes, whereas toArray() allows for customized processing of XML content, enabling easier manipulation of XML data in various applications. Adding comprehensive tests ensures the integrity and functionality of these methods.

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

@byjg byjg merged commit e7ee827 into master Jul 3, 2025
4 of 5 checks passed
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
Functionality Infinite Recursion Risk ▹ view
Design Complex Type Handling in Array Conversion ▹ 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 +316 to +320
protected function _toArray(SimpleXMLElement|DOMNode|array $arr, Closure|null $func = null): array
{
if ($arr instanceof SimpleXMLElement) {
return $this->_toArray((array) $arr, $func);
}
Copy link

Choose a reason for hiding this comment

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

Infinite Recursion Risk category Functionality

Tell me more
What is the issue?

The _toArray method contains potential infinite recursion when handling SimpleXMLElement instances.

Why this matters

If a SimpleXMLElement is passed, it will continuously cast to array and call itself, leading to a stack overflow.

Suggested change ∙ Feature Preview

Modify the method to handle SimpleXMLElement directly:

protected function _toArray(SimpleXMLElement|DOMNode|array $arr, Closure|null $func = null): array
{
    if ($arr instanceof SimpleXMLElement) {
        $arr = (array) $arr;
    } elseif ($arr instanceof DOMNode) {
        $arr = (array) simplexml_import_dom($arr);
    }

    $newArr = array();
    if (!empty($arr)) {
        foreach ($arr as $key => $value) {
            $newArr[$key] = (
                is_array($value)
                || ($value instanceof DOMNode)
                || ($value instanceof SimpleXMLElement)
                    ? $this->_toArray($value, $func)
                    : (!empty($func) ? $func($value) : $value)
            );
        }
    }
    return $newArr;
}
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 +326
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);
}

$newArr = array();
Copy link

Choose a reason for hiding this comment

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

Complex Type Handling in Array Conversion category Design

Tell me more
What is the issue?

Method accepts multiple types (SimpleXMLElement|DOMNode|array) and has recursive type casting, making the flow hard to follow and maintain.

Why this matters

Violates SOLID's Single Responsibility Principle by handling multiple type conversions in one method. This complexity makes the code harder to test and more prone to bugs.

Suggested change ∙ Feature Preview

Split into specialized methods for each type:

protected function convertSimpleXmlToArray(SimpleXMLElement $element): array;
protected function convertDomNodeToArray(DOMNode $node): array;
protected function processArrayValues(array $input): array;
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.

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