← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Magic Strings

Style Beginner
debt(d5/e3/b5/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list phpmd, phpcs, and phpstan — all specialist/linter-class tools rather than the compiler or a default linter. Repeated string literals across files can be flagged by these tools, but they require configuration and deliberate adoption. Not caught by the PHP compiler itself.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes replacing string literals with PHP 8.1 enums or class constants — a mechanical substitution pattern. For a single string it's trivial, but scattered usage across multiple call sites within a component typically requires a find-and-replace pass, landing it at e3 rather than e1.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). The applies_to covers web, cli, and queue-worker contexts — broad reach across the system. Common mistakes show magic strings scattered across the codebase affecting event names, config keys, status values, and error messages. Every future maintainer must search the codebase to find all usages, creating a persistent but not entirely system-defining tax.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap — a documented gotcha most devs eventually learn' (t5). The misconception field explicitly states that developers believe magic strings are only a problem when repeated, missing that even a single unexplained string like 'pending' is a magic string. This is a well-known code-smell trap that most developers encounter and learn, but it's not catastrophic or contradicting another language's behavior.

About DEBT scoring →

Also Known As

magic string hardcoded string literal unnamed string constant

TL;DR

Raw string literals used directly in code as identifiers or flags — prone to typos, hard to refactor, and lacking IDE support.

Explanation

Magic strings are the string equivalent of magic numbers: inline string literals like 'admin', 'pending', or 'user_created' used as status values, event names, or configuration keys. They cause typo bugs that aren't caught until runtime, resist refactoring (no IDE rename support), and offer no context to readers. Replace them with constants, enums (PHP 8.1+), or class constants — these are findable, renameable, and document valid values in one place.

Common Misconception

Magic strings are only a problem when the same string appears in multiple places. A single unexplained string like "pending" in a status check is still a magic string — a named constant Status::PENDING is refactoring-safe, self-documenting, and IDE-navigable.

Why It Matters

Magic strings are literal string values used directly in code without explanation or central definition — a typo in one copy goes undetected because there is no single source of truth to validate against.

Common Mistakes

  • Status values as strings scattered across the codebase: 'active', 'pending', 'inactive' — define as constants or enums.
  • Event names as raw strings: event('user.created') — a typo in the listener silently misses the event.
  • Config keys as inline strings repeated everywhere — one rename requires searching the whole codebase.
  • Error message strings duplicated in multiple places — they diverge and give users inconsistent messages.

Code Examples

✗ Vulnerable
if ($user->role === 'admin') { /* typo risk */ }
✓ Fixed
if ($user->role === Role::ADMIN->value) { /* enum backed by string */ }

Added 15 Mar 2026
Edited 22 Mar 2026
Views 74
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 1 ping W 1 ping T 1 ping F 0 pings S 2 pings S 2 pings M 0 pings T 0 pings W 2 pings T 0 pings F 4 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 1 ping S 4 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 17 ChatGPT 12 Ahrefs 6 Perplexity 6 Google 4 Unknown AI 4 Scrapy 3 Majestic 2 Claude 2 Bing 2 SEMrush 2 Meta AI 1 PetalBot 1
crawler 58 crawler_json 3 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Replace string literals used as status codes, types, or identifiers with PHP 8.1 enums or class constants — they're refactoring-safe and PHPStan can verify exhaustiveness
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
if ($status === 'active') or match($type) on string literals; same string literal repeated in multiple files
Auto-detectable: ✓ Yes phpmd phpcs phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✓ Auto-fixable Fix: Low Context: File


✓ schema.org compliant