Skip to content

Commit

Permalink
remove hook_info special handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Shapovalov committed Jan 21, 2025
1 parent 988f22f commit d87116f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 48 deletions.
11 changes: 1 addition & 10 deletions coder_sniffer/Drupal/Sniffs/Attributes/ValidHookNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
class ValidHookNameSniff implements Sniff
{

/**
* List of hooks that should not be fixed.
*
* @var string[]
*/
public array $hookExceptions = ['hook_info'];


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -69,9 +62,7 @@ public function process(File $phpcsFile, $stackPtr)
// Remove outer quotes.
$hookNameValue = trim($tokens[$hookName]['content'], '"\'');

if (in_array($hookNameValue, $this->hookExceptions) === false
&& strpos($hookNameValue, 'hook_') === 0
) {
if (strpos($hookNameValue, 'hook_') === 0) {
$fix = $phpcsFile->addFixableWarning("Hook name should not start with 'hook_'. Hook name used: $hookNameValue", $hookName, 'HookPrefix');
if ($fix === true && strlen($hookNameValue) > 5) {
// Remove "hook_" prefix.
Expand Down
20 changes: 2 additions & 18 deletions tests/Drupal/Attributes/ValidHookNameUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function module_valid() {
/**
* Single quotes.
*/
#[Hook('hook_node_view')]
function module_node_view() {
#[Hook('hook_info')]
function module_info() {

}

Expand Down Expand Up @@ -64,22 +64,6 @@ function module_hookpirate_view() {

}

/**
* "hook_info" is exception. No warning.
*/
#[Hook('hook_info')]
function module_hook_info() {

}

/**
* Named argument. "hook_info" is exception. No warning.
*/
#[Hook(hook: 'hook_info')]
function mymodule_hook_info() {

}

/**
* Valid hook.
*/
Expand Down
20 changes: 2 additions & 18 deletions tests/Drupal/Attributes/ValidHookNameUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function module_valid() {
/**
* Single quotes.
*/
#[Hook('node_view')]
function module_node_view() {
#[Hook('info')]
function module_info() {

}

Expand Down Expand Up @@ -63,22 +63,6 @@ function module_hookpirate_view() {

}

/**
* "hook_info" is exception. No warning.
*/
#[Hook('hook_info')]
function module_hook_info() {

}

/**
* Named argument. "hook_info" is exception. No warning.
*/
#[Hook(hook: 'hook_info')]
function mymodule_hook_info() {

}

/**
* Valid hook.
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/Drupal/Attributes/ValidHookNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ protected function getWarningList(string $testFile): array
44 => 1,
52 => 1,
62 => 1,
101 => 1,
116 => 1,
85 => 1,
100 => 1,
];

}//end getWarningList()
Expand Down

0 comments on commit d87116f

Please sign in to comment.