Skip to content

Commit bbd9a76

Browse files
committed
Merge remote-tracking branch 'origin/8.3.x' into fix-function-phpcs-ignore
2 parents 049f757 + 99b280c commit bbd9a76

File tree

6 files changed

+372
-2
lines changed

6 files changed

+372
-2
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ jobs:
8484
# core is updated to that version.
8585
run: |
8686
cd drupal/core
87-
../../vendor/bin/phpcs -p -s --ignore=lib/Drupal/Core/Entity/EntityType.php,lib/Drupal/Core/Recipe/RecipeInputFormTrait.php,lib/Drupal/Core/Form/FormState.php,modules/migrate/src/Plugin/Migration.php,modules/views/src/ViewExecutable.php,modules/views/src/Plugin/views/style/StylePluginBase.php,core/lib/Drupal/Core/FileTransfer/FTP.php,core/lib/Drupal/Core/FileTransfer/SSH.php
87+
../../vendor/bin/phpcs -p -s --ignore=lib/Drupal/Core/Entity/EntityType.php,lib/Drupal/Core/Recipe/RecipeInputFormTrait.php,lib/Drupal/Core/Form/FormState.php,modules/migrate/src/Plugin/Migration.php,modules/views/src/ViewExecutable.php,modules/views/src/Plugin/views/style/StylePluginBase.php,core/lib/Drupal/Core/FileTransfer/FTP.php,core/lib/Drupal/Core/FileTransfer/SSH.php,modules/system/tests/modules/theme_test/src/EventSubscriber/ThemeTestSubscriber.php
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* \Drupal\Sniffs\Attributes\ValidHookNameSniff.
4+
*
5+
* @category PHP
6+
* @package PHP_CodeSniffer
7+
* @link http://pear.php.net/package/PHP_CodeSniffer
8+
*/
9+
10+
namespace Drupal\Sniffs\Attributes;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
15+
/**
16+
* Checks that Hook attribute argument name not starts with "hook_" prefix.
17+
*
18+
* @category PHP
19+
* @package PHP_CodeSniffer
20+
* @link http://pear.php.net/package/PHP_CodeSniffer
21+
*/
22+
class ValidHookNameSniff implements Sniff
23+
{
24+
25+
26+
/**
27+
* Returns an array of tokens this test wants to listen for.
28+
*
29+
* @return array<int|string>
30+
*/
31+
public function register()
32+
{
33+
return [T_ATTRIBUTE];
34+
35+
}//end register()
36+
37+
38+
/**
39+
* Processes this test, when one of its tokens is encountered.
40+
*
41+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
42+
* token was found.
43+
* @param int $stackPtr The position in the PHP_CodeSniffer
44+
* file's token stack where the token
45+
* was found.
46+
*
47+
* @return void|int Optionally returns a stack pointer. The sniff will not be
48+
* called again on the current file until the returned stack
49+
* pointer is reached. Return $phpcsFile->numTokens + 1 to skip
50+
* the rest of the file.
51+
*/
52+
public function process(File $phpcsFile, $stackPtr)
53+
{
54+
$tokens = $phpcsFile->getTokens();
55+
$attributeName = $phpcsFile->findNext(T_STRING, ($stackPtr + 1));
56+
if ($attributeName !== false
57+
&& $tokens[$attributeName]['content'] === 'Hook'
58+
) {
59+
$hookName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($attributeName + 2));
60+
if ($hookName !== false) {
61+
// Remove outer quotes.
62+
$hookNameValue = trim($tokens[$hookName]['content'], '"\'');
63+
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) {
71+
// Return outer quotes.
72+
$hookNameValueFixed = str_replace($hookNameValue, $hookNameValueFixed, $tokens[$hookName]['content']);
73+
$phpcsFile->fixer->replaceToken($hookName, $hookNameValueFixed);
74+
}
75+
}
76+
}
77+
}//end if
78+
79+
}//end process()
80+
81+
82+
}//end class

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1 || ^1.0.0",
2020
"sirbrillig/phpcs-variable-analysis": "^2.11.7",
2121
"slevomat/coding-standard": "^8.11",
22-
"squizlabs/php_codesniffer": "^3.11.2",
22+
"squizlabs/php_codesniffer": "^3.11.3",
2323
"symfony/yaml": ">=3.4.0"
2424
},
2525
"autoload": {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains Test.
6+
*/
7+
8+
/**
9+
* Valid hook.
10+
*/
11+
#[Hook('valid')]
12+
function module_valid() {
13+
14+
}
15+
16+
/**
17+
* Single quotes.
18+
*/
19+
#[Hook('hook_info')]
20+
function module_info() {
21+
22+
}
23+
24+
/**
25+
* Double quotes.
26+
*/
27+
#[Hook("hook_node_load")]
28+
function module_node_load() {
29+
30+
}
31+
32+
/**
33+
* Not finished hook name. No warning
34+
*/
35+
#[Hook('hook_')]
36+
function module_system() {
37+
38+
}
39+
40+
/**
41+
* Attribute named argument.
42+
*/
43+
#[Hook(hook: 'hook_node_delete')]
44+
function module_node_delete() {
45+
46+
}
47+
48+
/**
49+
* Attribute named arguments.
50+
*/
51+
#[Hook(hook: 'hook_node_alter', module: 'custom_module')]
52+
function module_node_alter() {
53+
54+
}
55+
56+
/**
57+
* "hook" is a part of hook name.
58+
*/
59+
#[Hook('hook_piratehook_view')]
60+
function module_piratehook_view() {
61+
62+
}
63+
64+
/**
65+
* Implements hook_hookpirate_view().
66+
*
67+
* "hook" is a part of hook name.
68+
*/
69+
#[Hook('hook_hookpirate_view')]
70+
function module_hookpirate_view() {
71+
72+
}
73+
74+
/**
75+
* Valid hook.
76+
*/
77+
#[Hook('valid', 'validMethod', 'module')]
78+
class ValidHooks {
79+
80+
/**
81+
*
82+
*/
83+
public function validMethod() {
84+
85+
}
86+
87+
}
88+
89+
/**
90+
*
91+
*/
92+
#[Hook('hook_user_cancel', 'userCancel', 'custom')]
93+
class Hooks {
94+
95+
/**
96+
*
97+
*/
98+
public function userCancel() {
99+
100+
}
101+
102+
}
103+
104+
/**
105+
* Named arguments, double quotes.
106+
*/
107+
#[Hook(hook: "hook_user_login", method: "userLogin", module: "views")]
108+
class MyHooks {
109+
110+
/**
111+
*
112+
*/
113+
public function userLogin() {
114+
115+
}
116+
117+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains Test.
6+
*/
7+
8+
/**
9+
* Valid hook.
10+
*/
11+
#[Hook('valid')]
12+
function module_valid() {
13+
14+
}
15+
16+
/**
17+
* Single quotes.
18+
*/
19+
#[Hook('info')]
20+
function module_info() {
21+
22+
}
23+
24+
/**
25+
* Double quotes.
26+
*/
27+
#[Hook("node_load")]
28+
function module_node_load() {
29+
30+
}
31+
32+
/**
33+
* Not finished hook name. No warning.
34+
*/
35+
#[Hook('hook_')]
36+
function module_system() {
37+
38+
}
39+
40+
/**
41+
* Attribute named argument.
42+
*/
43+
#[Hook(hook: 'node_delete')]
44+
function module_node_delete() {
45+
46+
}
47+
48+
/**
49+
* Attribute named arguments.
50+
*/
51+
#[Hook(hook: 'node_alter', module: 'custom_module')]
52+
function module_node_alter() {
53+
54+
}
55+
56+
/**
57+
* "hook" is a part of hook name.
58+
*/
59+
#[Hook('piratehook_view')]
60+
function module_piratehook_view() {
61+
62+
}
63+
64+
/**
65+
* Implements hook_hookpirate_view().
66+
*
67+
* "hook" is a part of hook name.
68+
*/
69+
#[Hook('hookpirate_view')]
70+
function module_hookpirate_view() {
71+
72+
}
73+
74+
/**
75+
* Valid hook.
76+
*/
77+
#[Hook('valid', 'validMethod', 'module')]
78+
class ValidHooks {
79+
80+
/**
81+
*
82+
*/
83+
public function validMethod() {
84+
85+
}
86+
87+
}
88+
89+
/**
90+
*
91+
*/
92+
#[Hook('user_cancel', 'userCancel', 'custom')]
93+
class Hooks {
94+
95+
/**
96+
*
97+
*/
98+
public function userCancel() {
99+
100+
}
101+
102+
}
103+
104+
/**
105+
* Named arguments, double quotes.
106+
*/
107+
#[Hook(hook: "user_login", method: "userLogin", module: "views")]
108+
class MyHooks {
109+
110+
/**
111+
*
112+
*/
113+
public function userLogin() {
114+
115+
}
116+
117+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Drupal\Test\Attributes;
4+
5+
use Drupal\Test\CoderSniffUnitTest;
6+
7+
class ValidHookNameUnitTest extends CoderSniffUnitTest
8+
{
9+
10+
11+
/**
12+
* Returns the lines where errors should occur.
13+
*
14+
* The key of the array should represent the line number and the value
15+
* should represent the number of errors that should occur on that line.
16+
*
17+
* @param string $testFile The name of the file being tested.
18+
*
19+
* @return array<int, int>
20+
*/
21+
protected function getErrorList(string $testFile): array
22+
{
23+
return [];
24+
25+
}//end getErrorList()
26+
27+
28+
/**
29+
* Returns the lines where warnings should occur.
30+
*
31+
* The key of the array should represent the line number and the value
32+
* should represent the number of warnings that should occur on that line.
33+
*
34+
* @param string $testFile The name of the file being tested.
35+
*
36+
* @return array<int, int>
37+
*/
38+
protected function getWarningList(string $testFile): array
39+
{
40+
return [
41+
19 => 1,
42+
27 => 1,
43+
43 => 1,
44+
51 => 1,
45+
59 => 1,
46+
69 => 1,
47+
92 => 1,
48+
107 => 1,
49+
];
50+
51+
}//end getWarningList()
52+
53+
54+
}//end class

0 commit comments

Comments
 (0)