Skip to content

Commit e1ca9fd

Browse files
committed
Merge remote-tracking branch 'origin/8.3.x' into class-name
2 parents d40ade4 + 78034f0 commit e1ca9fd

17 files changed

+503
-33
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 --exclude=Drupal.Commenting.FunctionComment --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,modules/views/src/Plugin/views/pager/PagerPluginBase.php,lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderInterface.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

coder_sniffer/Drupal/Sniffs/Classes/FullyQualifiedNamespaceSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public function process(File $phpcsFile, $stackPtr)
6565
return;
6666
}
6767

68+
// Skip names in PHP attributes, no standards defined yet.
69+
if (isset($tokens[$stackPtr]['attribute_closer']) === true) {
70+
return $tokens[$stackPtr]['attribute_closer'];
71+
}
72+
6873
// Check if this is a use statement and ignore those.
6974
$before = $phpcsFile->findPrevious([T_STRING, T_NS_SEPARATOR, T_WHITESPACE, T_COMMA, T_AS], $stackPtr, null, true);
7075
if ($tokens[$before]['code'] === T_USE || $tokens[$before]['code'] === T_NAMESPACE) {

coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public function process(File $phpcsFile, $stackPtr)
6363
$name = $tokens[$stackPtr]['content'];
6464
$classCodeStart = $stackPtr;
6565

66-
$previousContent = null;
6766
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
6867
if (isset($find[$tokens[$commentEnd]['code']]) === true) {
6968
if (isset(Tokens::$phpcsCommentTokens[$tokens[$commentEnd]['code']]) === true) {
@@ -73,10 +72,6 @@ public function process(File $phpcsFile, $stackPtr)
7372
continue;
7473
}
7574

76-
if ($previousContent === null) {
77-
$previousContent = $commentEnd;
78-
}
79-
8075
if ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END
8176
&& isset($tokens[$commentEnd]['attribute_opener']) === true
8277
) {

coder_sniffer/Drupal/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Tokens;
1415

1516
/**
1617
* Ensures doc blocks follow basic formatting.
@@ -50,9 +51,18 @@ public function register()
5051
*/
5152
public function process(File $phpcsFile, $stackPtr)
5253
{
53-
$tokens = $phpcsFile->getTokens();
54-
$commentEnd = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr + 1));
55-
$commentStart = $tokens[$commentEnd]['comment_opener'];
54+
$tokens = $phpcsFile->getTokens();
55+
56+
if (isset($tokens[$stackPtr]['comment_closer']) === false
57+
|| ($tokens[$tokens[$stackPtr]['comment_closer']]['content'] === ''
58+
&& $tokens[$stackPtr]['comment_closer'] === ($phpcsFile->numTokens - 1))
59+
) {
60+
// Don't process an unfinished comment during live coding.
61+
return;
62+
}
63+
64+
$commentStart = $stackPtr;
65+
$commentEnd = $tokens[$stackPtr]['comment_closer'];
5666

5767
$empty = [
5868
T_DOC_COMMENT_WHITESPACE,
@@ -463,15 +473,21 @@ public function process(File $phpcsFile, $stackPtr)
463473
// Check for a value. No value means no padding needed.
464474
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
465475
if ($string !== false && $tokens[$string]['line'] === $tokens[$tag]['line']) {
466-
$paddings[$tag] = strlen($tokens[($tag + 1)]['content']);
476+
$paddings[$tag] = $tokens[($tag + 1)]['length'];
467477
}
468478
}
469479

470480
// Check that there was single blank line after the tag block
471-
// but account for a multi-line tag comments.
481+
// but account for multi-line tag comments.
482+
$find = Tokens::$phpcsCommentTokens;
483+
$find[T_DOC_COMMENT_TAG] = T_DOC_COMMENT_TAG;
484+
472485
$lastTag = $group[$pos];
473-
$next = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($lastTag + 3), $commentEnd);
474-
if ($next !== false && $tokens[$next]['column'] === $tokens[$firstTag]['column']) {
486+
$next = $phpcsFile->findNext($find, ($lastTag + 3), $commentEnd);
487+
if ($next !== false
488+
&& $tokens[$next]['column'] === $tokens[$firstTag]['column']
489+
&& in_array($tokens[$lastTag]['content'], $checkTags) === true
490+
) {
475491
$prev = $phpcsFile->findPrevious([T_DOC_COMMENT_TAG, T_DOC_COMMENT_STRING], ($next - 1), $commentStart);
476492
if ($tokens[$next]['line'] !== ($tokens[$prev]['line'] + 2)) {
477493
$error = 'There must be a single blank line after a tag group';

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,16 @@ public function register()
7575
public function process(File $phpcsFile, $stackPtr)
7676
{
7777
$tokens = $phpcsFile->getTokens();
78-
$ignore = Tokens::$methodPrefixes;
78+
$ignore = (Tokens::$methodPrefixes + Tokens::$phpcsCommentTokens);
7979
$ignore[T_WHITESPACE] = T_WHITESPACE;
8080
$functionCodeStart = $stackPtr;
8181

8282
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
8383
if (isset($ignore[$tokens[$commentEnd]['code']]) === true) {
84+
if (isset(Tokens::$phpcsCommentTokens[$tokens[$commentEnd]['code']]) === true) {
85+
$functionCodeStart = $commentEnd;
86+
}
87+
8488
continue;
8589
}
8690

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+
}

0 commit comments

Comments
 (0)