Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Universal/NoLeadingBackslash: add metrics #49

Merged
merged 1 commit into from
Apr 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Universal/Sniffs/UseStatements/NoLeadingBackslashSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
class NoLeadingBackslashSniff implements Sniff
{

/**
* Name of the metric.
*
* @since 1.0.0
*
* @var string
*/
const METRIC_NAME = 'Import use with leading backslash';

/**
* Returns an array of tokens this test wants to listen for.
*
Expand Down Expand Up @@ -86,6 +95,8 @@ public function process(File $phpcsFile, $stackPtr)
}

if ($tokens[$nextNonEmpty]['code'] === \T_NS_SEPARATOR) {
$phpcsFile->recordMetric($nextNonEmpty, self::METRIC_NAME, 'yes');

$error = 'An import use statement should never start with a leading backslash';
$fix = $phpcsFile->addFixableError($error, $nextNonEmpty, 'LeadingBackslashFound');

Expand All @@ -96,6 +107,8 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->fixer->replaceToken($nextNonEmpty, '');
}
}
} else {
$phpcsFile->recordMetric($nextNonEmpty, self::METRIC_NAME, 'no');
}

// Move the stackPtr forward to the next part of the use statement, if any.
Expand Down