@@ -70,29 +70,20 @@ public function process(File $phpcsFile, $stackPtr)
70
70
71
71
if (isset (Tokens::$ castTokens [$ tokens [$ stackPtr ]['code ' ]]) === true ) {
72
72
// A cast token.
73
- if (strtolower ($ tokens [$ stackPtr ]['content ' ]) !== $ tokens [$ stackPtr ]['content ' ]) {
74
- if ($ tokens [$ stackPtr ]['content ' ] === strtoupper ($ tokens [$ stackPtr ]['content ' ])) {
75
- $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'upper ' );
76
- } else {
77
- $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'mixed ' );
78
- }
79
-
80
- $ error = 'PHP type casts must be lowercase; expected "%s" but found "%s" ' ;
81
- $ data = [
82
- strtolower ($ tokens [$ stackPtr ]['content ' ]),
83
- $ tokens [$ stackPtr ]['content ' ],
84
- ];
85
-
86
- $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'TypeCastFound ' , $ data );
87
- if ($ fix === true ) {
88
- $ phpcsFile ->fixer ->replaceToken ($ stackPtr , strtolower ($ tokens [$ stackPtr ]['content ' ]));
89
- }
90
- } else {
91
- $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'lower ' );
92
- }//end if
73
+ $ this ->processType (
74
+ $ phpcsFile ,
75
+ $ stackPtr ,
76
+ $ tokens [$ stackPtr ]['content ' ],
77
+ 'PHP type casts must be lowercase; expected "%s" but found "%s" ' ,
78
+ 'TypeCastFound '
79
+ );
93
80
94
81
return ;
95
- }//end if
82
+ }
83
+
84
+ /*
85
+ * Check function return type.
86
+ */
96
87
97
88
$ props = $ phpcsFile ->getMethodProperties ($ stackPtr );
98
89
@@ -103,29 +94,15 @@ public function process(File $phpcsFile, $stackPtr)
103
94
if ($ returnType !== ''
104
95
&& isset ($ this ->phpTypes [$ returnTypeLower ]) === true
105
96
) {
106
- // A function return type.
107
- if ($ returnTypeLower !== $ returnType ) {
108
- if ($ returnType === strtoupper ($ returnType )) {
109
- $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'upper ' );
110
- } else {
111
- $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'mixed ' );
112
- }
113
-
114
- $ error = 'PHP return type declarations must be lowercase; expected "%s" but found "%s" ' ;
115
- $ token = $ props ['return_type_token ' ];
116
- $ data = [
117
- $ returnTypeLower ,
118
- $ returnType ,
119
- ];
120
-
121
- $ fix = $ phpcsFile ->addFixableError ($ error , $ token , 'ReturnTypeFound ' , $ data );
122
- if ($ fix === true ) {
123
- $ phpcsFile ->fixer ->replaceToken ($ token , $ returnTypeLower );
124
- }
125
- } else {
126
- $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'lower ' );
127
- }//end if
128
- }//end if
97
+ $ error = 'PHP return type declarations must be lowercase; expected "%s" but found "%s" ' ;
98
+ $ errorCode = 'ReturnTypeFound ' ;
99
+
100
+ $ this ->processType ($ phpcsFile , $ props ['return_type_token ' ], $ returnType , $ error , $ errorCode );
101
+ }
102
+
103
+ /*
104
+ * Check function parameter types.
105
+ */
129
106
130
107
$ params = $ phpcsFile ->getMethodParameters ($ stackPtr );
131
108
if (empty ($ params ) === true ) {
@@ -140,32 +117,53 @@ public function process(File $phpcsFile, $stackPtr)
140
117
if ($ typeHint !== ''
141
118
&& isset ($ this ->phpTypes [$ typeHintLower ]) === true
142
119
) {
143
- // A function return type.
144
- if ($ typeHintLower !== $ typeHint ) {
145
- if ($ typeHint === strtoupper ($ typeHint )) {
146
- $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'upper ' );
147
- } else {
148
- $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'mixed ' );
149
- }
150
-
151
- $ error = 'PHP parameter type declarations must be lowercase; expected "%s" but found "%s" ' ;
152
- $ token = $ param ['type_hint_token ' ];
153
- $ data = [
154
- $ typeHintLower ,
155
- $ typeHint ,
156
- ];
157
-
158
- $ fix = $ phpcsFile ->addFixableError ($ error , $ token , 'ParamTypeFound ' , $ data );
159
- if ($ fix === true ) {
160
- $ phpcsFile ->fixer ->replaceToken ($ token , $ typeHintLower );
161
- }
162
- } else {
163
- $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'lower ' );
164
- }//end if
165
- }//end if
166
- }//end foreach
120
+ $ error = 'PHP parameter type declarations must be lowercase; expected "%s" but found "%s" ' ;
121
+ $ errorCode = 'ParamTypeFound ' ;
122
+
123
+ $ this ->processType ($ phpcsFile , $ param ['type_hint_token ' ], $ typeHint , $ error , $ errorCode );
124
+ }
125
+ }
167
126
168
127
}//end process()
169
128
170
129
130
+ /**
131
+ * Processes a type cast or a singular type declaration.
132
+ *
133
+ * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
134
+ * @param int $stackPtr The position of the type token.
135
+ * @param string $type The type found.
136
+ * @param string $error Error message template.
137
+ * @param string $errorCode The error code.
138
+ *
139
+ * @return void
140
+ */
141
+ protected function processType (File $ phpcsFile , $ stackPtr , $ type , $ error , $ errorCode )
142
+ {
143
+ $ typeLower = strtolower ($ type );
144
+
145
+ if ($ typeLower === $ type ) {
146
+ $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'lower ' );
147
+ return ;
148
+ }
149
+
150
+ if ($ type === strtoupper ($ type )) {
151
+ $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'upper ' );
152
+ } else {
153
+ $ phpcsFile ->recordMetric ($ stackPtr , 'PHP type case ' , 'mixed ' );
154
+ }
155
+
156
+ $ data = [
157
+ $ typeLower ,
158
+ $ type ,
159
+ ];
160
+
161
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , $ errorCode , $ data );
162
+ if ($ fix === true ) {
163
+ $ phpcsFile ->fixer ->replaceToken ($ stackPtr , $ typeLower );
164
+ }
165
+
166
+ }//end processType()
167
+
168
+
171
169
}//end class
0 commit comments