php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75168 Unexpected PHP Warning Node no longer exists
Submitted: 2017-09-07 12:33 UTC Modified: 2017-09-07 13:20 UTC
From: goetas at gmail dot com Assigned:
Status: Open Package: SimpleXML related
PHP Version: 7.0.23 OS: any
Private report: No CVE-ID:
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: goetas at gmail dot com
New email:
PHP Version: OS:

 

 [2017-09-07 12:33 UTC] goetas at gmail dot com
Description:
------------
currently the simplexml api exposes the SimpleXMLElement class when asking elements that does not exists in the XML dom, but some operations are inconsistent.


$xml // instanceof \SimpleXMLElement

$node = $xml->NOT_EXISTING_NODE_NAME; // instanceof \SimpleXMLElement

$a = $node->attributes()  // instanceof \SimpleXMLElement
$b = $node->children() // instanceof \SimpleXMLElement

$a->asXML() // works
$a->count() // error
$a->getName() // does not work
// all the other method call will trigger the error

Test script:
---------------
<?php

$xml = simplexml_load_string('<root/>');
var_dump($xml->NOT_EXISTING_NODE_NAME instanceof \SimpleXMLElement);

$attributes = $xml->NOT_EXISTING_NODE_NAME->attributes();
var_dump(count($attributes));

Expected result:
----------------
bool(true)
PHP Warning:  count(): Node no longer exists in /home/goetas/test.php on line 7
int(0)


Actual result:
--------------
bool(true)
PHP Warning:  count(): Node no longer exists in /home/goetas/test.php on line 7
int(0)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-07 13:06 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2017-09-07 13:06 UTC] requinix@php.net
According to this
  https://3v4l.org/1BHKS
you can use those methods on a non-existent child without warnings, but if you try them on attributes() or children() then you will get warnings.

Which makes sense to me: accessing a missing node and trying basic functions on it should be safe, but continuing on to using its attributes or children after you disregarded the opportunity to check for errors (ie, if the node is missing) should create warnings.

Anyway, are you asking to add warnings to the child or to remove warnings from the atttributes/children?
 [2017-09-07 13:20 UTC] goetas at gmail dot com
-Status: Feedback +Status: Open
 [2017-09-07 13:20 UTC] goetas at gmail dot com
Im asking to remove the warnings or to add some method to understand that the node does not exists (IMO should be done both)

There are more people online had similar issues.... https://stackoverflow.com/questions/2502038/simplexml-node-no-longer-exists 


I have a function that receives as argument an simplexmlelement instance and there is not way to avoid a notice being triggered.

More specifically, if you take a look at this function (https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/XmlDeserializationVisitor.php#L398) there is no way to understand if an notice is going to be triggered or not...
 [2017-09-07 13:23 UTC] goetas at gmail dot com
HHVM behavior in this case looks correct IMO
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC