Skip to content

Commit 05c815d

Browse files
authored
Merge pull request #298 from simPod/attributes
Add attribute sniffs
2 parents f2488f5 + 0801e1b commit 05c815d

File tree

9 files changed

+104
-32
lines changed

9 files changed

+104
-32
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"require": {
2424
"php": "^7.2 || ^8.0",
2525
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7",
26-
"slevomat/coding-standard": "^8.5.1",
26+
"slevomat/coding-standard": "^8.6",
2727
"squizlabs/php_codesniffer": "^3.7"
2828
},
2929
"config": {

lib/Doctrine/ruleset.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@
110110
<rule ref="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed">
111111
<type>error</type>
112112
</rule>
113+
<!-- Disallow multiple attributes inside one #[] -->
114+
<rule ref="SlevomatCodingStandard.Attributes.DisallowAttributesJoining"/>
115+
<!-- Require only one attribute per line -->
116+
<rule ref="SlevomatCodingStandard.Attributes.DisallowMultipleAttributesPerLine"/>
117+
<!-- Attributes should be adjoined to the PHP element they belong to -->
118+
<rule ref="SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing"/>
119+
<!-- Require PHPDoc always before attributes -->
120+
<rule ref="SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment"/>
113121
<!-- Require that single line arrays have the correct spacing: no space around brackets and one space after comma -->
114122
<rule ref="SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace"/>
115123
<!-- Require comma after last element in multi-line array -->

tests/expected_report.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ FILE ERRORS WARNINGS
66
tests/input/array_indentation.php 10 0
77
tests/input/arrow-functions-format.php 10 0
88
tests/input/assignment-operators.php 4 0
9+
tests/input/attributes.php 15 0
910
tests/input/binary_operators.php 9 0
1011
tests/input/class-references.php 10 0
1112
tests/input/ClassPropertySpacing.php 2 0
@@ -51,9 +52,9 @@ tests/input/use-ordering.php 1 0
5152
tests/input/useless-semicolon.php 2 0
5253
tests/input/UselessConditions.php 21 0
5354
----------------------------------------------------------------------
54-
A TOTAL OF 437 ERRORS AND 0 WARNINGS WERE FOUND IN 47 FILES
55+
A TOTAL OF 452 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
5556
----------------------------------------------------------------------
56-
PHPCBF CAN FIX 370 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
57+
PHPCBF CAN FIX 374 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5758
----------------------------------------------------------------------
5859

5960

tests/fixed/attributes.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
#[Attribute1]
6+
#[Attribute2]
7+
#[Attribute3]
8+
class TestClass
9+
{
10+
}
11+
12+
#[Attribute1]
13+
#[Attribute2]
14+
#[Attribute3]
15+
class TestClass2
16+
{
17+
}
18+
19+
#[Attribute1]
20+
#[Attribute2]
21+
#[Attribute3]
22+
class TestClass3
23+
{
24+
}
25+
26+
/** @internal */
27+
#[Attribute1]
28+
#[Attribute2]
29+
#[Attribute3]
30+
class TestClass4
31+
{
32+
}

tests/input/attributes.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
#[Attribute1] #[Attribute2]
6+
#[Attribute3]
7+
class TestClass
8+
{
9+
}
10+
11+
#[Attribute1,Attribute2]
12+
#[Attribute3]
13+
class TestClass2
14+
{
15+
}
16+
17+
#[Attribute1]
18+
#[Attribute2]
19+
#[Attribute3]
20+
21+
class TestClass3
22+
{
23+
}
24+
25+
#[Attribute1]
26+
#[Attribute2]
27+
#[Attribute3]
28+
/** @internal */
29+
class TestClass4
30+
{
31+
}

tests/php72-compatibility.patch

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index 151bce4..0afca8c 100644
2+
index 9e34caa..ea3b611 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
5-
@@ -14,26 +14,23 @@ tests/input/constants-var.php 7 0
5+
@@ -16,26 +16,23 @@ tests/input/constants-var.php 7 0
66
tests/input/ControlStructures.php 28 0
77
tests/input/doc-comment-spacing.php 11 0
88
tests/input/duplicate-assignment-variable.php 1 0
@@ -37,7 +37,7 @@ index 151bce4..0afca8c 100644
3737
tests/input/semicolon_spacing.php 3 0
3838
tests/input/single-line-array-spacing.php 5 0
3939
tests/input/spread-operator.php 6 0
40-
@@ -42,17 +39,16 @@ tests/input/strings.php 1 0
40+
@@ -44,17 +41,16 @@ tests/input/strings.php 1 0
4141
tests/input/superfluous-naming.php 11 0
4242
tests/input/test-case.php 8 0
4343
tests/input/trailing_comma_on_array.php 1 0
@@ -51,11 +51,11 @@ index 151bce4..0afca8c 100644
5151
-tests/input/UselessConditions.php 21 0
5252
+tests/input/UselessConditions.php 20 0
5353
----------------------------------------------------------------------
54-
-A TOTAL OF 437 ERRORS AND 0 WARNINGS WERE FOUND IN 47 FILES
55-
+A TOTAL OF 390 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
54+
-A TOTAL OF 452 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
55+
+A TOTAL OF 405 ERRORS AND 0 WARNINGS WERE FOUND IN 44 FILES
5656
----------------------------------------------------------------------
57-
-PHPCBF CAN FIX 370 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
58-
+PHPCBF CAN FIX 323 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
57+
-PHPCBF CAN FIX 374 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
58+
+PHPCBF CAN FIX 327 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5959
----------------------------------------------------------------------
6060

6161

tests/php73-compatibility.patch

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index 151bce4..94718eb 100644
2+
index 9e34caa..c5062cb 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
5-
@@ -14,26 +14,23 @@ tests/input/constants-var.php 7 0
5+
@@ -16,26 +16,23 @@ tests/input/constants-var.php 7 0
66
tests/input/ControlStructures.php 28 0
77
tests/input/doc-comment-spacing.php 11 0
88
tests/input/duplicate-assignment-variable.php 1 0
@@ -37,7 +37,7 @@ index 151bce4..94718eb 100644
3737
tests/input/semicolon_spacing.php 3 0
3838
tests/input/single-line-array-spacing.php 5 0
3939
tests/input/spread-operator.php 6 0
40-
@@ -42,17 +39,17 @@ tests/input/strings.php 1 0
40+
@@ -44,17 +41,17 @@ tests/input/strings.php 1 0
4141
tests/input/superfluous-naming.php 11 0
4242
tests/input/test-case.php 8 0
4343
tests/input/trailing_comma_on_array.php 1 0
@@ -52,11 +52,11 @@ index 151bce4..94718eb 100644
5252
-tests/input/UselessConditions.php 21 0
5353
+tests/input/UselessConditions.php 20 0
5454
----------------------------------------------------------------------
55-
-A TOTAL OF 437 ERRORS AND 0 WARNINGS WERE FOUND IN 47 FILES
56-
+A TOTAL OF 392 ERRORS AND 0 WARNINGS WERE FOUND IN 44 FILES
55+
-A TOTAL OF 452 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
56+
+A TOTAL OF 407 ERRORS AND 0 WARNINGS WERE FOUND IN 45 FILES
5757
----------------------------------------------------------------------
58-
-PHPCBF CAN FIX 370 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
59-
+PHPCBF CAN FIX 325 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
58+
-PHPCBF CAN FIX 374 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
59+
+PHPCBF CAN FIX 329 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6060
----------------------------------------------------------------------
6161

6262

tests/php74-compatibility.patch

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index 151bce4..08a098f 100644
2+
index 9e34caa..ed67841 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
5-
@@ -14,26 +14,23 @@ tests/input/constants-var.php 7 0
5+
@@ -16,26 +16,23 @@ tests/input/constants-var.php 7 0
66
tests/input/ControlStructures.php 28 0
77
tests/input/doc-comment-spacing.php 11 0
88
tests/input/duplicate-assignment-variable.php 1 0
@@ -35,7 +35,7 @@ index 151bce4..08a098f 100644
3535
tests/input/semicolon_spacing.php 3 0
3636
tests/input/single-line-array-spacing.php 5 0
3737
tests/input/spread-operator.php 6 0
38-
@@ -42,17 +39,17 @@ tests/input/strings.php 1 0
38+
@@ -44,17 +41,17 @@ tests/input/strings.php 1 0
3939
tests/input/superfluous-naming.php 11 0
4040
tests/input/test-case.php 8 0
4141
tests/input/trailing_comma_on_array.php 1 0
@@ -50,11 +50,11 @@ index 151bce4..08a098f 100644
5050
-tests/input/UselessConditions.php 21 0
5151
+tests/input/UselessConditions.php 20 0
5252
----------------------------------------------------------------------
53-
-A TOTAL OF 437 ERRORS AND 0 WARNINGS WERE FOUND IN 47 FILES
54-
+A TOTAL OF 401 ERRORS AND 0 WARNINGS WERE FOUND IN 44 FILES
53+
-A TOTAL OF 452 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
54+
+A TOTAL OF 416 ERRORS AND 0 WARNINGS WERE FOUND IN 45 FILES
5555
----------------------------------------------------------------------
56-
-PHPCBF CAN FIX 370 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
57-
+PHPCBF CAN FIX 334 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
56+
-PHPCBF CAN FIX 374 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
57+
+PHPCBF CAN FIX 338 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5858
----------------------------------------------------------------------
5959

6060

tests/php80-compatibility.patch

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index 151bce4..873691d 100644
2+
index 9e34caa..71022c4 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
5-
@@ -16,7 +16,6 @@ tests/input/doc-comment-spacing.php 11 0
5+
@@ -18,7 +18,6 @@ tests/input/doc-comment-spacing.php 11 0
66
tests/input/duplicate-assignment-variable.php 1 0
77
tests/input/EarlyReturn.php 7 0
88
tests/input/example-class.php 47 0
99
-tests/input/ExampleBackedEnum.php 3 0
1010
tests/input/Exceptions.php 1 0
1111
tests/input/forbidden-comments.php 14 0
1212
tests/input/forbidden-functions.php 6 0
13-
@@ -31,7 +30,7 @@ tests/input/null_coalesce_equal_operator.php 5 0
13+
@@ -33,7 +32,7 @@ tests/input/null_coalesce_equal_operator.php 5 0
1414
tests/input/null_coalesce_operator.php 3 0
1515
tests/input/null_safe_operator.php 1 0
1616
tests/input/optimized-functions.php 1 0
@@ -19,15 +19,15 @@ index 151bce4..873691d 100644
1919
tests/input/return_type_on_closures.php 26 0
2020
tests/input/return_type_on_methods.php 22 0
2121
tests/input/semicolon_spacing.php 3 0
22-
@@ -50,9 +49,9 @@ tests/input/use-ordering.php 1 0
22+
@@ -52,9 +51,9 @@ tests/input/use-ordering.php 1 0
2323
tests/input/useless-semicolon.php 2 0
2424
tests/input/UselessConditions.php 21 0
2525
----------------------------------------------------------------------
26-
-A TOTAL OF 437 ERRORS AND 0 WARNINGS WERE FOUND IN 47 FILES
27-
+A TOTAL OF 431 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
26+
-A TOTAL OF 452 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
27+
+A TOTAL OF 446 ERRORS AND 0 WARNINGS WERE FOUND IN 47 FILES
2828
----------------------------------------------------------------------
29-
-PHPCBF CAN FIX 370 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
30-
+PHPCBF CAN FIX 364 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
29+
-PHPCBF CAN FIX 374 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
30+
+PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
3131
----------------------------------------------------------------------
3232

3333

0 commit comments

Comments
 (0)