6
6
7
7
use PHP_CodeSniffer \Files \File ;
8
8
use PHP_CodeSniffer \Sniffs \AbstractVariableSniff ;
9
- use PHP_CodeSniffer \Util \Tokens ;
10
9
11
10
use function assert ;
12
11
use function ltrim ;
13
12
use function preg_match ;
14
13
use function preg_match_all ;
15
14
use function sprintf ;
16
- use function strpos ;
17
- use function substr ;
18
- use function ucfirst ;
19
15
20
16
use const T_DOUBLE_COLON ;
21
17
use const T_NULLSAFE_OBJECT_OPERATOR ;
@@ -63,17 +59,9 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
63
59
if ($ tokens [$ bracket ]['code ' ] !== T_OPEN_PARENTHESIS ) {
64
60
$ objVarName = $ tokens [$ var ]['content ' ];
65
61
66
- // There is no way for us to know if the var is public or
67
- // private, so we have to ignore a leading underscore if there is
68
- // one and just check the main part of the variable name.
69
- $ originalVarName = $ objVarName ;
70
- if (strpos ($ objVarName , '_ ' ) === 0 ) {
71
- $ objVarName = substr ($ objVarName , 1 );
72
- }
73
-
74
62
if (! $ this ->matchesRegex ($ objVarName )) {
75
63
$ error = 'Member variable "%s" is not in valid camel caps format ' ;
76
- $ data = [$ originalVarName ];
64
+ $ data = [$ objVarName ];
77
65
$ phpcsFile ->addError ($ error , $ var , 'MemberNotCamelCaps ' , $ data );
78
66
}
79
67
}
@@ -82,14 +70,7 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
82
70
83
71
$ objOperator = $ phpcsFile ->findPrevious (T_WHITESPACE , $ stackPtr - 1 , null , true );
84
72
if ($ tokens [$ objOperator ]['code ' ] === T_DOUBLE_COLON ) {
85
- // The variable lives within a class, and is referenced like
86
- // this: MyClass::$_variable, so we don't know its scope.
87
- $ objVarName = $ varName ;
88
- if (strpos ($ objVarName , '_ ' ) === 0 ) {
89
- $ objVarName = substr ($ objVarName , 1 );
90
- }
91
-
92
- if (! $ this ->matchesRegex ($ objVarName )) {
73
+ if (! $ this ->matchesRegex ($ varName )) {
93
74
$ error = 'Member variable "%s" is not in valid camel caps format ' ;
94
75
$ data = [$ tokens [$ stackPtr ]['content ' ]];
95
76
$ phpcsFile ->addError ($ error , $ stackPtr , 'MemberNotCamelCaps ' , $ data );
@@ -98,23 +79,12 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
98
79
return ;
99
80
}
100
81
101
- // There is no way for us to know if the var is public or private,
102
- // so we have to ignore a leading underscore if there is one and just
103
- // check the main part of the variable name.
104
- $ originalVarName = $ varName ;
105
- if (strpos ($ varName , '_ ' ) === 0 ) {
106
- $ inClass = $ phpcsFile ->hasCondition ($ stackPtr , Tokens::$ ooScopeTokens );
107
- if ($ inClass === true ) {
108
- $ varName = substr ($ varName , 1 );
109
- }
110
- }
111
-
112
82
if ($ this ->matchesRegex ($ varName )) {
113
83
return ;
114
84
}
115
85
116
86
$ error = 'Variable "%s" is not in valid camel caps format ' ;
117
- $ data = [$ originalVarName ];
87
+ $ data = [$ varName ];
118
88
$ phpcsFile ->addError ($ error , $ stackPtr , 'NotCamelCaps ' , $ data );
119
89
}
120
90
@@ -141,21 +111,8 @@ protected function processMemberVar(File $phpcsFile, $stackPtr): void
141
111
return ;
142
112
}
143
113
144
- $ public = ($ memberProps ['scope ' ] !== 'private ' );
145
114
$ errorData = [$ varName ];
146
115
147
- if (($ public === true ) && strpos ($ varName , '_ ' ) === 0 ) {
148
- $ error = '%s member variable "%s" must not contain a leading underscore ' ;
149
- $ data = [
150
- ucfirst ($ memberProps ['scope ' ]),
151
- $ errorData [0 ],
152
- ];
153
- $ phpcsFile ->addError ($ error , $ stackPtr , 'PublicHasUnderscore ' , $ data );
154
- }
155
-
156
- // Remove a potential underscore prefix for testing CamelCaps.
157
- $ varName = ltrim ($ varName , '_ ' );
158
-
159
116
if ($ this ->matchesRegex ($ varName )) {
160
117
return ;
161
118
}
0 commit comments