Skip to content

Add exception design rules to ensure clean encapsulation #64

@TravisCarden

Description

@TravisCarden

Hi. Thank you so much for this package; I love it! 😍

I have a couple of custom PHPStan rules I use to help ensure clean encapsulation for my Composer library (php-tuf/composer-stager), and I'm interested in contributing them because I think they could be valuable to others, too. They're as polished as you would expect from something ad-hoc and project-specific, but I would be happy to generalize them and figure out how to write tests for them if you'd be interested in having them. Here they are. Please let me know if you would be open to pull requests.

  • Classes\MissingExceptionInterfaceRule requires all custom exceptions to implement a common project-specific interface so that projects using my library can always fall back to catching it, knowing that it will capture any of my exceptions.
  • Methods\ForbiddenThrowsRule ensures that public methods (i.e., those that client code could call) never throw generic or third party exceptions directly. This is the counterpart to the previous rule--it ensures that no third-party exceptions will leak through.

These rules, paired with those already in your package ensuring that all exceptions get wrapped/re-thrown, for example, can ensure nice, clean encapsulation for a library.

Note: Previously proposed (and declined) in phpstan/phpstan#8434

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions