Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Xml::arrayToXml fails on nested arrays

オープン 初心者向け
#7 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
65/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
php
領域
testing-qa

調査の方向性

Xml::arrayToXml ヘルパーから始め、issue のネストされた配列の例を再現します。ネストされた要素が存在する前に渡されている再帰分岐を調べ、その後、例が PHP エラーなしで期待されるネストされた XML を生成することを確認します。

索引モデルが issue の本文から書いたものです。

説明

The helper function arrayToXml has code indended to handle nested arrays, but currently always throws PHP errors. This means nested arrays can't be used when testing soap functions with array data either.

Reproduce

Code:

$dom = new \DOMDocument();
$array = [
    'foo' => [
        'bar' => '1'
    ]
];
echo \Codeception\Util\Xml::arrayToXml($dom, $dom, $array)->saveXML();

Expected output:

<?xml version="1.0"?>
<foo><bar>1</bar></foo>

Result:

Undefined property: DOMDocument::$foo
Cause

When the function encounters an array, it calls itself to parse the array, with $domNode->$el as the new $domNode parameter. The problem is that that node ->$el will never exist, since that's the node this function was supposed to create in the first place.

Fix

Create a new element first, to pass to the recursive call, and then append that new node to the xml structure.

Current code:

            if (is_array($val)) {
                self::arrayToXml($xml, $domNode->$el, $val);
            } else {
                $domNode->appendChild($xml->createElement($el, $val));
            }

Fixed code:

            if (is_array($val)) {
                $elementNode = $xml->createElement($el);
                self::arrayToXml($xml, $elementNode, $val);
                $domNode->appendChild($elementNode);
            } else {
                $domNode->appendChild($xml->createElement($el, $val));
            }

Alternatively, we could make it a little more consistent by always creating and appending the element in the same way, and only set the value inside the if-condition. That code would look like this:

            $elementNode = $xml->createElement($el);
            if (is_array($val)) {
                self::arrayToXml($xml, $elementNode, $val);
            } else {
                $elementNode->nodeValue = $val;
            }
            $domNode->appendChild($elementNode);

Both solutions result in working code for nested arrays.

主要言語
PHP
スター
17
フォーク
1
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Codeception/lib-xml のほかの issue

Codeception/lib-xml の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。