66
77use PHP_CodeSniffer \Files \File ;
88use PHP_CodeSniffer \Sniffs \AbstractVariableSniff ;
9- use PHP_CodeSniffer \Util \Tokens ;
109
1110use function assert ;
1211use function ltrim ;
1312use function preg_match ;
1413use function preg_match_all ;
1514use function sprintf ;
16- use function strpos ;
17- use function substr ;
18- use function ucfirst ;
1915
2016use const T_DOUBLE_COLON ;
2117use const T_NULLSAFE_OBJECT_OPERATOR ;
@@ -63,17 +59,9 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
6359 if ($ tokens [$ bracket ]['code ' ] !== T_OPEN_PARENTHESIS ) {
6460 $ objVarName = $ tokens [$ var ]['content ' ];
6561
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-
7462 if (! $ this ->matchesRegex ($ objVarName )) {
7563 $ error = 'Member variable "%s" is not in valid camel caps format ' ;
76- $ data = [$ originalVarName ];
64+ $ data = [$ objVarName ];
7765 $ phpcsFile ->addError ($ error , $ var , 'MemberNotCamelCaps ' , $ data );
7866 }
7967 }
@@ -82,14 +70,7 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
8270
8371 $ objOperator = $ phpcsFile ->findPrevious (T_WHITESPACE , $ stackPtr - 1 , null , true );
8472 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 )) {
9374 $ error = 'Member variable "%s" is not in valid camel caps format ' ;
9475 $ data = [$ tokens [$ stackPtr ]['content ' ]];
9576 $ phpcsFile ->addError ($ error , $ stackPtr , 'MemberNotCamelCaps ' , $ data );
@@ -98,23 +79,12 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
9879 return ;
9980 }
10081
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-
11282 if ($ this ->matchesRegex ($ varName )) {
11383 return ;
11484 }
11585
11686 $ error = 'Variable "%s" is not in valid camel caps format ' ;
117- $ data = [$ originalVarName ];
87+ $ data = [$ varName ];
11888 $ phpcsFile ->addError ($ error , $ stackPtr , 'NotCamelCaps ' , $ data );
11989 }
12090
@@ -141,21 +111,8 @@ protected function processMemberVar(File $phpcsFile, $stackPtr): void
141111 return ;
142112 }
143113
144- $ public = ($ memberProps ['scope ' ] !== 'private ' );
145114 $ errorData = [$ varName ];
146115
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-
159116 if ($ this ->matchesRegex ($ varName )) {
160117 return ;
161118 }
0 commit comments