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

Return Type Declarations (PHP 7.0)

PHP PHP 7.0+ Beginner
debt(d5/e3/b3/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list phpstan, psalm, and rector as tools — all specialist static analysis tools, not default linters. Missing return types or wrong return types are caught by running PHPStan at level 6+, not by standard PHP syntax errors or a basic linter.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix states 'Add return type to every function. Use ?Type for nullable, void for no return, never for always-throws. Run PHPStan level 6+ to find missing return types.' This is a repetitive but straightforward annotation pattern — not a one-line patch (there are many functions) but also not a multi-file architectural change. Rector can automate much of this, keeping it at e3.

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

Closest to 'localised tax' (b3). The choice applies to web, cli, and queue-worker contexts (broad), but the burden is a per-function annotation tax rather than a cross-cutting architectural constraint. Once return types are added, they live locally on each function and don't reshape the rest of the codebase. Future maintainers pay a small ongoing cost to keep return types correct, but it doesn't create strong gravitational pull.

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

Closest to 'notable trap' (t5). The misconception field explicitly states: 'Return types are only checked by static analysis — PHP enforces return types at runtime too, throwing TypeError for violations.' This is a well-documented gotcha — developers coming from optional-typing mindsets assume return types are purely advisory, but PHP 7.0+ enforces them at runtime. This is a notable, documented surprise but not a catastrophic contradiction with a similar concept elsewhere.

About DEBT scoring →

TL;DR

PHP 7.0 return type declarations (:int, :string, :void, :static, :never) enable functions to declare what they return — making return contract violations detectable at runtime and by static analysis.

Explanation

Return types declared with colon: function getName(): string. PHP 7.1 added: ?Type (nullable), void (must not return a value or return;). PHP 8.0 added: static (returns same class). PHP 8.1 added: never (function always throws or exits). PHP 8.0 also: mixed (any type — avoid), union types (int|string). Return types are enforced at runtime — returning wrong type throws TypeError. Key discipline: always declare return types. void functions may not return a value. never is for functions that always throw: function fail(): never { throw new Exception(); }.

Common Misconception

Return types are only checked by static analysis — PHP enforces return types at runtime too, throwing TypeError for violations.

Why It Matters

Return type declarations create a complete contract for functions — parameters in, return type out — enabling comprehensive static analysis and runtime safety.

Common Mistakes

  • Not adding return types — PHPStan can't verify without them.
  • Returning null from a non-nullable return type — add ?Type or handle it.
  • Not using void for functions that don't return — allows accidental return values.

Code Examples

✗ Vulnerable
function getUser(int $id) { // No return type
    return $this->db->find($id) ?? false; // Inconsistent return
}
✓ Fixed
function getUser(int $id): ?User {
    return $this->db->find($id); // Null if not found
}

function deleteUser(int $id): void {
    $this->db->delete($id);
    // Cannot return a value
}

function abort(string $msg): never {
    throw new HttpException($msg);
}

Added 23 Mar 2026
Views 94
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M
No pings yet today
No pings yesterday
Amazonbot 12 PetalBot 11 Ahrefs 7 ChatGPT 6 Perplexity 5 Scrapy 5 Unknown AI 3 Google 3 Bing 2 Brave Search 2 Applebot 2 Meta AI 1 Twitter/X 1 Sogou 1
crawler 55 crawler_json 5 pre-tracking 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Add return type to every function. Use ?Type for nullable, void for no return, never for always-throws. Run PHPStan level 6+ to find missing return types.
📦 Applies To
PHP 7.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
function [a-z]+\([^)]*\)\s*[^:]
Auto-detectable: ✓ Yes phpstan psalm rector
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Medium ✓ Auto-fixable Fix: Medium Context: Function Tests: Update


✓ schema.org compliant