Skip to content

Commit 5e27267

Browse files
author
Nikolay Shapovalov
committed
Fixes after review, add test case for "hook_"
1 parent d87116f commit 5e27267

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

coder_sniffer/Drupal/Sniffs/Attributes/ValidHookNameSniff.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ public function process(File $phpcsFile, $stackPtr)
5757
&& $tokens[$attributeName]['content'] === 'Hook'
5858
) {
5959
$hookName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($attributeName + 2));
60-
if ($hookName !== false
61-
) {
60+
if ($hookName !== false) {
6261
// Remove outer quotes.
6362
$hookNameValue = trim($tokens[$hookName]['content'], '"\'');
6463

65-
if (strpos($hookNameValue, 'hook_') === 0) {
66-
$fix = $phpcsFile->addFixableWarning("Hook name should not start with 'hook_'. Hook name used: $hookNameValue", $hookName, 'HookPrefix');
67-
if ($fix === true && strlen($hookNameValue) > 5) {
68-
// Remove "hook_" prefix.
69-
$hookNameValueFixed = substr($hookNameValue, 5);
64+
if (strpos($hookNameValue, 'hook_') === 0 && $hookNameValue !== 'hook_') {
65+
// Remove "hook_" prefix.
66+
$hookNameValueFixed = substr($hookNameValue, 5);
67+
$message = sprintf("The hook name should not start with 'hook_', expected '%s' but found '%s", $hookNameValueFixed, $hookNameValue);
68+
69+
$fix = $phpcsFile->addFixableWarning($message, $hookName, 'HookPrefix');
70+
if ($fix === true) {
7071
// Return outer quotes.
7172
$hookNameValueFixed = str_replace($hookNameValue, $hookNameValueFixed, $tokens[$hookName]['content']);
7273
$phpcsFile->fixer->replaceToken($hookName, $hookNameValueFixed);

tests/Drupal/Attributes/ValidHookNameUnitTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ function module_node_load() {
2929

3030
}
3131

32+
/**
33+
* Not finished hook name. No warning
34+
*/
35+
#[Hook('hook_')]
36+
function module_system() {
37+
38+
}
3239

3340
/**
3441
* Attribute named argument.

tests/Drupal/Attributes/ValidHookNameUnitTest.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ function module_node_load() {
2929

3030
}
3131

32+
/**
33+
* Not finished hook name. No warning.
34+
*/
35+
#[Hook('hook_')]
36+
function module_system() {
37+
38+
}
39+
3240
/**
3341
* Attribute named argument.
3442
*/

tests/Drupal/Attributes/ValidHookNameUnitTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ protected function getWarningList(string $testFile): array
4040
return [
4141
19 => 1,
4242
27 => 1,
43-
36 => 1,
44-
44 => 1,
45-
52 => 1,
46-
62 => 1,
47-
85 => 1,
48-
100 => 1,
43+
43 => 1,
44+
51 => 1,
45+
59 => 1,
46+
69 => 1,
47+
92 => 1,
48+
107 => 1,
4949
];
5050

5151
}//end getWarningList()

0 commit comments

Comments
 (0)