7
7
use PHP_CodeSniffer \Exceptions \DeepExitException ;
8
8
use PHP_CodeSniffer \Files \File ;
9
9
use PHP_CodeSniffer \Sniffs \Sniff ;
10
- use PHP_CodeSniffer \Util \Common ;
11
10
use SymfonyCustom \Helpers \SniffHelper ;
12
11
13
12
/**
@@ -69,12 +68,18 @@ class ValidTypeHintSniff implements Sniff
69
68
|
70
69
(?<classString>
71
70
class-string(?:
72
- \s*<\s*[ \\\\\w]+\s*>
71
+ \s*<\s*
72
+ (?<classStringContent>
73
+ (?&simple)
74
+ )
75
+ \s*>
73
76
)?
74
77
)
75
78
|
76
79
(?<simple>
77
- [@$?]?[ \\\\\w]+
80
+ \\\\?\w+(?: \\\\\w+)*
81
+ |
82
+ \$this
78
83
)
79
84
)
80
85
)
@@ -84,6 +89,36 @@ class-string(?:
84
89
)
85
90
' ;
86
91
92
+ private const PRIMITIVES_TYPES = [
93
+ 'string ' ,
94
+ 'int ' ,
95
+ 'float ' ,
96
+ 'bool ' ,
97
+ 'array ' ,
98
+ 'resource ' ,
99
+ 'null ' ,
100
+ 'callable ' ,
101
+ 'iterable ' ,
102
+ ];
103
+ private const KEYWORD_TYPES = [
104
+ 'mixed ' ,
105
+ 'void ' ,
106
+ 'object ' ,
107
+ 'number ' ,
108
+ 'false ' ,
109
+ 'true ' ,
110
+ 'self ' ,
111
+ 'static ' ,
112
+ '$this ' ,
113
+ ];
114
+ private const ALIAS_TYPES = [
115
+ 'boolean ' => 'bool ' ,
116
+ 'integer ' => 'int ' ,
117
+ 'double ' => 'float ' ,
118
+ 'real ' => 'float ' ,
119
+ 'callback ' => 'callable ' ,
120
+ ];
121
+
87
122
/**
88
123
* @return array
89
124
*/
@@ -105,7 +140,7 @@ public function process(File $phpcsFile, $stackPtr): void
105
140
}
106
141
107
142
$ matchingResult = preg_match (
108
- '{^ ' .self ::REGEX_TYPES .'(?:[\s\t].*)?$}sx ' ,
143
+ '{^ ' .self ::REGEX_TYPES .'(?:[\s\t].*)?$}six ' ,
109
144
$ tokens [$ stackPtr + 2 ]['content ' ],
110
145
$ matches
111
146
);
@@ -153,16 +188,18 @@ private function getValidTypes(string $content): string
153
188
$ types = [];
154
189
$ separators = [];
155
190
while ('' !== $ content && false !== $ content ) {
156
- preg_match ('{^ ' .self ::REGEX_TYPES .'$}x ' , $ content , $ matches );
191
+ preg_match ('{^ ' .self ::REGEX_TYPES .'$}ix ' , $ content , $ matches );
157
192
158
- if (isset ($ matches ['multiple ' ]) && '' !== $ matches ['multiple ' ]) {
193
+ if (isset ($ matches ['array ' ]) && '' !== $ matches ['array ' ]) {
194
+ $ validType = $ this ->getValidTypes (substr ($ matches ['array ' ], 0 , -2 )).'[] ' ;
195
+ } elseif (isset ($ matches ['multiple ' ]) && '' !== $ matches ['multiple ' ]) {
159
196
$ validType = '( ' .$ this ->getValidTypes ($ matches ['mutipleContent ' ]).') ' ;
160
197
} elseif (isset ($ matches ['generic ' ]) && '' !== $ matches ['generic ' ]) {
161
198
$ validType = $ this ->getValidGenericType ($ matches ['genericName ' ], $ matches ['genericContent ' ]);
162
199
} elseif (isset ($ matches ['object ' ]) && '' !== $ matches ['object ' ]) {
163
200
$ validType = $ this ->getValidObjectType ($ matches ['objectContent ' ]);
164
- } elseif (isset ($ matches ['array ' ]) && '' !== $ matches ['array ' ]) {
165
- $ validType = $ this -> getValidTypes ( substr ( $ matches [ ' array ' ], 0 , - 2 )). ' [] ' ;
201
+ } elseif (isset ($ matches ['classString ' ]) && '' !== $ matches ['classString ' ]) {
202
+ $ validType = preg_replace ( ' /class-string/i ' , ' class-string ' , $ matches [ ' classString ' ]) ;
166
203
} else {
167
204
$ validType = $ this ->getValidType ($ matches ['type ' ]);
168
205
}
@@ -225,7 +262,7 @@ private function getValidGenericType(string $genericName, string $genericContent
225
262
$ validType = $ this ->getValidType ($ genericName ).'< ' ;
226
263
227
264
while ('' !== $ genericContent && false !== $ genericContent ) {
228
- preg_match ('{^ ' .self ::REGEX_TYPES .',?}x ' , $ genericContent , $ matches );
265
+ preg_match ('{^ ' .self ::REGEX_TYPES .',?}ix ' , $ genericContent , $ matches );
229
266
230
267
$ validType .= $ this ->getValidTypes ($ matches ['types ' ]).', ' ;
231
268
$ genericContent = substr ($ genericContent , strlen ($ matches ['types ' ]) + 1 );
@@ -253,7 +290,7 @@ private function getValidObjectType(string $objectContent): string
253
290
$ objectContent = $ split [2 ];
254
291
}
255
292
256
- preg_match ('{^ ' .self ::REGEX_TYPES .',?}x ' , $ objectContent , $ matches );
293
+ preg_match ('{^ ' .self ::REGEX_TYPES .',?}ix ' , $ objectContent , $ matches );
257
294
258
295
$ validType .= $ this ->getValidTypes ($ matches ['types ' ]).', ' ;
259
296
$ objectContent = substr ($ objectContent , strlen ($ matches ['types ' ]) + 1 );
@@ -270,15 +307,14 @@ private function getValidObjectType(string $objectContent): string
270
307
private function getValidType (string $ typeName ): string
271
308
{
272
309
$ lowerType = strtolower ($ typeName );
273
- switch ($ lowerType ) {
274
- case 'bool ' :
275
- case 'boolean ' :
276
- return 'bool ' ;
277
- case 'int ' :
278
- case 'integer ' :
279
- return 'int ' ;
310
+ if (in_array ($ lowerType , self ::PRIMITIVES_TYPES ) || in_array ($ lowerType , self ::KEYWORD_TYPES )) {
311
+ return $ lowerType ;
312
+ }
313
+
314
+ if (isset (self ::ALIAS_TYPES [$ lowerType ])) {
315
+ return self ::ALIAS_TYPES [$ lowerType ];
280
316
}
281
317
282
- return Common:: suggestType ( $ typeName) ;
318
+ return $ typeName ;
283
319
}
284
320
}
0 commit comments