@@ -34,6 +34,11 @@ class DocBlockTypeOrderSniff extends AbstractSniff {
34
34
'false ' ,
35
35
];
36
36
37
+ /**
38
+ * @var string[]
39
+ */
40
+ protected $ sortOrder ;
41
+
37
42
/**
38
43
* @inheritDoc
39
44
*/
@@ -88,7 +93,7 @@ protected function assertOrder(File $phpCsFile, array $docBlockParams) {
88
93
}
89
94
$ expectedTypes = implode ('| ' , $ expectedOrder );
90
95
91
- $ fix = $ phpCsFile ->addFixableError ('Nullish/falsely value should be the last element, expected ` ' . $ expectedTypes . '` ' , $ docBlockParam ['index ' ], 'WrongOrder ' );
96
+ $ fix = $ phpCsFile ->addFixableError ('Nullish/falsely value in ` ' . $ docBlockParam [ ' type ' ] . ' ` should be the last element, expected ` ' . $ expectedTypes . '` ' , $ docBlockParam ['index ' ], 'WrongOrder ' );
92
97
if (!$ fix ) {
93
98
continue ;
94
99
}
@@ -110,29 +115,90 @@ protected function assertOrder(File $phpCsFile, array $docBlockParams) {
110
115
* @return string[]
111
116
*/
112
117
protected function getExpectedOrder (array $ elements ) {
113
- global $ sortOrder ;
118
+ if (version_compare (PHP_VERSION , '7.0 ' ) < 0 ) {
119
+ return $ this ->getExpectedOrderLegacy ($ elements );
120
+ }
114
121
115
122
$ sortOrder = array_reverse ($ this ->sortMap );
123
+ $ this ->sortOrder = $ sortOrder ;
124
+
116
125
usort ($ elements , [$ this , 'compare ' ]);
117
126
118
127
return $ elements ;
119
128
}
120
129
130
+ /**
131
+ * For PHP 5 we need a custom fallback sort.
132
+ *
133
+ * @param string[] $elements
134
+ *
135
+ * @return string[]
136
+ */
137
+ protected function getExpectedOrderLegacy (array $ elements ) {
138
+ $ sortOrder = array_reverse ($ this ->sortMap );
139
+ $ sortOrder = array_flip ($ sortOrder );
140
+ foreach ($ elements as $ element ) {
141
+ if (!isset ($ sortOrder [$ element ])) {
142
+ $ sortOrder [$ element ] = -1 ;
143
+ }
144
+ }
145
+
146
+ $ array = [];
147
+ foreach ($ elements as $ element ) {
148
+ $ array [$ element ] = $ sortOrder [$ element ];
149
+ }
150
+
151
+ static ::asort ($ array );
152
+
153
+ $ elements = array_keys ($ array );
154
+
155
+ return $ elements ;
156
+ }
157
+
158
+ /**
159
+ * asort() but with a/b of same value to keep existing order.
160
+ *
161
+ * Required for PHP5, as the order can be inverse here.
162
+ *
163
+ * @param array $array
164
+ * @param int $sortFlags
165
+ *
166
+ * @return bool
167
+ */
168
+ protected static function asort (array &$ array , $ sortFlags = SORT_REGULAR ) {
169
+ $ index = 0 ;
170
+ foreach ($ array as &$ item ) {
171
+ $ item = [$ index ++, $ item ];
172
+ }
173
+ $ result = uasort ($ array , function ($ a , $ b ) use ($ sortFlags ) {
174
+ if ($ a [1 ] == $ b [1 ]) {
175
+ return $ a [0 ] - $ b [0 ];
176
+ }
177
+ $ set = [-1 => $ a [1 ], 1 => $ b [1 ]];
178
+ asort ($ set , $ sortFlags );
179
+ reset ($ set );
180
+ return key ($ set );
181
+ });
182
+ foreach ($ array as &$ item ) {
183
+ $ item = $ item [1 ];
184
+ }
185
+ return $ result ;
186
+ }
187
+
121
188
/**
122
189
* @param string $a
123
190
* @param string $b
124
191
*
125
192
* @return int
126
193
*/
127
194
protected function compare ($ a , $ b ) {
128
- global $ sortOrder ;
195
+ $ sortOrder = $ this -> sortOrder ;
129
196
130
197
$ aIndex = array_search ($ a , $ sortOrder , true );
131
198
$ bIndex = array_search ($ b , $ sortOrder , true );
132
199
if ($ aIndex === false ) {
133
200
return -1 ;
134
201
}
135
-
136
202
if ($ bIndex === false ) {
137
203
return 1 ;
138
204
}
0 commit comments