-
Notifications
You must be signed in to change notification settings - Fork 53
feat(ValidHookName): Add sniff for hook name in PHP attributes #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
3d9d0b5
49a47e3
3ec769a
49ad673
a26ccc7
48b339f
a8c8966
3329b89
74a0534
9c6f937
0f46b15
970f9a3
28a9622
988f22f
d87116f
826142d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/** | ||
* \Drupal\Sniffs\Attribute\ValidHookNameSniff. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
namespace Drupal\Sniffs\Attributes; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
/** | ||
* Checks that Hook attribute argument name not starts with "hook_" prefix. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
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. | ||
* | ||
* @return array<int|string> | ||
*/ | ||
public function register() | ||
{ | ||
return [T_ATTRIBUTE]; | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the | ||
* token was found. | ||
* @param int $stackPtr The position in the PHP_CodeSniffer | ||
* file's token stack where the token | ||
* was found. | ||
* | ||
* @return void|int Optionally returns a stack pointer. The sniff will not be | ||
* called again on the current file until the returned stack | ||
* pointer is reached. Return $phpcsFile->numTokens + 1 to skip | ||
* the rest of the file. | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
$attributeName = $phpcsFile->findNext(T_STRING, ($stackPtr + 1)); | ||
if ($attributeName !== false | ||
&& $tokens[$attributeName]['content'] === 'Hook' | ||
) { | ||
$hookName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($attributeName + 2)); | ||
if ($hookName !== false | ||
) { | ||
// Remove outer quotes. | ||
$hookNameValue = trim($tokens[$hookName]['content'], '"\''); | ||
|
||
if (in_array($hookNameValue, $this->hookExceptions) === false | ||
&& 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. | ||
$hookNameValueFixed = substr($hookNameValue, 5); | ||
|
||
// Return outer quotes. | ||
$hookNameValueFixed = str_replace($hookNameValue, $hookNameValueFixed, $tokens[$hookName]['content']); | ||
$phpcsFile->fixer->replaceToken($hookName, $hookNameValueFixed); | ||
} | ||
} | ||
} | ||
}//end if | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Test. | ||
*/ | ||
|
||
/** | ||
* Valid hook. | ||
*/ | ||
#[Hook('valid')] | ||
function module_valid() { | ||
|
||
} | ||
|
||
/** | ||
* Single quotes. | ||
*/ | ||
#[Hook('hook_node_view')] | ||
function module_node_view() { | ||
|
||
} | ||
|
||
/** | ||
* Double quotes. | ||
*/ | ||
#[Hook("hook_node_load")] | ||
function module_node_load() { | ||
|
||
} | ||
|
||
|
||
/** | ||
* Attribute named argument. | ||
*/ | ||
#[Hook(hook: 'hook_node_delete')] | ||
function module_node_delete() { | ||
|
||
} | ||
|
||
/** | ||
* Attribute named arguments. | ||
*/ | ||
#[Hook(hook: 'hook_node_alter', module: 'custom_module')] | ||
function module_node_alter() { | ||
|
||
} | ||
|
||
/** | ||
* "hook" is a part of hook name. | ||
*/ | ||
#[Hook('hook_piratehook_view')] | ||
function module_piratehook_view() { | ||
|
||
} | ||
|
||
/** | ||
* Implements hook_hookpirate_view(). | ||
* | ||
* "hook" is a part of hook name. No warning. | ||
*/ | ||
#[Hook('hook_hookpirate_view')] | ||
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. | ||
*/ | ||
#[Hook('valid', 'validMethod', 'module')] | ||
class ValidHooks { | ||
|
||
/** | ||
* | ||
*/ | ||
public function validMethod() { | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* | ||
*/ | ||
#[Hook('hook_user_cancel', 'userCancel', 'custom')] | ||
class Hooks { | ||
|
||
/** | ||
* | ||
*/ | ||
public function userCancel() { | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* Named arguments, double quotes. | ||
*/ | ||
#[Hook(hook: "hook_user_login", method: "userLogin", module: "views")] | ||
class MyHooks { | ||
|
||
/** | ||
* | ||
*/ | ||
public function userLogin() { | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Test. | ||
*/ | ||
|
||
/** | ||
* Valid hook. | ||
*/ | ||
#[Hook('valid')] | ||
function module_valid() { | ||
|
||
} | ||
|
||
/** | ||
* Single quotes. | ||
*/ | ||
#[Hook('node_view')] | ||
function module_node_view() { | ||
|
||
} | ||
|
||
/** | ||
* Double quotes. | ||
*/ | ||
#[Hook("node_load")] | ||
function module_node_load() { | ||
|
||
} | ||
|
||
/** | ||
* Attribute named argument. | ||
*/ | ||
#[Hook(hook: 'node_delete')] | ||
function module_node_delete() { | ||
|
||
} | ||
|
||
/** | ||
* Attribute named arguments. | ||
*/ | ||
#[Hook(hook: 'node_alter', module: 'custom_module')] | ||
function module_node_alter() { | ||
|
||
} | ||
|
||
/** | ||
* "hook" is a part of hook name. | ||
*/ | ||
#[Hook('piratehook_view')] | ||
function module_piratehook_view() { | ||
|
||
} | ||
|
||
/** | ||
* Implements hook_hookpirate_view(). | ||
* | ||
* "hook" is a part of hook name. No warning. | ||
*/ | ||
#[Hook('hookpirate_view')] | ||
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. | ||
*/ | ||
#[Hook('valid', 'validMethod', 'module')] | ||
class ValidHooks { | ||
|
||
/** | ||
* | ||
*/ | ||
public function validMethod() { | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* | ||
*/ | ||
#[Hook('user_cancel', 'userCancel', 'custom')] | ||
class Hooks { | ||
|
||
/** | ||
* | ||
*/ | ||
public function userCancel() { | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* Named arguments, double quotes. | ||
*/ | ||
#[Hook(hook: "user_login", method: "userLogin", module: "views")] | ||
class MyHooks { | ||
|
||
/** | ||
* | ||
*/ | ||
public function userLogin() { | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Drupal\Test\Attributes; | ||
|
||
use Drupal\Test\CoderSniffUnitTest; | ||
|
||
class ValidHookNameUnitTest 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. | ||
* | ||
* @param string $testFile The name of the file being tested. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
protected function getErrorList(string $testFile): array | ||
{ | ||
return []; | ||
|
||
}//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. | ||
* | ||
* @param string $testFile The name of the file being tested. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
protected function getWarningList(string $testFile): array | ||
{ | ||
return [ | ||
19 => 1, | ||
27 => 1, | ||
36 => 1, | ||
44 => 1, | ||
52 => 1, | ||
62 => 1, | ||
101 => 1, | ||
116 => 1, | ||
]; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |
Uh oh!
There was an error while loading. Please reload this page.