Skip to content

6.x: Add stricter naming convention sniffs - #424

Draft
dereuromark wants to merge 7 commits into
5.xfrom
6.x
Draft

6.x: Add stricter naming convention sniffs#424
dereuromark wants to merge 7 commits into
5.xfrom
6.x

Conversation

@dereuromark

Copy link
Copy Markdown
Member

Summary

This PR introduces stricter naming convention enforcement for CakePHP 6.x, aligned with PSR naming conventions.

New Sniffs

ValidPropertyNameSniff (new)

  • Flags underscore-prefixed properties ($_propertyName)
  • Applies to all visibility levels (public, protected, private)

ValidFunctionNameSniff (updated)

  • Now flags underscore-prefixed protected/private methods
  • Exception: Entity accessor/mutator patterns are still allowed (_getFieldName(), _setFieldName())
  • Added T_ENUM support
  • Fixed $_magicMethods property naming (was using underscore prefix itself)

Ruleset Changes

  • Removed PSR2.Classes.PropertyDeclaration.Underscore exclusion - PSR2 property underscore checks now apply
  • Added new Slevomat sniffs for modern PHP:
    • RequireNullCoalesceEqualOperator - Require ??= operator
    • RequireNullSafeObjectOperator - Require ?-> operator
    • RequireSelfReference - Require self:: instead of class name
    • NullTypeHintOnLastPosition - Enforce string|null instead of null|string

Migration

Projects upgrading to CakePHP 6.x should:

  1. Rename underscore-prefixed properties: $_propertyName$propertyName
  2. Rename underscore-prefixed methods (except Entity accessors): _methodName()methodName()

The cakephp/upgrade tool has Rector rules to automate these renames.

Tests

All existing tests pass, new test cases added for:

  • Property underscore detection (class and trait)
  • Method underscore detection with Entity accessor exception

- Update ValidFunctionNameSniff to flag underscore-prefixed protected/private
methods (except Entity accessor/mutator pattern _get*, _set*)
- Add ValidPropertyNameSniff to flag underscore-prefixed properties
- Remove PSR2.Classes.PropertyDeclaration.Underscore exclusion from ruleset
- Add new Slevomat sniffs:
- RequireNullCoalesceEqualOperator
- RequireNullSafeObjectOperator
- RequireSelfReference
- NullTypeHintOnLastPosition
- Fix $_magicMethods property naming in sniff itself
- Add T_ENUM support to ValidFunctionNameSniff
Comment threadCakePHP/Sniffs/NamingConventions/ValidPropertyNameSniff.php Outdated
@dereuromark

Copy link
Copy Markdown
MemberAuthor

You're right - removed the custom ValidPropertyNameSniff. The PSR2 built-in handles properties.

Kept the custom ValidFunctionNameSniff though, since it allows the Entity accessor/mutator pattern (_getName, _setField) which the PSR2 method sniff would flag.

@dereuromark

dereuromark commented Mar 14, 2026

Copy link
Copy Markdown
MemberAuthor

The base is off, should target 6.x right?
So this would not be merged. Only close PR as done.

LordSimaland others added 3 commits March 14, 2026 11:43
Relax the ValidEnumNameSniff to allow omitting the "Enum" suffix when
the enum is in a namespace containing "Enum" as a segment (e.g.,
`App\Model\Enum\Status` is now valid without the suffix).
This avoids redundant naming like `App\Model\Enum\FooBarEnum`.
Refs #425
Fix conflict between TypeHintSniff and Slevomat null position sniffs
@dereuromark
dereuromark marked this pull request as ready for review April 2, 2026 13:08
@dereuromark
dereuromark marked this pull request as draft April 2, 2026 13:08
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.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@dereuromark@ADmad@LordSimal