File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed
Inpsyde/Sniffs/CodeQuality Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change 1818use Inpsyde \PhpcsHelpers ;
1919use PHP_CodeSniffer \Files \File ;
2020use PHP_CodeSniffer \Sniffs \Sniff ;
21+ use PHP_CodeSniffer \Util \Tokens ;
2122
2223/**
2324 * @package php-coding-standards
@@ -34,6 +35,8 @@ final class NoAccessorsSniff implements Sniff
3435
3536 public $ skipForFunctions = true ;
3637
38+ public $ skipForNonPublic = true ;
39+
3740 /**
3841 * @return int[]
3942 */
@@ -54,11 +57,28 @@ public function process(File $file, $position)
5457 }
5558
5659 $ functionName = $ file ->getDeclarationName ($ position );
57-
5860 if (!$ functionName || in_array ($ functionName , self ::ALLOWED_NAMES , true )) {
5961 return ;
6062 }
6163
64+ if ($ this ->skipForNonPublic && PhpcsHelpers::functionIsMethod ($ file , $ position )) {
65+ $ modifierPointerPosition = $ file ->findPrevious (
66+ [T_WHITESPACE , T_ABSTRACT ],
67+ $ position - 1 ,
68+ null ,
69+ true ,
70+ null ,
71+ true
72+ );
73+ $ modifierPointer = $ file ->getTokens ()[$ modifierPointerPosition ] ?? null ;
74+ if (!in_array ($ modifierPointer ['code ' ], Tokens::$ scopeModifiers , true )) {
75+ $ modifierPointer = null ;
76+ }
77+ if ($ modifierPointer && $ modifierPointer ['code ' ] !== T_PUBLIC ) {
78+ return ;
79+ }
80+ }
81+
6282 preg_match ('/^(set|get)[_A-Z0-9]+/ ' , $ functionName , $ matches );
6383 if (!$ matches ) {
6484 return ;
Original file line number Diff line number Diff line change @@ -40,8 +40,7 @@ function withThing() {
4040
4141 }
4242
43- // @phpcsWarningCodeOnNextLine NoSetter
44- function setTheThing ($ foo , $ bar ) {
43+ private function setTheThing ($ foo , $ bar ) {
4544
4645 }
4746
You can’t perform that action at this time.
0 commit comments