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

Pure Function

quality Intermediate
debt(d7/e3/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). PHPStan/Psalm don't flag impurity by default; spotting global reads, static state, or DB calls hidden in calculator-shaped functions requires human review.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). Per quick_fix, extracting a pure function means passing inputs as arguments instead of reading globals — a small refactor within the function, occasionally rippling to a few callers.

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

Closest to 'localised tax' (b3). Purity is a per-function discipline applied across web/cli/queue contexts, but each impure function imposes only localised testing/caching friction rather than shaping the whole system.

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

Closest to 'notable trap' (t5). The misconception (that purity doesn't apply to PHP) plus the caching-impure-results mistake is a documented gotcha most devs eventually learn, but the 'obvious' approach isn't always wrong.

About DEBT scoring →

Also Known As

pure functions referential transparency side-effect-free function

TL;DR

A function whose output depends only on its inputs and that produces no side effects — maximally testable and predictable.

Explanation

A pure function has two properties: referential transparency (same inputs always produce the same output) and no side effects (it doesn't modify external state, perform I/O, or depend on mutable global state). Pure functions are the easiest code to test (no mocking required), reason about, and safely parallelise. In PHP, extracting business logic into pure static functions or methods that accept all dependencies as parameters maximises testability even without a functional programming framework.

Common Misconception

Pure functions are a functional programming concept that does not apply to PHP. Pure functions — same input always produces same output, no side effects — are equally valuable in PHP. They are trivially unit-testable, safe to cache, and safe to parallelise regardless of language.

Why It Matters

Pure functions always return the same output for the same input and have no side effects — they are trivially testable, cacheable, and composable without unexpected interactions.

Common Mistakes

  • Functions that read global state, static properties, or the database — impure despite looking like calculators.
  • Functions that modify their arguments — callers do not expect mutation from a getter-style function.
  • Not distinguishing pure utility functions from impure service methods — architecture clarity suffers.
  • Caching impure function results — stale cache returns wrong values when underlying state changes.

Code Examples

✗ Vulnerable
// Impure: depends on external state, has a side effect
function applyDiscount(float $price): float {
    $rate = Config::get('discount_rate'); // hidden dependency
    Log::info('Discount applied');        // side effect
    return $price * (1 - $rate);
}
✓ Fixed
// Pure: output determined solely by inputs, no side effects
function applyDiscount(float $price, float $rate): float {
    return $price * (1 - $rate);
}
// Caller injects the rate; logging done at call site if needed

Added 15 Mar 2026
Edited 22 Mar 2026
Views 28
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings 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 0 pings W 2 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Perplexity 8 Amazonbot 7 SEMrush 3 Unknown AI 2 Google 2 Bing 1
crawler 22 crawler_json 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Extract functions that compute results from inputs without touching global state, databases, or external services — these are pure functions that are trivially testable
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Function reading global variable, writing to property, or calling external service mixed with computation logic
Auto-detectable: ✗ No phpstan psalm
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update

✓ schema.org compliant