Skip to content

Commit a775e65

Browse files
committed
Initial attempt at adding metrics
1 parent 4c08369 commit a775e65

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

Universal/Sniffs/DeclareStatements/BlockModeSniff.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@
3838
class BlockModeSniff implements Sniff
3939
{
4040

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+
4159
/**
4260
* The option for the encoding directive.
4361
*
@@ -144,6 +162,12 @@ public function process(File $phpcsFile, $stackPtr)
144162

145163
$usesBlockMode = isset($tokens[$stackPtr]['scope_opener']);
146164

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+
147171
// If strict types is defined using block mode, throw error.
148172
if ($usesBlockMode && isset($directiveStrings['strict_types'])) {
149173
$phpcsFile->addError(
@@ -153,7 +177,14 @@ public function process(File $phpcsFile, $stackPtr)
153177
);
154178
return;
155179
}
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+
*/
157188

158189
// Check if there is a code between the declare statement and opening brace/alternative syntax.
159190
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($closeParenPtr + 1), null, true);
@@ -168,6 +199,12 @@ public function process(File $phpcsFile, $stackPtr)
168199
return;
169200
}
170201

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+
171208
// Multiple directives - if one requires block mode usage, other has to as well.
172209
if (count($directiveStrings) > 1
173210
&& (($this->encodingBlockMode === 'disallow' && $this->ticksBlockMode !== 'disallow')

0 commit comments

Comments
 (0)