Giter Site home page Giter Site logo

zanbaldwin / phpstan-class-flags Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 12 KB

A PHPStan extension to enforce explicitly stating when a class should not have readonly or final keywords.

License: MIT License

PHP 100.00%
phpstan phpstan-extension

phpstan-class-flags's Introduction

PHPStan: Enforce Class Flags

This library is a PHPStan plugin that disallows classes with ambiguous class flags (the final and readonly keywords) in PHP v8.2+

  • Is Stateful rule: every class analyzed by PHPStan must either be readonly or have a #[HasState] attribute to explicitly declare its statefulness.
  • Is Extendable rule: every class analyzed by PHPStan must either be final or have an #[Extendable] attribute to explicitly declare its extensibility.

You can use the \HasState and \Extendable classes for convenience, or use the WeDevelop\PHPStan\ClassFlags\Flag\HasState and WeDevelop\PHPStan\ClassFlags\Flag\Extendable attribute classes if you prefer to reference a valid class that exists.

Installation

composer require --dev wedevelop/phpstan-class-flags

Configuration

In your phpstan.neon (or phpstan.dist.neon) configuration, add following section:

includes:
    - 'vendor/wedevelop/phpstan-class-flags/rules.neon'

To exclude certain classes from having the rules applied, configure the classFlagsIgnore parameter. You may specify concrete implementations, or you may ignore a range of classes that implement/extend another FQCN.

parameters:
    classFlagsIgnore:
        stateful:
            concrete:
                - 'App\MySpecificImplementation\SomeConcreteClass'
            instanceOf:
                # You cannot make a class readonly if it extends from a
                # non-readonly class.
                - 'Vendor\Legacy\ConfigBuilderInterface'
        # This option is particularly useful for classes you know will have
        # proxies automatically generated.
        extendable:
            instanceOf:
                # App still uses annotations instead of attributes, and not
                # automatically detected as Doctrine entities.
                - 'App\Entity\PersistableEntityInterface'

Classes that are Doctrine entities/documents, as defined via the attributes #[ORM\Entity] or #[ODM\Document], are automatically ignored from the Is Extendable rule.

phpstan-class-flags's People

Contributors

zanbaldwin avatar

Watchers

 avatar

phpstan-class-flags's Issues

Ignore Namespaces

Add ability to ignore entire namespaces (or perhaps instanceof interfaces, etc). Useful for ignoring things like Doctrine entities.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.