Skip to content

Commit c66c7d7

Browse files
author
Nikolay Shapovalov
committed
fix
1 parent 9bdfba7 commit c66c7d7

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

coder_sniffer/Drupal/Sniffs/Classes/TestSniff.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PHP_CodeSniffer\Sniffs\Sniff;
1414

1515
/**
16-
* Checks that class references do not use FQN but use statements.
16+
* Checks that Hook attribute argument name not starts with "hook_" prefix.
1717
*
1818
* @category PHP
1919
* @package PHP_CodeSniffer
@@ -52,14 +52,19 @@ public function register()
5252
public function process(File $phpcsFile, $stackPtr)
5353
{
5454
$tokens = $phpcsFile->getTokens();
55-
$end = $phpcsFile->findNext([T_ATTRIBUTE_END], ($stackPtr + 2));
56-
$shortContent = '';
57-
if ()
58-
for ($i = ($stackPtr + 1); $i < $end; $i++) {
59-
$shortContent .= $tokens[$i]['content'];
60-
}
61-
if (preg_match('/^hook\(.hook_/i', $shortContent, $matches) === 1) {
62-
var_dump($matches);
55+
// $shortContent = '';
56+
// $end = $phpcsFile->findNext([T_ATTRIBUTE_END], ($stackPtr + 1));
57+
// for ($i = ($stackPtr + 1); $i < $end; $i++) {
58+
// $shortContent .= $tokens[$i]['content'];
59+
// }
60+
// $a = 1;
61+
62+
if ($tokens[$stackPtr + 1]['type'] === 'T_STRING'
63+
&& $tokens[$stackPtr + 1]['content'] === 'Hook'
64+
&& $tokens[$stackPtr + 3]['type'] === 'T_CONSTANT_ENCAPSED_STRING'
65+
&& str_contains($tokens[$stackPtr + 3]['content'], 'hook_')
66+
) {
67+
$phpcsFile->addWarning('Hook name should not start with "hook_" prefix. Hook name used:' . $tokens[$stackPtr + 3]['content'], $stackPtr + 3,'HookAttributePrefixName');
6368
}
6469

6570
}//end process()

tests/Drupal/bad/BadUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ protected function getWarningList(string $testFile): array
435435
823 => 1,
436436
824 => 1,
437437
836 => 1,
438+
874 => 1,
438439
];
439440
}//end switch
440441

tests/Drupal/bad/bad.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,4 +868,12 @@ function test30(TestType $a = NULL) {
868868
echo "Hello";
869869
}
870870

871+
/**
872+
* Implements hook_node_view().
873+
*/
874+
#[Hook('hook_node_view')]
875+
function bad_node_view() {
876+
877+
}
878+
871879
?>

tests/Drupal/bad/bad.php.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,3 +921,11 @@ function test29(
921921
function test30(?TestType $a = NULL) {
922922
echo "Hello";
923923
}
924+
925+
/**
926+
* Implements hook_node_view().
927+
*/
928+
#[Hook('node_view')]
929+
function bad_node_view() {
930+
931+
}

0 commit comments

Comments
 (0)