Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phpstan Sealed Classes Laravel Package

jiripudil/phpstan-sealed-classes

PHPStan extension that adds sealed classes support for PHP: declare which classes/interfaces may extend or implement a type, and have PHPStan report violations. Helps enforce architecture boundaries and prevent unauthorized inheritance in large codebases.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require --dev jiripudil/phpstan-sealed-classes. Then enable it in your PHPStan config (phpstan.neon):

includes:
    - vendor/jiripudil/phpstan-sealed-classes/extension.neon

Start by annotating your sealed classes/interfaces with @phpstan-sealed and listing permitted subclasses (e.g., @phpstan-sealed-with class Foo, class Bar). The first use case is preventing accidental subclassing outside the allowed set — ideal for enum-like type hierarchies (e.g., algebraic data types in domain models).

Implementation Patterns

Use @phpstan-sealed-with on sealed interfaces or abstract classes to enforce compile-time subclass constraints. Typical pattern:

/**
 * @phpstan-sealed-with Operation\Add, Operation\Subtract
 */
interface Operation {}

final class Add implements Operation {}
final class Subtract implements Operation {}

Works best with PSR-4 autoloading — ensure permitted classes are autoloadable. Combine with @extends for genericsealed types (e.g., sealed Result<Ok, Error>). Leverage IDE auto-imports: PHPStan will surface only allowed subclasses in @phpstan-sealed-with, reducing cognitive load during maintenance.

Gotchas and Tips

  • Autoloading matters: If a class in @phpstan-sealed-with isn’t autoloadable (e.g., misnamed file), PHPStan may silently skip validation — always run composer dump-autoload after adding classes.
  • Case sensitivity: Class names in @phpstan-sealed-with are case-sensitive and must match the exact FQCN (including namespace), even if PHP allows case-insensitive class resolution.
  • Interfaces only: Sealing interfaces is supported, but sealed abstract classes with final children require each child to be explicitly listed — don’t assume PHP’s inheritance graph is inferred.
  • Extension points: Add custom rules in your config to flag misuse (e.g., non-final children of sealed types). Use phpstan-phpdoc-parser to extend the docblock parser if you need custom annotations.
  • Debug tip: Run phpstan analyse -l max and watch for "Sealed type..." errors — they often reveal missing final keywords or incorrect namespaces.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle