-
Notifications
You must be signed in to change notification settings - Fork 84
6.x: Add stricter naming convention sniffs #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dereuromark
wants to merge
7
commits into
5.x
Choose a base branch
from
6.x
base: 5.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be92b75
Add stricter naming convention sniffs for CakePHP 6.x
dereuromark 5a99825
Remove custom ValidPropertyNameSniff, use PSR2 built-in instead
dereuromark 6446baa
Fix coding standard - remove double spaces
dereuromark 9cf1618
add custom enum sniff (#425)
LordSimal 1950643
Allow enum suffix omission when in Enum namespace (#428)
dereuromark a747422
Merge pull request #427 from cakephp/fix-void-null-type-order-conflict
dereuromark c8f3c35
Disallow partial uses in ReferenceUsedNamesOnly (#431)
dereuromark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
CakePHP/Sniffs/NamingConventions/ValidPropertyNameSniff.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
| /** | ||
| * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | ||
| * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
| * | ||
| * Licensed under The MIT License | ||
| * For full copyright and license information, please see the LICENSE.txt | ||
| * Redistributions of files must retain the above copyright notice. | ||
| * | ||
| * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
| * @link https://github.com/cakephp/cakephp-codesniffer | ||
| * @since CakePHP CodeSniffer 6.0.0 | ||
| * @license https://www.opensource.org/licenses/mit-license.php MIT License | ||
| */ | ||
|
|
||
| /** | ||
| * Ensures property names follow PSR naming conventions. | ||
| * | ||
| * Non-public properties should not be prefixed with an underscore. | ||
| */ | ||
| namespace CakePHP\Sniffs\NamingConventions; | ||
|
|
||
| use PHP_CodeSniffer\Files\File; | ||
| use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; | ||
|
|
||
| class ValidPropertyNameSniff extends AbstractVariableSniff | ||
| { | ||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| protected function processMemberVar(File $phpcsFile, $stackPtr) | ||
| { | ||
| $tokens = $phpcsFile->getTokens(); | ||
| $propName = ltrim($tokens[$stackPtr]['content'], '$'); | ||
|
|
||
| // Only check properties starting with underscore | ||
| if ($propName[0] !== '_') { | ||
| return; | ||
| } | ||
|
|
||
| $props = $phpcsFile->getMemberProperties($stackPtr); | ||
|
|
||
| // Public properties with underscore are also bad, but less common | ||
| // Focus on protected/private which was the old convention | ||
| if ($props['scope'] !== 'public') { | ||
| $error = 'Non-public property "$%s" should not be prefixed with underscore'; | ||
| $phpcsFile->addError($error, $stackPtr, 'PropertyWithUnderscore', [$propName]); | ||
| } else { | ||
| $error = 'Public property "$%s" must not be prefixed with underscore'; | ||
| $phpcsFile->addError($error, $stackPtr, 'PublicPropertyWithUnderscore', [$propName]); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| protected function processVariable(File $phpcsFile, $stackPtr) | ||
| { | ||
| // We only care about member variables (properties) | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| protected function processVariableInString(File $phpcsFile, $stackPtr) | ||
| { | ||
| // We only care about member variables (properties) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
CakePHP/Tests/NamingConventions/ValidPropertyNameUnitTest.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| namespace Beakman; | ||
|
|
||
| class PropertyNames | ||
| { | ||
| public string $publicProperty = ''; | ||
|
|
||
| public string $_publicUnderscore = ''; // error | ||
|
|
||
| protected string $protectedProperty = ''; | ||
|
|
||
| protected string $_protectedUnderscore = ''; // error | ||
|
|
||
| private string $privateProperty = ''; | ||
|
|
||
| private string $_privateUnderscore = ''; // error | ||
|
|
||
| public static string $publicStatic = ''; | ||
|
|
||
| protected static string $_protectedStatic = ''; // error | ||
|
|
||
| private static string $_privateStatic = ''; // error | ||
| } | ||
|
|
||
| trait PropertyNamesTrait | ||
| { | ||
| public string $publicProperty = ''; | ||
|
|
||
| public string $_publicUnderscore = ''; // error | ||
|
|
||
| protected string $protectedProperty = ''; | ||
|
|
||
| protected string $_protectedUnderscore = ''; // error | ||
|
|
||
| private string $privateProperty = ''; | ||
|
|
||
| private string $_privateUnderscore = ''; // error | ||
| } |
33 changes: 33 additions & 0 deletions
33
CakePHP/Tests/NamingConventions/ValidPropertyNameUnitTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| namespace CakePHP\Tests\NamingConventions; | ||
|
|
||
| use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase; | ||
|
|
||
| class ValidPropertyNameUnitTest extends AbstractSniffTestCase | ||
| { | ||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getErrorList(): array | ||
| { | ||
| return [ | ||
| 9 => 1, // public $_publicUnderscore | ||
| 13 => 1, // protected $_protectedUnderscore | ||
| 17 => 1, // private $_privateUnderscore | ||
| 21 => 1, // protected static $_protectedStatic | ||
| 23 => 1, // private static $_privateStatic | ||
| 30 => 1, // public $_publicUnderscore (trait) | ||
| 34 => 1, // protected $_protectedUnderscore (trait) | ||
| 38 => 1, // private $_privateUnderscore (trait) | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getWarningList(): array | ||
| { | ||
| return []; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.