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

Missing Return Type Declarations

Code Quality 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' (d5) — PHPStan/Psalm/Rector flag missing return types at appropriate levels; the PHP engine itself won't error on a missing declaration.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3) — quick_fix says add return types method by method; each is a small annotation but multiple methods across files need touching, with possible cascading nullable/void adjustments.

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

Closest to 'localised tax' (b3) — applies broadly across web/cli/queue contexts but each missing type is a localised quality issue rather than a system-shaping choice; cumulatively slows analysis confidence.

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

Closest to 'notable trap' (t5) — the misconception that @return PHPDoc equals a native return type is a well-known gotcha; devs eventually learn PHPDoc isn't engine-enforced.

About DEBT scoring →

Also Known As

return type hint return type missing return type

TL;DR

Functions without declared return types lose static analysis coverage, allow type confusion bugs, and make code harder to understand without reading the implementation.

Explanation

PHP 7.0+ supports return type declarations. PHP 8.0+ adds union types and mixed. PHP 8.1 adds never (functions that always throw or exit). PHP 8.2 adds true/false as standalone types. Declaring return types enables PHPStan and Psalm to catch type mismatches, enables IDE autocompletion, serves as executable documentation, and allows the PHP engine to optimise. In PHP 8.0+ declare strict_types=1 and always annotate return types — there is no valid reason to omit them.

Common Misconception

PHPDoc @return is equivalent to a native return type — PHPDoc comments are not enforced at runtime and are not analysed by the PHP engine; native type declarations are.

Why It Matters

A function with no declared return type that sometimes returns null and sometimes returns an array silently breaks callers who expect an array — a declared return type makes this a detectable error.

Common Mistakes

  • Using @return PHPDoc instead of a native return type — PHPDoc is advisory only.
  • Omitting return types on private methods — they benefit just as much from type declarations.
  • Returning null from a non-nullable return type — add ?Type to acknowledge the nullable case.
  • Not using void for functions that return nothing — void is an explicit contract.

Code Examples

✗ Vulnerable
// No return types — type errors caught at runtime, not analysis time:
function getUser($id) {
    return $this->db->find($id); // What does this return? array? object? null?
}

function saveUser($data) {
    $this->db->insert($data);
    // Returns null implicitly — was that intentional?
}
✓ Fixed
// Explicit return types — analysable, self-documenting:
function getUser(int $id): ?User {
    return $this->db->find($id); // Nullable User — clear contract
}

function saveUser(array $data): void {
    $this->db->insert($data); // void = no return value — intentional
}

function findOrFail(int $id): User {
    return $this->db->findOrFail($id); // Non-nullable — throws if not found
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 81
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 0 pings M 0 pings 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 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 2 pings S 0 pings S 0 pings M
No pings yet today
No pings yesterday
Amazonbot 9 Ahrefs 7 SEMrush 7 PetalBot 7 Google 4 Scrapy 4 Perplexity 3 Applebot 3 Unknown AI 2 Twitter/X 2 Meta AI 1 Bing 1 Brave Search 1
crawler 49 crawler_json 2
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ Quick Fix
Add return types to all methods — start with void for methods that don't return, then add specific types; PHPStan will tell you when you've got the type wrong and catch bugs before runtime
📦 Applies To
PHP 7.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Method without return type declaration; mixed or no type where specific type is clear; missing void on methods with no return
Auto-detectable: ✓ Yes phpstan psalm rector
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Function


✓ schema.org compliant