Skip to content

RepeatedHtmlEntityEncoding ​

warning native off by default

Group: Probable bugs

Detect htmlspecialchars or htmlentities directly nesting an equivalent encoding call with matching flags/encoding and default or true double_encode.

Example ​

php
<?php
echo htmlspecialchars(htmlspecialchars($text));

Reported:

  • line 2: Encode HTML entities at one intended output boundary.

Detection ​

  • Report htmlspecialchars or htmlentities directly nesting an equivalent encoding call with matching flags/encoding and default or true double_encode.
  • Resolve builtin calls and named arguments before checking the contract. Project-local callees are considered only when their bodies or immutable summaries establish the required facts. The default is disabled; enable only when the stated intent matches the application.
  • For flow-dependent conditions, require a reachable source-to-use path with no intervening invalidation. Unknown calls, ambiguous aliases, recursion or analysis-budget exhaustion cannot establish definite facts.

Exceptions (no report) ​

  • Different contexts/options, double_encode false on the outer call and intentionally literal entity rendering are excluded.
  • When either call receives an unpacked argument (...$options), its flags and encoding are unknown and may differ: no report.
  • Unresolved targets, incomplete required facts, suppressed findings and PHP versions lacking the referenced API or language feature produce no report. User declarations shadowing builtin names are not builtin contracts.

Fix ​

No automatic fix: repeated encoding can intentionally display entity syntax.

PHP versions ​

Apply only on PHP versions supporting the referenced APIs and syntax (within PHP 5.3–8.5). Builtin behavior and argument contracts follow the configured PHP version. Extension-specific calls require resolved extension API symbols; do not treat same-named application functions as extension calls.

Options ​

None. Enable or disable this rule through normal rule configuration.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore RepeatedHtmlEntityEncoding

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