Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PropertyAccess] SimpleXMLElement object property value cannot be set dynamically #53398

Closed
@renatogcarvalho

Description

@renatogcarvalho

Symfony version(s) affected

v5.4+

Description

The PropertyAccess component on version v5.4+ no longer can set a value to a SimpleXMLElement class property dynamically.

I noticed there were some changes made on #37622 and #44419, however this issue is still happening for the SimpleXMLElement class at least.

How to reproduce

Code snippet:

<?php

require_once 'vendor/autoload.php';

use Symfony\Component\PropertyAccess\PropertyAccess;

$xml = new \SimpleXMLElement(<<<XML
<root>
    <message></message>
</root>
XML
);

$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
    ->getPropertyAccessor();

dump($xml);

$propertyAccessor->setValue($xml, 'message', 123);

dump($xml);

Result on v4.4.44:

➜  sf-4-4 git:(main) ✗ php test.php
SimpleXMLElement {#2
  +"message": SimpleXMLElement {#16}
}
SimpleXMLElement {#2
  +"message": "123"
}

Result on v5.4.26:

➜  sf-5-4 git:(main) ✗ php test.php 
SimpleXMLElement {#2
  +"message": SimpleXMLElement {#20}
}
PHP Fatal error:  Uncaught Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException: Could not determine access type for property "message" in class "SimpleXMLElement". in /sf-5-4/vendor/symfony/property-access/PropertyAccessor.php:604
Stack trace:
#0 /sf-5-4/vendor/symfony/property-access/PropertyAccessor.php(175): Symfony\Component\PropertyAccess\PropertyAccessor->writeProperty(Array, 'message', 123)
#1 /sf-5-4/test.php(20): Symfony\Component\PropertyAccess\PropertyAccessor->setValue(Object(SimpleXMLElement), 'message', 123)
#2 {main}
  thrown in /sf-5-4/vendor/symfony/property-access/PropertyAccessor.php on line 604

Possible Solution

No response

Additional Context

In our use case we use the component to parse XML API responses, and sometimes need to make adjustments to certain node elements.

For the time being we can obviously adjust the code to set the property values directly (i.e. $xml->property = 'value'), however would be great if this can be fixed as it simplifies our implementation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions