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

Interpreter Pattern

Code Quality Advanced
debt(d7/e5/b5/t3)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints indicate automated detection is 'no' and the tool listed (phpstan) would not flag misapplication of this design pattern — it cannot detect that you've used a giant switch/nested if-else where an interpreter pattern would be appropriate, nor that you've applied interpreter to an overly complex grammar. Only code review or runtime performance testing would reveal misuse.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix describes defining a grammar as multiple PHP classes (BooleanExpression, AndExpression, OrExpression), which means either introducing or replacing an existing ad-hoc implementation requires creating several new classes and reworking the evaluation logic across a component. Common mistakes like switching to a proper parser generator for complex grammars would also be a multi-file change.

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

Closest to 'persistent productivity tax' (b5). The pattern applies to web and cli contexts and affects the core rule/expression evaluation subsystem. Once adopted, every new grammar rule or business condition requires a new class, and all maintainers must understand the pattern's structure. It's not architectural (b7-b9) but it does impose a persistent tax on the component and teams touching DSL logic.

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

Closest to 'minor surprise (one edge case)' (t3). The misconception field states devs think 'Interpreter is for building general-purpose languages' when it's actually most useful for small DSLs with well-defined grammars. This is a scoping misconception rather than a behavioral gotcha — a competent developer will likely discover the correct usage relatively quickly, making it a minor rather than serious trap.

About DEBT scoring →

Also Known As

interpreter expression evaluator rule engine

TL;DR

A grammar represented as a class hierarchy — used for search query parsers, expression evaluators, and rule engines.

Explanation

Terminal expressions (values) and non-terminal expressions (AND, OR, NOT) form a composite tree. Parse input to build the tree; interpret evaluates it. PHP use cases: search query parsers, business rule engines. For complex grammars use a proper parser (PHP-Parser) rather than hand-rolling the Interpreter pattern.

Common Misconception

Interpreter is for building general-purpose languages — it is most useful for small DSLs with well-defined grammars.

Why It Matters

The interpreter pattern is most valuable when the grammar is simple and performance is not critical — configuration DSLs, rule engines, and template syntax. Implementing it teaches how parsers and evaluators work at a conceptual level, which transfers directly to understanding how PHP itself executes code. For complex or high-performance needs, it should be replaced by a proper parser generator or embedded scripting language.

Common Mistakes

  • Using for complex grammars — use proper parser
  • Not caching parsed expressions
  • No error handling in terminals

Code Examples

✗ Vulnerable
function evalRule(string $rule, Order $o): bool {
    if (preg_match('/total > (\d+)/',$rule,$m)) return $o->total > $m[1];
    // Cannot handle AND, OR
}
✓ Fixed
interface Expression { public function interpret(Context $ctx): bool; }
class GreaterThan implements Expression {
    public function __construct(private string $f, private float $v) {}
    public function interpret(Context $ctx): bool { return $ctx->get($this->f) > $this->v; }
}
class AndExpr implements Expression {
    public function __construct(private Expression $l, private Expression $r) {}
    public function interpret(Context $ctx): bool { return $this->l->interpret($ctx) && $this->r->interpret($ctx); }
}

Added 16 Mar 2026
Edited 12 Jun 2026
Views 49
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 1 ping T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 1 ping T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 10 Google 8 Perplexity 4 Ahrefs 3 SEMrush 3 ChatGPT 2 Unknown AI 2 Claude 2 Bing 2 Scrapy 2 PetalBot 2 Majestic 1 Meta AI 1
crawler 36 crawler_json 6
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: High
⚡ Quick Fix
Use the interpreter pattern for domain-specific rule languages — define a grammar as PHP classes (BooleanExpression, AndExpression, OrExpression) so business users can write filter rules
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
Business rule evaluation via giant switch or nested if/else; user-configurable filter logic evaluated at runtime; domain-specific mini-language needed
Auto-detectable: ✗ No phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: File Tests: Update

✓ schema.org compliant