Skip to content

Proposal for clearer rule definition syntax with canBeUsedBy #491

@pfazzi

Description

@pfazzi

Feature Request

Q A
New Feature yes
RFC yes
BC Break no

Summary

I have a recurring use case in our codebase where we want to prevent all modules from depending on a specific namespace, except a couple of well-known exceptions.

Today, this is expressed like this:

// No other module should depend on this one.
// We only allow two known exceptions: Logging and RequestHandler.
Rule::allClasses()
    ->except(
        'Acme\Quoting\Requests*',
        'Acme\Domain\Logging',
        'Acme\Service\RequestHandler',
    )
    ->that(new ResideInOneOfTheseNamespaces('Acme'))
    ->should(new NotDependsOnTheseNamespaces('Acme\Quoting\Requests'))
    ->because('no other module except Logging and RequestHandler should depend on the Requests module');

I think this rule is harder to read and easy to get wrong, especially as the number of exceptions grows.

I’d like to propose a second, more declarative syntax to make this kind of intent more readable:

Rule::namespace('Acme\Quoting\Requests')
    ->canBeUsedBy(
        'Acme\Domain\Logging',
        'Acme\Service\RequestHandler'
    );

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions