38
38
class BlockModeSniff implements Sniff
39
39
{
40
40
41
+ /**
42
+ * Name of the metric.
43
+ *
44
+ * @since 1.0.0
45
+ *
46
+ * @var string
47
+ */
48
+ const DECLARE_SCOPE_METRIC = 'Declare directive scope ' ;
49
+
50
+ /**
51
+ * Name of the metric.
52
+ *
53
+ * @since 1.0.0
54
+ *
55
+ * @var string
56
+ */
57
+ const DECLARE_TYPE_METRIC = 'Declare directive type ' ;
58
+
41
59
/**
42
60
* The option for the encoding directive.
43
61
*
@@ -144,6 +162,12 @@ public function process(File $phpcsFile, $stackPtr)
144
162
145
163
$ usesBlockMode = isset ($ tokens [$ stackPtr ]['scope_opener ' ]);
146
164
165
+ if ($ usesBlockMode ) {
166
+ $ phpcsFile ->recordMetric ($ stackPtr , self ::DECLARE_SCOPE_METRIC , 'Block ' );
167
+ } else {
168
+ $ phpcsFile ->recordMetric ($ stackPtr , self ::DECLARE_SCOPE_METRIC , 'Global ' );
169
+ }
170
+
147
171
// If strict types is defined using block mode, throw error.
148
172
if ($ usesBlockMode && isset ($ directiveStrings ['strict_types ' ])) {
149
173
$ phpcsFile ->addError (
@@ -153,7 +177,14 @@ public function process(File $phpcsFile, $stackPtr)
153
177
);
154
178
return ;
155
179
}
156
- // To do: Add a fixer!
180
+ /*
181
+ * To do: Add a fixer
182
+ *
183
+ * But only if strict_types is on its own. In this case we should remove the last brace,
184
+ * remove the first one, and after the closing parenthesis add a comma.
185
+ *
186
+ * Add a fixable test for this case!
187
+ */
157
188
158
189
// Check if there is a code between the declare statement and opening brace/alternative syntax.
159
190
$ nextNonEmpty = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ closeParenPtr + 1 ), null , true );
@@ -168,6 +199,12 @@ public function process(File $phpcsFile, $stackPtr)
168
199
return ;
169
200
}
170
201
202
+ foreach (\array_keys ($ directiveStrings ) as $ directiveString ) {
203
+ if (isset ($ this ->allowedDirectives [$ directiveString ])) {
204
+ $ phpcsFile ->recordMetric ($ stackPtr , self ::DECLARE_TYPE_METRIC , $ directiveString );
205
+ }
206
+ }
207
+
171
208
// Multiple directives - if one requires block mode usage, other has to as well.
172
209
if (count ($ directiveStrings ) > 1
173
210
&& (($ this ->encodingBlockMode === 'disallow ' && $ this ->ticksBlockMode !== 'disallow ' )
0 commit comments