Skip to content

Commit

Permalink
Initial attempt at adding metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo-d committed Dec 3, 2022
1 parent 4c08369 commit a775e65
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion Universal/Sniffs/DeclareStatements/BlockModeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@
class BlockModeSniff implements Sniff
{

/**
* Name of the metric.
*
* @since 1.0.0
*
* @var string
*/
const DECLARE_SCOPE_METRIC = 'Declare directive scope';

/**
* Name of the metric.
*
* @since 1.0.0
*
* @var string
*/
const DECLARE_TYPE_METRIC = 'Declare directive type';

/**
* The option for the encoding directive.
*
Expand Down Expand Up @@ -144,6 +162,12 @@ public function process(File $phpcsFile, $stackPtr)

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

if ($usesBlockMode) {
$phpcsFile->recordMetric($stackPtr, self::DECLARE_SCOPE_METRIC, 'Block');
} else {
$phpcsFile->recordMetric($stackPtr, self::DECLARE_SCOPE_METRIC, 'Global');
}

// If strict types is defined using block mode, throw error.
if ($usesBlockMode && isset($directiveStrings['strict_types'])) {
$phpcsFile->addError(
Expand All @@ -153,7 +177,14 @@ public function process(File $phpcsFile, $stackPtr)
);
return;
}
// To do: Add a fixer!
/*
* To do: Add a fixer
*
* But only if strict_types is on its own. In this case we should remove the last brace,
* remove the first one, and after the closing parenthesis add a comma.
*
* Add a fixable test for this case!
*/

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

foreach (\array_keys($directiveStrings) as $directiveString) {
if (isset($this->allowedDirectives[$directiveString])) {
$phpcsFile->recordMetric($stackPtr, self::DECLARE_TYPE_METRIC, $directiveString);
}
}

// Multiple directives - if one requires block mode usage, other has to as well.
if (count($directiveStrings) > 1
&& (($this->encodingBlockMode === 'disallow' && $this->ticksBlockMode !== 'disallow')
Expand Down

0 comments on commit a775e65

Please sign in to comment.