FormAlterDocSniff.php in Coder 8.2
File
coder_sniffer/DrupalPractice/Sniffs/FunctionDefinitions/FormAlterDocSniff.php
View source
<?php
namespace DrupalPractice\Sniffs\FunctionDefinitions;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionDefinition;
use DrupalPractice\Project;
class FormAlterDocSniff extends FunctionDefinition {
public function processFunction(File $phpcsFile, $stackPtr, $functionPtr) {
$tokens = $phpcsFile
->getTokens();
$docCommentEnd = $phpcsFile
->findPrevious(T_WHITESPACE, $functionPtr - 1, null, true);
if ($docCommentEnd === false || $tokens[$docCommentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
return;
}
$commentLine = $docCommentEnd - 1;
$commentFound = false;
while ($tokens[$commentLine]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
if (strpos($tokens[$commentLine]['content'], 'Implements hook_form_alter().') === 0) {
$commentFound = true;
break;
}
$commentLine--;
}
if ($commentFound === false) {
return;
}
$projectName = Project::getName($phpcsFile);
if ($projectName === false) {
return;
}
if ($tokens[$stackPtr]['content'] !== $projectName . '_form_alter') {
$warning = 'Doc comment indicates hook_form_alter() but function signature is "%s" instead of "%s". Did you mean hook_form_FORM_ID_alter()?';
$data = array(
$tokens[$stackPtr]['content'],
$projectName . '_form_alter',
);
$phpcsFile
->addWarning($warning, $commentLine, 'Different', $data);
}
}
}