Skip to content

Commit

Permalink
fix(FullyQualifiedNamespace): Fix detection of class names at the beg…
Browse files Browse the repository at this point in the history
…inning of attributes (#3483583)
  • Loading branch information
klausi authored Jan 3, 2025
1 parent b7fc828 commit bba74b3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function process(File $phpcsFile, $stackPtr)
}

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

Expand Down
22 changes: 22 additions & 0 deletions tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.4.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Drupal\action_link_test_plugins\Plugin\StateAction;

use Drupal\action_link\Entity\ActionLinkInterface;
use Drupal\action_link\Plugin\StateAction\StateActionBase;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;

/**
* Test action which is always usable.
*/
#[\Drupal\action_link\Attribute\StateAction(
id: 'test_always',
label: new \Drupal\Core\StringTranslation\TranslatableMarkup('Test Always'),
description: new \Drupal\Core\StringTranslation\TranslatableMarkup('Test Always'),
directions: [
'change' => 'change',
]
)]
class TestAlways extends StateActionBase {
}
21 changes: 21 additions & 0 deletions tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.4.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Drupal\action_link_test_plugins\Plugin\StateAction;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\action_link\Attribute\StateAction;
use Drupal\action_link\Plugin\StateAction\StateActionBase;

/**
* Test action which is always usable.
*/
#[StateAction(
id: 'test_always',
label: new TranslatableMarkup('Test Always'),
description: new TranslatableMarkup('Test Always'),
directions: [
'change' => 'change',
]
)]
class TestAlways extends StateActionBase {
}
9 changes: 8 additions & 1 deletion tests/Drupal/Classes/FullyQualifiedNamespaceUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ protected function getErrorList(string $testFile): array
return [8 => 1];
case 'FullyQualifiedNamespaceUnitTest.3.inc':
return [10 => 2];
}
case 'FullyQualifiedNamespaceUnitTest.4.inc':
return [
13 => 1,
15 => 1,
16 => 1,
];
}//end switch

return [];

Expand Down Expand Up @@ -75,6 +81,7 @@ protected function getTestFiles($testFileBase): array
__DIR__.'/FullyQualifiedNamespaceUnitTest.1.inc',
__DIR__.'/FullyQualifiedNamespaceUnitTest.2.inc',
__DIR__.'/FullyQualifiedNamespaceUnitTest.3.inc',
__DIR__.'/FullyQualifiedNamespaceUnitTest.4.inc',
__DIR__.'/FullyQualifiedNamespaceUnitTest.api.php',
];

Expand Down
4 changes: 3 additions & 1 deletion tests/Drupal/Commenting/ClassCommentUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Testing class/trait comments.
*/

use Some\Attribute;

/**
*
*/
Expand Down Expand Up @@ -60,7 +62,7 @@ class WrongSpacing {
/**
* This is correct.
*/
#[Some\Attribute(foo: 'bar')]
#[Attribute(foo: 'bar')]
#[Other\Attribute(baz: 'qux')]
class DoubleAttribute {

Expand Down
4 changes: 3 additions & 1 deletion tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Some function comment tests.
*/

use Some\Attribute;

/**
* Test.
*
Expand Down Expand Up @@ -579,7 +581,7 @@ class Test41 {
/**
* Method docblock.
*/
#[Some\Attribute(foo: 'bar')]
#[Attribute(foo: 'bar')]
#[Other\Attribute(baz: 'qux')]
public function method() {
}
Expand Down

0 comments on commit bba74b3

Please sign in to comment.