Download the PHP package jiripudil/phpstan-sealed-classes without Composer
On this page you can find all versions of the php package jiripudil/phpstan-sealed-classes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jiripudil/phpstan-sealed-classes
More information about jiripudil/phpstan-sealed-classes
Files in jiripudil/phpstan-sealed-classes
Package phpstan-sealed-classes
Short Description Sealed classes support for PHPStan.
License MIT
Informations about the package phpstan-sealed-classes
Sealed classes with PHPStan
This extension adds support for sealed classes and interfaces to PHPStan.
Installation
To use this extension, require it via Composer
If you are using phpstan/extension-installer
, this extension's configuration will be automatically enabled.
Otherwise, you need to include it explicitly in your phpstan.neon
:
Usage
Sealed classes and interfaces allow developers to restrict class hierarchies: a sealed class can only be subclassed by classes that are explicitly permitted to do so. The same applies to sealed interfaces and their implementations. In a way, sealed classes are similar to enumerations, with an important distinction: while enums are singletons, a subclass of a sealed class can have multiple instances, each with its own state.
You can seal a class or an interface by attributing it as #[Sealed]
. The attribute accepts a list of permitted descendants or implementations:
While the first two classes will be allowed, PHPStan will report an error for the third:
Note that the restrictions do not apply to indirect subclasses. If a direct subclass of a sealed class is not sealed itself, it can be further extended without raising any errors. This code is perfectly fine: