Skip to content

Commit 387cd37

Browse files
committed
Attributes should be processed by FQN
1 parent 076e8db commit 387cd37

File tree

10 files changed

+50
-21
lines changed

10 files changed

+50
-21
lines changed

SlevomatCodingStandard/Helpers/Attribute.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@ class Attribute
1212

1313
private string $name;
1414

15+
private string $fullyQualifiedName;
16+
1517
private int $startPointer;
1618

1719
private int $endPointer;
1820

1921
private ?string $content = null;
2022

21-
public function __construct(int $attributePointer, string $name, int $startPointer, int $endPointer, ?string $content = null)
23+
public function __construct(
24+
int $attributePointer,
25+
string $name,
26+
string $fullyQualifiedName,
27+
int $startPointer,
28+
int $endPointer,
29+
?string $content = null
30+
)
2231
{
2332
$this->attributePointer = $attributePointer;
2433
$this->name = $name;
34+
$this->fullyQualifiedName = $fullyQualifiedName;
2535
$this->startPointer = $startPointer;
2636
$this->endPointer = $endPointer;
2737
$this->content = $content;
@@ -37,6 +47,11 @@ public function getName(): string
3747
return $this->name;
3848
}
3949

50+
public function getFullyQualifiedName(): string
51+
{
52+
return $this->fullyQualifiedName;
53+
}
54+
4055
public function getStartPointer(): int
4156
{
4257
return $this->startPointer;

SlevomatCodingStandard/Helpers/AttributeHelper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public static function getAttributes(File $phpcsFile, int $attributeOpenerPointe
7878
$attributes[] = new Attribute(
7979
$attributeOpenerPointer,
8080
$attributeName,
81+
NamespaceHelper::resolveClassName($phpcsFile, $attributeName, $attributeOpenerPointer),
8182
$attributeNameStartPointer,
8283
$attributeNameEndPointer,
8384
);
@@ -88,6 +89,7 @@ public static function getAttributes(File $phpcsFile, int $attributeOpenerPointe
8889
$attributes[] = new Attribute(
8990
$attributeOpenerPointer,
9091
$attributeName,
92+
NamespaceHelper::resolveClassName($phpcsFile, $attributeName, $attributeOpenerPointer),
9193
$attributeNameStartPointer,
9294
$attributeNameEndPointer,
9395
);
@@ -99,6 +101,7 @@ public static function getAttributes(File $phpcsFile, int $attributeOpenerPointe
99101
$attributes[] = new Attribute(
100102
$attributeOpenerPointer,
101103
$attributeName,
104+
NamespaceHelper::resolveClassName($phpcsFile, $attributeName, $attributeOpenerPointer),
102105
$attributeNameStartPointer,
103106
$tokens[$pointerAfterAttributeName]['parenthesis_closer'],
104107
TokenHelper::getContent(

SlevomatCodingStandard/Sniffs/Attributes/AttributesOrderSniff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ public function process(File $phpcsFile, $attributeOpenerPointer): void
9292
uasort(
9393
$expectedOrder,
9494
static fn (array $attributesGroup1, array $attributesGroup2): int => strnatcmp(
95-
$attributesGroup1[0]->getName(),
96-
$attributesGroup2[0]->getName(),
95+
$attributesGroup1[0]->getFullyQualifiedName(),
96+
$attributesGroup2[0]->getFullyQualifiedName(),
9797
),
9898
);
9999

100100
} else {
101101
$actualOrder = [];
102102

103103
foreach ($attributesGroups as $attributesGroupNo => $attributesGroup) {
104-
$attributeName = $this->normalizeAttributeName($attributesGroup[0]->getName());
104+
$attributeName = $this->normalizeAttributeName($attributesGroup[0]->getFullyQualifiedName());
105105

106106
foreach ($this->order as $orderPosition => $attributeNameOnPosition) {
107107
if (

SlevomatCodingStandard/Sniffs/TypeHints/DisallowMixedTypeHintSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ private function targetHasOverrideAttribute(File $phpcsFile, int $docCommentOpen
8888
}
8989

9090
$attributeNames = array_map(
91-
static fn (Attribute $name): string => $name->getName(),
91+
static fn (Attribute $name): string => $name->getFullyQualifiedName(),
9292
AttributeHelper::getAttributes($phpcsFile, $nextPointer),
9393
);
9494

95-
return in_array('Override', $attributeNames, true) || in_array('\Override', $attributeNames, true);
95+
return in_array('\Override', $attributeNames, true);
9696
}
9797

9898
}

tests/Helpers/AttributeHelperTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@ public function testGetAttributesInsideAttributeTags(): void
1818
$attributes = AttributeHelper::getAttributes($phpcsFile, $firstAttributePointer);
1919

2020
$expected = [
21-
4 => ['Attribute1', null],
22-
7 => ['\\FQN\\Attribute2', "('var')"],
23-
17 => ['Attribute3', "(option: PDO::class, option2: true, option3: 'False')"],
24-
39 => ['\\Attribute4', '()'],
25-
44 => ['Attribute5', null],
21+
['Attribute1', '\\Attribute1', null],
22+
['\\FQN\\Attribute2', '\\FQN\\Attribute2', "('var')"],
23+
['Attribute3', '\\Attribute3', "(option: PDO::class, option2: true, option3: 'False')"],
24+
['\\Attribute4', '\\Attribute4', '()'],
25+
['Attribute5', '\\Attribute5', null],
26+
['Attribute6', '\\FQN\\Attribute6', null],
2627
];
2728

2829
self::assertCount(count($expected), $attributes);
2930

30-
foreach ($attributes as $attribute) {
31-
self::assertSame(3, $attribute->getAttributePointer());
32-
self::assertArrayHasKey($attribute->getStartPointer(), $expected);
33-
self::assertSame($expected[$attribute->getStartPointer()][0], $attribute->getName());
34-
self::assertSame($expected[$attribute->getStartPointer()][1], $attribute->getContent());
31+
foreach ($attributes as $attributeNo => $attribute) {
32+
self::assertSame(11, $attribute->getAttributePointer());
33+
self::assertSame($expected[$attributeNo][0], $attribute->getName());
34+
self::assertSame($expected[$attributeNo][1], $attribute->getFullyQualifiedName());
35+
self::assertSame($expected[$attributeNo][2], $attribute->getContent());
3536
}
3637
}
3738

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php // lint >= 8.0
22

3+
use FQN\Attribute6;
4+
35
#[Attribute1, \FQN\Attribute2('var'),
4-
Attribute3(option: PDO::class, option2: true, option3: 'False'),\Attribute4(),Attribute5]
6+
Attribute3(option: PDO::class, option2: true, option3: 'False'),\Attribute4(),Attribute5,Attribute6]
57
class FooClass
68
{
79
}

tests/Sniffs/Attributes/AttributesOrderSniffTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public function testErrors(): void
4040

4141
self::assertSame(3, $report->getErrorCount());
4242

43-
self::assertSniffError($report, 3, AttributesOrderSniff::CODE_INCORRECT_ORDER);
44-
self::assertSniffError($report, 14, AttributesOrderSniff::CODE_INCORRECT_ORDER);
45-
self::assertSniffError($report, 19, AttributesOrderSniff::CODE_INCORRECT_ORDER);
43+
self::assertSniffError($report, 5, AttributesOrderSniff::CODE_INCORRECT_ORDER);
44+
self::assertSniffError($report, 16, AttributesOrderSniff::CODE_INCORRECT_ORDER);
45+
self::assertSniffError($report, 21, AttributesOrderSniff::CODE_INCORRECT_ORDER);
4646

4747
self::assertAllFixedInFile($report);
4848
}

tests/Sniffs/Attributes/data/attributesOrderErrors.fixed.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Group\Unknown;
4+
35
#[Attribute1]
46
#[Attribute2]
57
#[\Group\Attribute1]
@@ -20,6 +22,7 @@ public function method()
2022
#[Attribute2]
2123
#[\Group\Attribute1]
2224
#[\Group\Attribute2]
25+
#[Unknown]
2326
#[\Group\Unknown]
2427
#[AppAssertB\SomeAssert]
2528
#[AppAssertA\SomeAssert]

tests/Sniffs/Attributes/data/attributesOrderErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Group\Unknown;
4+
35
#[UnknownOrder]
46
#[\Group\Unknown]
57
#[\Group\Attribute2]
@@ -16,6 +18,7 @@ public function method()
1618
{
1719
}
1820

21+
#[Unknown]
1922
#[UnknownOrder] #[\Group\Unknown] #[AppAssertB\SomeAssert]
2023
#[\Group\Attribute2] #[Attribute2] #[\Group\Attribute1]
2124
#[Attribute1] #[AppAssertA\SomeAssert]

tests/Sniffs/Attributes/data/attributesOrderNoErrors.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Group\Unknown;
4+
35
#[Attribute1]
46
#[Attribute2]
57
#[\Group\Attribute1]
@@ -12,7 +14,7 @@ class Whatever
1214
{
1315

1416
#[Attribute1] #[Attribute2]
15-
#[\Group\Attribute1] #[\Group\Attribute2] #[\Group\Unknown]
17+
#[\Group\Attribute1] #[\Group\Attribute2] #[\Group\Unknown] #[Unknown]
1618
#[AppAssertA\SomeAssert] #[AppAssertB\SomeAssert]
1719
#[UnknownOrder]
1820
public function method()

0 commit comments

Comments
 (0)