Skip to content

Disallow partial uses in ReferenceUsedNamesOnly#431

Merged
LordSimal merged 1 commit into6.xfrom
fix/partial-uses-430
Apr 11, 2026
Merged

Disallow partial uses in ReferenceUsedNamesOnly#431
LordSimal merged 1 commit into6.xfrom
fix/partial-uses-430

Conversation

@dereuromark
Copy link
Copy Markdown
Member

@dereuromark dereuromark commented Apr 11, 2026

Fixes #430.

Partial namespace references like Mockery\MockInterface (as opposed to the FQCN form \Mockery\MockInterface) were not flagged by SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly because Slevomat's allowPartialUses property defaults to true.

Setting it to false makes the sniff flag such references so they must be imported via a use statement.

Before

namespace App\Test;

class FooTest {
    public function bar(Mockery\MockInterface $x): void {}  // not flagged
}

After

ERROR | Partial use statements are not allowed, but referencing Mockery\MockInterface found.

The FQCN form (\Mockery\MockInterface) was and remains flagged by the same sniff.

Note: this is a behavior-tightening change. Downstream code that intentionally uses partial imports would be affected - but I guess thats fair enough as they could change it back themselves.

Partial namespace references like Mockery\MockInterface were not being
flagged by ReferenceUsedNamesOnly because Slevomat's allowPartialUses
defaults to true. Set it to false so such references are required to be
imported via a use statement.
@LordSimal LordSimal merged commit c8f3c35 into 6.x Apr 11, 2026
6 checks passed
@LordSimal LordSimal deleted the fix/partial-uses-430 branch April 11, 2026 10:32
@ADmad
Copy link
Copy Markdown
Member

ADmad commented Apr 11, 2026

@dereuromark
Copy link
Copy Markdown
Member Author

dereuromark commented Apr 11, 2026

This goes into 6.x right now, we can backport it, but thats indeed a bit breaking.
Maybe for 5.next and a new minor?

@LordSimal
Copy link
Copy Markdown
Contributor

technically nothing can break, its just a code style change. I'd say this can stay in 6.x

@dereuromark
Copy link
Copy Markdown
Member Author

cakephp/cakephp#19391

@ADmad
Copy link
Copy Markdown
Member

ADmad commented Apr 11, 2026

My point was this rule is a bit too pedantic for my liking.

@ADmad
Copy link
Copy Markdown
Member

ADmad commented Apr 11, 2026

If the latest PER standard allows such partial usages then I would be in favor of keeping it.

@dereuromark
Copy link
Copy Markdown
Member Author

dereuromark commented Apr 11, 2026

Partials make the review much harder for humans IMO, as you need to scope in the context then always (you cannot directly see if this is root level or nested namespace).
For machines there is no difference of course.
But handling this manually is a bit more cumbersome.

Disallow partial uses

Pros

  • One canonical form: every external class is in the use block. Easy to scan dependencies.
  • Refactors and static analysis are more reliable.
  • Enforces aliasing for real collisions, which is explicit rather than implicit.
  • Matches the spirit of PSR-12 / most modern PHP style guides.

Cons

  • Forces use ... as Alias for name clashes, which some find uglier than a short partial path.
  • Loses the "namespace-as-context" readability in domain-heavy code.
  • Migration cost when tightening the rule on an existing codebase.

@LordSimal
Copy link
Copy Markdown
Contributor

I don't mind either way, i just thought its obvious to keep it more consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants