Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FormAlterDocSniff.php

class FormAlterDocSniff

Checks that the comment "Implements hook_form_alter()." actually matches the function signature.

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

  • class \Drupal\Sniffs\Semantics\FunctionDefinition implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \DrupalPractice\Sniffs\FunctionDefinitions\FormAlterDocSniff extends \Drupal\Sniffs\Semantics\FunctionDefinition

Expanded class hierarchy of FormAlterDocSniff

File

vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/FunctionDefinitions/FormAlterDocSniff.php, line 24

Namespace

DrupalPractice\Sniffs\FunctionDefinitions
View source
class FormAlterDocSniff extends FunctionDefinition {
    
    /**
     * Process this function definition.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
     * @param int                         $stackPtr    The position of the function name
     *                                                 in the stack.
     * @param int                         $functionPtr The position of the function keyword
     *                                                 in the stack.
     *
     * @return void
     */
    public function processFunction(File $phpcsFile, $stackPtr, $functionPtr) {
        $tokens = $phpcsFile->getTokens();
        $docCommentEnd = $phpcsFile->findPrevious(T_WHITESPACE, $functionPtr - 1, null, true);
        // If there is no doc comment there is nothing we can check.
        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 = [
                $tokens[$stackPtr]['content'],
                $projectName . '_form_alter',
            ];
            $phpcsFile->addWarning($warning, $commentLine, 'Different', $data);
        }
    }
    
    //end processFunction()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FormAlterDocSniff::processFunction public function Process this function definition. Overrides FunctionDefinition::processFunction
FunctionDefinition::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process
FunctionDefinition::register public function Returns an array of tokens this test wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal