Skip to content

DomMissingAttributeCheckedAsNull ​

warning native on by default

Group: Probable bugs

Use hasAttribute to test whether an attribute exists.

Example ​

php
<?php
function f(DOMElement $e) { if ($e->getAttribute('key') === null) {} }

Reported:

  • line 2: Use hasAttribute to test whether an attribute exists.

Detection ​

  • DOMElement getAttribute result is compared strictly with null, directly or via unchanged local binding. Both === and !== are covered. Do not report comparisons to empty string or tests using hasAttribute.
  • 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.
  • Do not report a call that a nullsafe operator can skip: either the getAttribute call itself uses ?->, or an earlier property fetch or method call in the same unparenthesised member chain does. Such an expression yields null whenever the nullsafe receiver is null, so the null comparison is meaningful and hasAttribute is not an equivalent test. This also applies when the result reaches the comparison through a local binding.
  • Both @custos-ignore DomMissingAttributeCheckedAsNull and @noinspection DomMissingAttributeCheckedAsNull 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": {
    "DomMissingAttributeCheckedAsNull": {
      "enabled": false
    }
  }
}

Suppress ​

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

php
// @custos-ignore DomMissingAttributeCheckedAsNull

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