@@ -63,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr)
63
63
// Make sure the first letter is a capital.
64
64
if (preg_match ('|^[A-Z]| ' , $ name ) === 0 ) {
65
65
$ error = '%s name must use UpperCamel naming and begin with a capital letter ' ;
66
- $ phpcsFile -> addError ( $ error , $ stackPtr , 'StartWithCapital ' , $ errorData );
66
+ $ this -> processUpperLowerCase ( $ className , $ phpcsFile , 'StartWithCapital ' , $ error , $ errorData );
67
67
}
68
68
69
69
// Search for underscores.
@@ -75,10 +75,44 @@ public function process(File $phpcsFile, $stackPtr)
75
75
// Ensure the name does not contain acronyms.
76
76
if (preg_match ('|[A-Z]{3}| ' , $ name ) === 1 ) {
77
77
$ error = '%s name must use UpperCamel naming and not contain multiple upper case letters in a row ' ;
78
- $ phpcsFile -> addError ( $ error , $ stackPtr , 'NoUpperAcronyms ' , $ errorData );
78
+ $ this -> processUpperLowerCase ( $ className , $ phpcsFile , 'NoUpperAcronyms ' , $ error , $ errorData );
79
79
}
80
80
81
81
}//end process()
82
82
83
83
84
+ protected function processUpperLowerCase (int $ stackPtr , File $ phpcsFile , string $ errorCode , string $ errorMessage , array $ errorData ): void
85
+ {
86
+ $ fix = $ phpcsFile ->addFixableError ($ errorMessage , $ stackPtr , $ errorCode , $ errorData );
87
+ if ($ fix === false ) {
88
+ return ;
89
+ }
90
+
91
+ $ tokens = $ phpcsFile ->getTokens ();
92
+ $ name = ucfirst ($ tokens [$ stackPtr ]['content ' ]);
93
+ $ upperCaseStarted = false ;
94
+ for ($ i = 0 ; $ i < strlen ($ name ); $ i ++) {
95
+ if ($ upperCaseStarted === true
96
+ && ctype_upper ($ name [$ i ]) === true
97
+ && isset ($ name [($ i + 1 )])
98
+ && (ctype_upper ($ name [($ i + 1 )]) === true || $ name [($ i + 1 )] === '_ ' )
99
+ ) {
100
+ $ name [$ i ] = strtolower ($ name [$ i ]);
101
+ } else {
102
+ if (ctype_upper ($ name [$ i ]) === true ) {
103
+ $ upperCaseStarted = true ;
104
+ } else {
105
+ $ upperCaseStarted = false ;
106
+ }
107
+ }
108
+ }
109
+
110
+ $ name [(strlen ($ name ) - 1 )] = strtolower ($ name [(strlen ($ name ) - 1 )]);
111
+ $ phpcsFile ->fixer ->replaceToken ($ stackPtr , $ name );
112
+
113
+ // @todo Can we move files?
114
+
115
+ }//end processUpperLowerCase()
116
+
117
+
84
118
}//end class
0 commit comments