Why `mixed` Is the Worst Type in Your PHP Codebase (and How to Kill It)
The article discusses the challenges posed by the 'mixed' type in PHP codebases, particularly in Laravel applications. It highlights how the introduction of 'mixed' in PHP 8.0 complicates type safety and can lead to runtime errors that are difficult to catch during development. The author provides insights on how to address these issues and improve code quality by avoiding the use of 'mixed'.
- ▪A team had 1,400 mixed return types in their Laravel 11 codebase, which led to unexpected errors.
- ▪The 'mixed' type allows any data type, making it difficult for static analysis tools to provide useful feedback.
- ▪Using 'mixed' can result in IDE autocomplete failures and runtime errors that are not caught during testing.
Opening excerpt (first ~120 words) tap to expand
try { if(localStorage) { let currentUser = localStorage.getItem('current_user'); if (currentUser) { currentUser = JSON.parse(currentUser); if (currentUser.id === 425693) { document.getElementById('article-show-container').classList.add('current-user-is-article-author'); } } } } catch (e) { console.error(e); } Gabriel Anhaia Posted on May 24 Why `mixed` Is the Worst Type in Your PHP Codebase (and How to Kill It) #php #types #phpstan #architecture Book: Decoupled PHP — Clean and Hexagonal Architecture for Applications That Outlive the Framework Also by me: Thinking in Go (2-book series) — Complete Guide to Go Programming + Hexagonal Architecture in Go My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub A team…
Excerpt limited to ~120 words for fair-use compliance. The full article is at DEV.to (Top).