14
14
*/
15
15
class ValidTypeHintSniff implements Sniff
16
16
{
17
- private const TEXT = '\\\\a-z0-9 ' ;
18
- private const OPENER = '\<\[\{ \( ' ;
19
- private const CLOSER = '\>\]\}\) ' ;
20
- private const MIDDLE = '\,\: ' ;
21
- private const SEPARATOR = '\&\| ' ;
17
+ private const TEXT = '[ \\\\a-z0-9] ' ;
18
+ private const OPENER = '\<|\[|\{| \( ' ;
19
+ private const MIDDLE = '\,|\:|\=\> ' ;
20
+ private const CLOSER = '\>|\]|\}|\) ' ;
21
+ private const SEPARATOR = '\&| \| ' ;
22
22
23
23
/**
24
24
* @return array
@@ -39,10 +39,12 @@ public function process(File $phpcsFile, $stackPtr): void
39
39
if (in_array ($ tokens [$ stackPtr ]['content ' ], SniffHelper::TAGS_WITH_TYPE )) {
40
40
preg_match (
41
41
'`^((?: '
42
- .'[ ' .self ::OPENER .self ::MIDDLE .self ::SEPARATOR .']\s* '
43
- .'|(?:[ ' .self ::TEXT .self ::CLOSER .']\s+) '
44
- .'(?=[\| ' .self ::OPENER .self ::MIDDLE .self ::CLOSER .self ::SEPARATOR .']) '
45
- .'|[ ' .self ::TEXT .self ::CLOSER .'] '
42
+ .'(?: ' .self ::OPENER .'| ' .self ::MIDDLE .'| ' .self ::SEPARATOR .')\s+ '
43
+ .'(?= ' .self ::TEXT .'| ' .self ::OPENER .'| ' .self ::MIDDLE .'| ' .self ::CLOSER .'| ' .self ::SEPARATOR .') '
44
+ .'| ' .self ::OPENER .'| ' .self ::MIDDLE .'| ' .self ::SEPARATOR
45
+ .'|(?: ' .self ::TEXT .'| ' .self ::CLOSER .')\s+ '
46
+ .'(?= ' .self ::OPENER .'| ' .self ::MIDDLE .'| ' .self ::CLOSER .'| ' .self ::SEPARATOR .') '
47
+ .'| ' .self ::TEXT .'| ' .self ::CLOSER .''
46
48
.')+)(.*)?`i ' ,
47
49
$ tokens [($ stackPtr + 2 )]['content ' ],
48
50
$ match
@@ -52,7 +54,6 @@ public function process(File $phpcsFile, $stackPtr): void
52
54
return ;
53
55
}
54
56
55
- // Check type (can be multiple, separated by '|').
56
57
$ type = $ match [1 ];
57
58
$ suggestedType = $ this ->getValidTypeName ($ type );
58
59
if ($ type !== $ suggestedType ) {
@@ -84,7 +85,7 @@ private function getValidTypeName(string $typeName): string
84
85
{
85
86
$ typeNameWithoutSpace = str_replace (' ' , '' , $ typeName );
86
87
$ parts = preg_split (
87
- '/([\| ' .self ::OPENER .self ::MIDDLE .self ::CLOSER .self ::SEPARATOR .'] )/ ' ,
88
+ '/( ' .self ::OPENER .' | ' . self ::MIDDLE .' | ' . self ::CLOSER .' | ' . self ::SEPARATOR .')/ ' ,
88
89
$ typeNameWithoutSpace ,
89
90
-1 ,
90
91
PREG_SPLIT_DELIM_CAPTURE
@@ -93,9 +94,15 @@ private function getValidTypeName(string $typeName): string
93
94
94
95
$ validType = '' ;
95
96
for ($ i = 0 ; $ i < $ partsNumber ; $ i += 2 ) {
96
- $ validType .= $ this ->suggestType ($ parts [$ i ]). $ parts [ $ i + 1 ] ;
97
+ $ validType .= $ this ->suggestType ($ parts [$ i ]);
97
98
98
- if (preg_match ('/[ ' .self ::MIDDLE .']/ ' , $ parts [$ i + 1 ])) {
99
+ if ('=> ' === $ parts [$ i + 1 ]) {
100
+ $ validType .= ' ' ;
101
+ }
102
+
103
+ $ validType .= $ parts [$ i + 1 ];
104
+
105
+ if (preg_match ('/ ' .self ::MIDDLE .'/ ' , $ parts [$ i + 1 ])) {
99
106
$ validType .= ' ' ;
100
107
}
101
108
}
0 commit comments