php / php/php-src

Readonly liskov violations in new Dom API

Open
#15,578 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: dom Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php
$xml = <<<END
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <child />
</root>
END;

$doc = Dom\XMLDocument::createFromString($xml);
$root = $doc->firstChild;
$text = $root->firstChild;
$child = $root->firstElementChild;

try {
    $text->nodeValue = 'Hello';
} catch (Throwable $t) {
    var_dump($t->getMessage());
}

try {
    $child->nodeValue = 'Hello';
} catch (Throwable $t) {
    var_dump($t->getMessage());
}

try {
    $doc->nodeValue = 'Hello';
} catch (Throwable $t) {
    var_dump($t->getMessage());
}


try {
    $text->textContent = 'Hello';
} catch (Throwable $t) {
    var_dump($t->getMessage());
}

try {
    $child->textContent = 'Hello';
} catch (Throwable $t) {
    var_dump($t->getMessage());
}

try {
    $doc->textContent = 'Hello';
} catch (Throwable $t) {
    var_dump($t->getMessage());
}

Resulted in this output:

string(55) "Cannot modify readonly property Dom\Element::$nodeValue"
string(59) "Cannot modify readonly property Dom\XMLDocument::$nodeValue"
string(61) "Cannot modify readonly property Dom\XMLDocument::$textContent"

But I expected this output instead:

In user code this would result in:

Fatal error: Cannot redeclare non-readonly property X::$y as readonly Y::$y

Since I've found multiple of these exploring the new Dom API it probably needs a closer look.

Tested in both docker (php:8.4-rc-alpine version 8.4.0beta3) and 3v4l

PHP Version

8.4.0beta3

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the readonly-property behavior from the issue's PHP example against the new Dom API, then inspect the declarations and inheritance relationships for nodeValue and textContent on Dom\Element and Dom\XMLDocument. Done means the reported Liskov-compatibility violations are resolved without the fatal readonly-property errors shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.