Skip to content

Commit 582503f

Browse files
committed
fix(FullyQualifiedNamespace): Fix detection of class names at the beginning of attributes
1 parent b7fc828 commit 582503f

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

coder_sniffer/Drupal/Sniffs/Classes/FullyQualifiedNamespaceSniff.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function process(File $phpcsFile, $stackPtr)
6060
}
6161

6262
// We are only interested in a backslash embedded between strings, which
63-
// means this is a class reference with more than once namespace part.
63+
// means this is a class reference with more than one namespace part.
6464
if ($tokens[($stackPtr - 1)]['code'] !== T_STRING || $tokens[($stackPtr + 1)]['code'] !== T_STRING) {
6565
return;
6666
}
@@ -76,7 +76,10 @@ public function process(File $phpcsFile, $stackPtr)
7676
// If this is a namespaced function call then ignore this because use
7777
// statements for functions are not possible in PHP 5.5 and lower.
7878
$after = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR, T_WHITESPACE], $stackPtr, null, true);
79-
if ($tokens[$after]['code'] === T_OPEN_PARENTHESIS && $tokens[$before]['code'] !== T_NEW) {
79+
if ($tokens[$after]['code'] === T_OPEN_PARENTHESIS
80+
&& $tokens[$before]['code'] !== T_NEW
81+
&& $tokens[$before]['code'] !== T_ATTRIBUTE
82+
) {
8083
return ($after + 1);
8184
}
8285

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Drupal\action_link_test_plugins\Plugin\StateAction;
4+
5+
use Drupal\action_link\Entity\ActionLinkInterface;
6+
use Drupal\action_link\Plugin\StateAction\StateActionBase;
7+
use Drupal\Core\Access\AccessResult;
8+
use Drupal\Core\Session\AccountInterface;
9+
10+
/**
11+
* Test action which is always usable.
12+
*/
13+
#[\Drupal\action_link\Attribute\StateAction(
14+
id: 'test_always',
15+
label: new \Drupal\Core\StringTranslation\TranslatableMarkup('Test Always'),
16+
description: new \Drupal\Core\StringTranslation\TranslatableMarkup('Test Always'),
17+
directions: [
18+
'change' => 'change',
19+
]
20+
)]
21+
class TestAlways extends StateActionBase {
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Drupal\action_link_test_plugins\Plugin\StateAction;
4+
5+
use Drupal\Core\StringTranslation\TranslatableMarkup;
6+
use Drupal\action_link\Attribute\StateAction;
7+
use Drupal\action_link\Plugin\StateAction\StateActionBase;
8+
9+
/**
10+
* Test action which is always usable.
11+
*/
12+
#[StateAction(
13+
id: 'test_always',
14+
label: new TranslatableMarkup('Test Always'),
15+
description: new TranslatableMarkup('Test Always'),
16+
directions: [
17+
'change' => 'change',
18+
]
19+
)]
20+
class TestAlways extends StateActionBase {
21+
}

tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ protected function getErrorList(string $testFile): array
3737
return [8 => 1];
3838
case 'FullyQualifiedNamespaceUnitTest.3.inc':
3939
return [10 => 2];
40+
case 'FullyQualifiedNamespaceUnitTest.4.inc':
41+
return [
42+
13 => 1,
43+
15 => 1,
44+
16 => 1,
45+
];
4046
}
4147

4248
return [];
@@ -75,6 +81,7 @@ protected function getTestFiles($testFileBase): array
7581
__DIR__.'/FullyQualifiedNamespaceUnitTest.1.inc',
7682
__DIR__.'/FullyQualifiedNamespaceUnitTest.2.inc',
7783
__DIR__.'/FullyQualifiedNamespaceUnitTest.3.inc',
84+
__DIR__.'/FullyQualifiedNamespaceUnitTest.4.inc',
7885
__DIR__.'/FullyQualifiedNamespaceUnitTest.api.php',
7986
];
8087

tests/Drupal/Commenting/ClassCommentUnitTest.inc.fixed

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Testing class/trait comments.
66
*/
77

8+
use Some\Attribute;
9+
810
/**
911
*
1012
*/
@@ -60,7 +62,7 @@ class WrongSpacing {
6062
/**
6163
* This is correct.
6264
*/
63-
#[Some\Attribute(foo: 'bar')]
65+
#[Attribute(foo: 'bar')]
6466
#[Other\Attribute(baz: 'qux')]
6567
class DoubleAttribute {
6668

tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Some function comment tests.
66
*/
77

8+
use Some\Attribute;
9+
810
/**
911
* Test.
1012
*
@@ -579,7 +581,7 @@ class Test41 {
579581
/**
580582
* Method docblock.
581583
*/
582-
#[Some\Attribute(foo: 'bar')]
584+
#[Attribute(foo: 'bar')]
583585
#[Other\Attribute(baz: 'qux')]
584586
public function method() {
585587
}

0 commit comments

Comments
 (0)