Skip to content

Exclude checks in specific namespaces #481

@AlessandroMinoccheri

Description

@AlessandroMinoccheri

Feature Request

Add the possibility to exclude namespaces from expressions

Q A
New Feature yes
RFC yes
BC Break it depends

Summary

After further discussion in this issue: #480, it became clear that there's currently no standard or intuitive way to exclude specific namespaces in expressions like NotDependsOnTheseNamespaces.

This expression uses a variadic parameter in its constructor, which makes it difficult to introduce additional arguments—such as an "exclude" array to define namespaces that should be ignored during the check.

We currently see two potential paths forward:

  1. Introduce a breaking change: Replace the variadic parameter with a standard array, allowing us to introduce a second parameter (e.g., for excluded namespaces). To maintain consistency, this change would need to be applied across all similar expressions, which could break existing implementations.
  2. Introduce a new rule, such as ExcludeTheseNamespacesFromChecks, which would accept an array (or variadic arguments) of namespaces to be excluded. This would avoid breaking changes and keep existing rules intact.

Example:

$rules[] = Rule::allClasses()
    ->that(new ResideInOneOfTheseNamespaces('')) // yes, some classes are not namespaced
    ->andThat(new NotResideInTheseNamespaces('App\Shop'))
    ->should(new NotDependsOnTheseNamespaces('App\Shop'))
    ->exclude(new ExcludeTheseNamespacesFromChecks('App\Shop\ShopFacade'))
    ->because('Other modules should only access Shop through ShopFacade');

Or

$rules[] = Rule::allClasses()
    ->that(new ResideInOneOfTheseNamespaces('')) // yes, some classes are not namespaced
    ->andThat(new NotResideInTheseNamespaces('App\Shop'))
    ->should(new NotDependsOnTheseNamespaces('App\Shop'))
    ->excludeTheseNamespacesFromChecks('App\Shop\ShopFacade')
    ->because('Other modules should only access Shop through ShopFacade');

What do you think @fain182 @micheleorselli @pfazzi ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions