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

PHPDoc / Docblock

style PHP 5.0+ Beginner

Also Known As

PHPDoc block PHP docblock doc comment

TL;DR

Structured comment blocks that document functions, classes, and properties — read by IDEs, static analysis tools, and documentation generators.

Explanation

PHPDoc docblocks (/** ... */) use tags like @param, @return, @throws, @var, @property, @template, and @deprecated to annotate code. IDEs (PhpStorm, VS Code with Intelephense) use them for autocomplete and type inference; tools like PHPStan and Psalm use them for advanced static analysis; phpDocumentor generates API documentation from them. With modern PHP's native type declarations, many @param and @return tags are redundant — use them to add context, describe array shapes, or document generic types not expressible in PHP's type system.

Common Misconception

PHPDoc blocks are redundant once PHP has native type declarations. Docblocks add information native types cannot express — @throws for exception contracts, @template for generics, complex union types in PHP 7, and @var for inline variable annotation used by IDEs and static analysers.

Why It Matters

PHPDoc docblocks provide machine-readable type information and documentation — IDEs use them for autocompletion, static analysers use them for type inference, and developers use them for understanding intent.

Common Mistakes

  • Docblocks that restate the type declarations already visible in the signature — zero added value.
  • Missing @throws tags — callers cannot know which exceptions to handle.
  • @param with no description for non-obvious parameters — the type is already in the declaration.
  • Outdated docblocks that contradict the current code — worse than no docblock.

Code Examples

💡 Note
Only document what types can't express: thrown exceptions, pre/post-conditions, non-obvious behaviour. Don't restate the signature.
✗ Vulnerable
/**
 * Get user
 * @param int $id The id
 * @return User The user
 */
public function getUser(int $id): User // Docblock adds nothing — all visible in signature

// Valuable docblock:
/**
 * Finds user by ID. Throws if not found because callers must handle absence explicitly.
 * @throws UserNotFoundException if no user with this ID exists
 * @throws DatabaseException on connection failure
 */
✓ Fixed
/**
 * Transfer funds between two accounts atomically.
 *
 * @param  Account $from      Source account — must have sufficient balance
 * @param  Account $to        Destination account
 * @param  Money   $amount    Amount to transfer — must be positive
 * @return Transfer           The completed transfer record
 * @throws InsufficientFundsException  If $from balance < $amount
 * @throws CurrencyMismatchException   If accounts use different currencies
 */
public function transfer(Account $from, Account $to, Money $amount): Transfer

// Skip docblocks when types + names are already self-documenting:
public function findById(int $id): ?User {} // no docblock needed

Added 15 Mar 2026
Edited 22 Mar 2026
Views 32
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings 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 2 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 1 ping W 1 ping T
Perplexity 9 Amazonbot 7 Unknown AI 3 Ahrefs 3 SEMrush 2 Majestic 1 Google 1
crawler 25 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use PHPDoc @param, @return, @throws for complex types that PHP's native type system cannot express — for simple scalar types, prefer native type declarations over redundant docblocks
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
@param string $name on method with string $name native type — redundant; missing @template for generic types; @return array when array{id: int, name: string} would be precise
Auto-detectable: ✓ Yes phpstan psalm phpcs phpdoc
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Low Context: Function

✓ schema.org compliant