View source
<?php
namespace Drupal\Sniffs\Commenting;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class DocCommentAlignmentSniff implements Sniff {
public function register() {
return [
T_DOC_COMMENT_OPEN_TAG,
];
}
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
$ignore = Tokens::$emptyTokens;
if ($phpcsFile->tokenizerType === 'JS') {
$ignore[] = T_EQUAL;
$ignore[] = T_STRING;
$ignore[] = T_OBJECT_OPERATOR;
}
$nextToken = $phpcsFile
->findNext($ignore, $stackPtr + 1, null, true);
$ignore = [
T_CLASS => true,
T_INTERFACE => true,
T_FUNCTION => true,
T_PUBLIC => true,
T_PRIVATE => true,
T_PROTECTED => true,
T_STATIC => true,
T_ABSTRACT => true,
T_PROPERTY => true,
T_OBJECT => true,
T_PROTOTYPE => true,
T_VAR => true,
];
if (isset($ignore[$tokens[$nextToken]['code']]) === false) {
$prevToken = $phpcsFile
->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) {
return;
}
}
$requiredColumn = $tokens[$stackPtr]['column'] + 1;
$endComment = $tokens[$stackPtr]['comment_closer'];
for ($i = $stackPtr + 1; $i <= $endComment; $i++) {
if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR && $tokens[$i]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
continue;
}
if ($tokens[$i]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
$prev = $phpcsFile
->findPrevious(T_DOC_COMMENT_WHITESPACE, $i - 1, $stackPtr, true);
if ($tokens[$prev]['line'] === $tokens[$i]['line']) {
continue;
}
}
if ($tokens[$i]['column'] !== $requiredColumn) {
$error = 'Expected %s space(s) before asterisk; %s found';
$data = [
$requiredColumn - 1,
$tokens[$i]['column'] - 1,
];
$fix = $phpcsFile
->addFixableError($error, $i, 'SpaceBeforeStar', $data);
if ($fix === true) {
$padding = str_repeat(' ', $requiredColumn - 1);
if ($tokens[$i]['column'] === 1) {
$phpcsFile->fixer
->addContentBefore($i, $padding);
}
else {
$phpcsFile->fixer
->replaceToken($i - 1, $padding);
}
}
}
if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) {
continue;
}
if ($tokens[$i + 2]['line'] !== $tokens[$i]['line']) {
continue;
}
if ($tokens[$i + 1]['code'] !== T_DOC_COMMENT_WHITESPACE) {
$error = 'Expected 1 space after asterisk; 0 found';
$fix = $phpcsFile
->addFixableError($error, $i, 'NoSpaceAfterStar');
if ($fix === true) {
$phpcsFile->fixer
->addContent($i, ' ');
}
}
else {
if ($tokens[$i + 2]['code'] === T_DOC_COMMENT_TAG && $tokens[$i + 1]['content'] !== ' ' && in_array($tokens[$i + 2]['content'], [
'@param',
'@return',
'@throws',
'@ingroup',
'@var',
]) === true) {
$error = 'Expected 1 space after asterisk; %s found';
$data = [
strlen($tokens[$i + 1]['content']),
];
$fix = $phpcsFile
->addFixableError($error, $i, 'SpaceAfterStar', $data);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($i + 1, ' ');
}
}
}
}
}
}