Unverified Commit d99e0f44 authored by Klaus Purer's avatar Klaus Purer Committed by GitHub
Browse files

feat(HookComment): Check hook implementation comments if they repeat the function name (#26)

parent b1446459
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -111,8 +111,22 @@ class HookCommentSniff implements Sniff
                    }
                }
            }//end if

            return;
        }//end if

        // Check if the doc block just repeats the function name with
        // "Implements example_hook_name()".
        $functionName = $phpcsFile->getDeclarationName($stackPtr);
        if ($functionName !== null && preg_match("/^[\s]*Implements $functionName\(\)\.$/i", $shortContent) === 1) {
            $error = 'Hook implementations must be documented with "Implements hook_example()."';
            $fix   = $phpcsFile->addFixableError($error, $short, 'HookRepeat');
            if ($fix === true) {
                $newComment = preg_replace('/Implements [^_]+/', 'Implements hook', $shortContent);
                $phpcsFile->fixer->replaceToken($short, $newComment);
            }
        }

    }//end process()


+12 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Example file with hook implementations.
 */

/**
 * Implements example_page_build().
 */
function example_page_build(&$page) {
}
+12 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Example file with hook implementations.
 */

/**
 * Implements hook_page_build().
 */
function example_page_build(&$page) {
}
+42 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Sniffs\Commenting;

use Drupal\Test\CoderSniffUnitTest;

class HookCommentUnitTest extends CoderSniffUnitTest
{

    /**
     * Returns the lines where errors should occur.
     *
     * The key of the array should represent the line number and the value
     * should represent the number of errors that should occur on that line.
     *
     * @return array(int => int)
     */
    public function getErrorList($testFile = NULL)
    {
        return array(
                9 => 1,
               );

    }//end getErrorList()


    /**
     * Returns the lines where warnings should occur.
     *
     * The key of the array should represent the line number and the value
     * should represent the number of warnings that should occur on that line.
     *
     * @return array(int => int)
     */
    public function getWarningList($testFile = NULL)
    {
        return array();

    }//end getWarningList()


}//end class