PHPStan extension for sealed classes and interfaces.

Overview

Sealed classes with PHPStan

This extension adds support for sealed classes and interfaces to PHPStan.

Installation

To use this extension, require it via Composer

composer require --dev jiripudil/phpstan-sealed-classes

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:

includes:
    - vendor/jiripudil/phpstan-sealed-classes/extension.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 an abstract class or an interface by attributing it as #[Sealed]. The attribute accepts a list of permitted descendants or implementations:

<?php

use JiriPudil\SealedClasses\Sealed;

#[Sealed(permits: [AllowedImplementation::class, AnotherImplementation::class])]
interface SealedInterface {}

class AllowedImplementation implements SealedInterface {}
class AnotherImplementation implements SealedInterface {}
class DisallowedImplementation implements SealedInterface {}

While the first two classes will be allowed, PHPStan will report an error for the third:

------ ---------------------------------------------------------------------------------------------------
 Line   sealed-interface.php
------ ---------------------------------------------------------------------------------------------------
 10     Class DisallowedImplementation is not allowed to implement a #[Sealed] interface SealedInterface.
------ ---------------------------------------------------------------------------------------------------

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:

<?php

use JiriPudil\SealedClasses\Sealed;

#[Sealed(permits: [AllowedDescendant::class])]
abstract class SealedClass {}

class AllowedDescendant extends SealedClass {}
class IndirectDescendant extends AllowedDescendant {}
You might also like...
Demonstration of OOP concepts and usage of Abstract class & Interfaces

Learn OOP Demonstration of OOP concepts and usage of Abstract class & Interfaces Usage clone this repo run composer install run php index.php Code str

YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic.

YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic. It is built based on the combination of ideas from the Yii framework and Laravel framework (yl).

Bind your interfaces to implementations automatically.
Bind your interfaces to implementations automatically.

Laravel Auto Binder This package automatically binds interfaces to implementations in the Service Container, scanning the specified project folders. T

Provides simple interfaces to implement a webhook-based tweeting system

webhook-tweeter This package aims to provide simple interfaces to implement a webhook-based tweeting system. This can, for example, be used to tweet a

Analyzes PHPStan baseline files and creates aggregated error trend-reports

Analyzes phpstan baseline files Analyzes PHPStan baseline files and creates aggregated error trend-reports.

Magento 2 Extension to cleanup admin menu and Store > Configuration area by arranging third party extension items.
Magento 2 Extension to cleanup admin menu and Store Configuration area by arranging third party extension items.

Clean Admin Menu - Magento 2 Extension It will merge all 3rd party extension's menu items in backend's primary menu to a common menu item named "Exten

Sandbox project for the PHPStan workshop

Sandbox project for a PHPStan workshop Installation Requirements Docker Engine Docker Compose Git Bash Getting started Clone this repository (git clon

A PHPStan package that supports working with Extbase

PHPStan for Extbase This package provides a couple of stubs and services to make your life easier when working with PHPStan and Extbase. Examples clas

Custom PHPStan rules

phpstan-rules Provides additional rules for phpstan/phpstan. Installation Run $ composer require --dev alister/phpstan-rules Usage All of the rules pr

Comments
  • Support sealing concrete classes

    Support sealing concrete classes

    Currently, this extension only supports sealing abstract classes or interfaces. Is there any reason to have that restriction ?

    In the upcoming v2 of scssphp, I have a use case that would require sealing a concrete class. Among our Value classes, our intent is that they should all be either final or sealed (no custom child classes are intended to be supported). But our SassList class is both a concrete class (representing normal Sass lists) and the parent of the SassArgumentList class (representing a special kind of list). Making SassList a sealed class would allow to represent that intent, but it is not supported by your package currently (disclaimer: scssphp does not use your package yet, but our other classes that would be marked as sealed are abstract so this would not be a blocker).

    Supports in other languages:

    • Dart allows sealing concrete classes
    • Hack allows sealing concrete classes
    • Java allows sealing concrete classes
    • the declined PHP RFC about sealed classes was also allowing sealing concrete classes
    • Kotlin makes sealed classes abstract (without requiring an abstract keyword), thus forbidding sealing concrete classes
    opened by stof 2
Owner
Jiří Pudil
👨‍💻 Web developer (PHP, TypeScript, React). 📖 Open sourcerer. ☕ Coffee addict. 🎸 Guitar noisemaker.
Jiří Pudil
The SensioLabs DeprecationDetector runs a static code analysis against your project's source code to find usages of deprecated methods, classes and interfaces

SensioLabs DeprecationDetector CAUTION: This package is abandoned and will no longer receive any updates. The SensioLabs DeprecationDetector runs a st

QOSSMIC GmbH 389 Nov 24, 2022
bin/magento command to display configured preferences for classes or interfaces

bin/magento command to display configured preferences for classes or interfaces A bin/magento command that will show you the configured preferences fo

David Manners 14 Jul 18, 2022
Um repositório com classes, interfaces para padronizar os projetos de PHP da empresa

php-utils PHP Utilities for Laravel/Lumen Installation cd /path/to/your/project composer require logcomex/php-utils Utilities Packages Contracts Excep

LogComex 1 Oct 10, 2022
PHPStan extension to support #[Readonly] constructor properties

icanhazstring/phpstan-readonly-property Support #[Readonly] promoted constructor properties for PHPStan. This library is used to have a full transitio

Andreas Frömer 4 Apr 5, 2022
Magento specific extension for phpstan

bitexpert/phpstan-magento This package provides some additional features for PHPStan to make it work for Magento 2 projects. Installation The preferre

bitExpert AG 92 Dec 7, 2022
The main scope of this extension is to help phpstan to detect the type of object after the Assert\Assertion validation.

PHPStan beberlei/assert extension PHPStan beberlei/assert Description The main scope of this extension is to help phpstan to detect the type of object

PHPStan 33 Jan 2, 2023
PHPStan extension for webmozart/assert

PHPStan webmozart/assert extension PHPStan webmozart/assert Description The main scope of this extension is to help phpstan to detect the type of obje

PHPStan 139 Dec 22, 2022
An extension for PHPStan for adding analysis for PHP Language Extensions.

PHPStan PHP Language Extensions (currently in BETA) This is an extension for PHPStan for adding analysis for PHP Language Extensions. Language feature

Dave Liddament 9 Nov 30, 2022
Extension for PHPStan to allow analysis of Drupal code.

phpstan-drupal Extension for PHPStan to allow analysis of Drupal code. Sponsors Would you like to sponsor? Usage When you are using phpstan/extension-

Matt Glaman 154 Jan 2, 2023
This component provides a collection of functions/classes using the symfony/intl package when the Intl extension is not installed.

Symfony Polyfill / Intl: ICU This package provides fallback implementations when the Intl extension is not installed. It is limited to the "en" locale

Symfony 2.4k Jan 6, 2023