Skip to content

DomTextAssignmentDestroysChildren ​

warning native off by default

Group: Probable bugs

Preserve child nodes when adding element text. This inspection is disabled by default; enabling it selects the stated policy.

Example ​

php
<?php
$d=new DOMDocument(); $d->loadXML('<box><part/></box>'); $d->documentElement->nodeValue='added';

Reported:

  • line 2: Preserve child nodes when adding element text.

Detection ​

  • Assigning nodeValue or textContent on a proven DOMElement with existing element children is reported. Prove children through direct construction or literal loadXML. Enabling establishes child-preservation policy; text-only nodes and unknown children are excluded.
  • Resolve builtin functions, extension classes, constants and method receivers, including aliases and supported named arguments. Inspect reachable operations in one lexical scope with bounded local flow.
  • Emit one finding per violating operation. Never infer policy from names or comments. When the proof is inconclusive, nothing is reported.

Exceptions (no report) ​

  • Exclude shadowed or unresolved symbols, incomplete code, unknown required constants, ambiguous receiver identity and unsupported versions. Reassignment, escaping aliases, and unknown calls invalidate dependent flow facts.
  • A child proven through a variable appended to the element stops counting once a later statement could move or remove that node: passing the variable to any call (including appending it to another parent), calling a method on it other than attribute accessors, hasChildNodes or appendChild, or any use other than reading or writing one of its properties. Rebinding the variable to a new value leaves the appended node in place and keeps the proof.
  • Both @custos-ignore DomTextAssignmentDestroysChildren and @noinspection DomTextAssignmentDestroysChildren suppress this native rule.

Fix ​

None. A safe repair requires choosing values, error handling, or application behavior.

PHP versions ​

Apply throughout PHP 5.3–8.5 only where the extension API and syntax exist. Do not flag API unavailability through this rule. Legacy DOM classes are the initial target; modern Dom namespace classes require independently established matching contracts.

Options ​

None. Use normal rule configuration to enable or disable the inspection.

Configure ​

In custos.json:

json
{
  "rules": {
    "DomTextAssignmentDestroysChildren": {
      "enabled": true
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file):

php
// @custos-ignore DomTextAssignmentDestroysChildren

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.