gosuperscript/monads
gosuperscript/monads is a lightweight functional programming library for PHP and Laravel, offering Option, Result, and related monads to handle nullable values and errors safely through clean, chainable APIs.
Option/Maybe and Result/Either types versus idiomatic PHP null + exception flows. This package is a candidate foundation for railway-oriented programming in service and domain layers. The new PHPStan extension (v1.3.0) meaningfully de-risks this decision by making "panicking" unwraps (e.g. unwrap() that throws) a static-analysis violation rather than a code-review convention.Option/Result at the boundaries (repositories, external API clients, jobs) while keeping Eloquent and controllers idiomatic. Static enforcement lets you define a boundary and mechanically keep the wrong idioms out of it.Result monad becomes the shared vocabulary. The PHPStan rule turns that standard from a style guide into a CI gate — a stronger input to a platform/standards decision.->map()->getOrElse()), and retry/Either-based job orchestration — each a bounded spike before broader adoption. Add "enforce the boundary with the PHPStan rule" to each spike's acceptance criteria.Consider it when:
Result chains beat exception pyramids.Look elsewhere (or write ~200 lines in-house) when:
phpoption/phpoption (the de-facto Option type used by Symfony/Doctrine ecosystems) is the safer "buy" for null-safety alone.To executives: This is a low-cost, low-commitment experiment, not a platform decision. A small monads library could reduce a class of null-handling and error-propagation bugs in our most complex flows. The latest release adds a static-analysis rule that automatically blocks the riskiest misuse pattern, which lowers the execution risk of the trial. The package itself is still early-stage (minimal community, no downstream adopters). Recommendation: fund a 2–3 day spike in one bounded area, measure defect reduction and engineer feedback, and only then make a build/buy/standardize call.
To engineering leadership: The real value is vocabulary and discipline, not the library. Monads give us explicit nullability (Option) and typed, composable failures (Result/Either) — a strong fit for external-API clients, payment orchestration, and validation pipelines. The v1.3.0 PHPStan extension directly addresses our top adoption risk (developers reaching for panicking unwraps under deadline pressure) by moving enforcement from PR review into CI. Remaining risks: team unfamiliarity, abstraction overhead, and the fact that a six-star package must be treated as auditable source we own, not a trusted dependency. Mitigation: vendor or fork it, require the PHPStan rule wherever the library is used, compare against phpoption/phpoption for the Option piece, and establish a one-page convention doc before wider rollout.
To engineering: Treat this as a patterns decision with a library attached. Prototype in one service boundary, keep Laravel-idiomatic code at the edges (controllers/Jobs), and enforce usage via the PHPStan extension plus PR review. Note the extension is opt-in — wiring it up is part of the spike, not an automatic benefit of upgrading. If it earns its keep, we either maintain the fork or graduate to a more established primitive.
How can I help you explore Laravel packages today?