Skip to content

QPXML::createElement() emits "Only variables should be passed by reference" for a prefixed element name #82

Description

@jakejackson1

Guidelines

Description of the bug

QPXML::createElement() emits a PHP notice whenever it is given a namespace-prefixed element name without an explicit $nsUri:

Notice: Only variables should be passed by reference in src/Extension/QPXML.php on line 194

The method still returns the right element, so this is a diagnostics-noise bug rather than a wrong-result one — but it will trip any application running with E_ALL and a strict error handler, and it fails builds that treat notices as errors.

Root cause

src/Extension/QPXML.php:194 passes a function's return value to array_shift(), which takes its argument by reference:

if ($nsUri === null && strpos($text, ':') !== false) {
	$ns    = array_shift(explode(':', $text));   // <-- notice
	$nsUri = $element->ownerDocument->lookupNamespaceURI($ns);

Suggested fix

No temporary needed — the prefix is just the part before the colon:

$ns    = strstr($text, ':', true);
$nsUri = $element->ownerDocument->lookupNamespaceURI($ns);

QueryPath version

4.2.0 (also reproduces on main at bed5d2c)

PHP Version and environment (server type, cli provider etc., enclosing libraries and their respective versions)

PHP 8.3.16 CLI (macOS, Homebrew). Present on every supported version — array_shift() has always taken its argument by reference.

Minimal reproducible PHP+HTML snippet to replicate bug

<?php
require __DIR__ . '/vendor/autoload.php';

use QueryPath\QueryPath;

QueryPath::enable(\QueryPath\Extension\QPXML::class);

$xml = '<?xml version="1.0"?><root xmlns:foo="http://example.com"><a/></root>';

qp($xml, 'a')->createElement('foo:bar');
// Notice: Only variables should be passed by reference in src/Extension/QPXML.php on line 194

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions