Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attribute sniffs #296

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require": {
"php": "^7.2 || ^8.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7",
"slevomat/coding-standard": "^8.5.1",
"slevomat/coding-standard": "^8.6",
"squizlabs/php_codesniffer": "^3.7"
},
"config": {
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@
<rule ref="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed">
<type>error</type>
</rule>
<!-- Disallow multiple attributes inside one #[] -->
<rule ref="SlevomatCodingStandard.Attributes.DisallowAttributesJoining"/>
<!-- Require only one attribute per line -->
<rule ref="SlevomatCodingStandard.Attributes.DisallowMultipleAttributesPerLine"/>
<!-- Attributes should be adjoined to the PHP element they belong to -->
<rule ref="SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing"/>
<!-- Require PHPDoc always before attributes -->
<rule ref="SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment"/>
<!-- Require that single line arrays have the correct spacing: no space around brackets and one space after comma -->
<rule ref="SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace"/>
<!-- Require comma after last element in multi-line array -->
Expand Down
5 changes: 3 additions & 2 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ FILE ERRORS WARNINGS
tests/input/array_indentation.php 10 0
tests/input/arrow-functions-format.php 10 0
tests/input/assignment-operators.php 4 0
tests/input/attributes.php 15 0
tests/input/binary_operators.php 9 0
tests/input/class-references.php 10 0
tests/input/ClassPropertySpacing.php 2 0
Expand Down Expand Up @@ -51,9 +52,9 @@ tests/input/use-ordering.php 1 0
tests/input/useless-semicolon.php 2 0
tests/input/UselessConditions.php 21 0
----------------------------------------------------------------------
A TOTAL OF 437 ERRORS AND 0 WARNINGS WERE FOUND IN 47 FILES
A TOTAL OF 452 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 370 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 374 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


32 changes: 32 additions & 0 deletions tests/fixed/attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

#[Attribute1]
#[Attribute2]
#[Attribute3]
class TestClass
{
}

#[Attribute1]
#[Attribute2]
#[Attribute3]
class TestClass2
{
}

#[Attribute1]
#[Attribute2]
#[Attribute3]
class TestClass3
{
}

/** @internal */
#[Attribute1]
#[Attribute2]
#[Attribute3]
class TestClass4
{
}
31 changes: 31 additions & 0 deletions tests/input/attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

#[Attribute1] #[Attribute2]
#[Attribute3]
class TestClass
{
}

#[Attribute1,Attribute2]
#[Attribute3]
class TestClass2
{
}

#[Attribute1]
#[Attribute2]
#[Attribute3]

class TestClass3
{
}

#[Attribute1]
#[Attribute2]
#[Attribute3]
/** @internal */
class TestClass4
{
}
64 changes: 32 additions & 32 deletions tests/php72-compatibility.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
index 151bce4..0afca8c 100644
index 9e34caa..ea3b611 100644
--- a/tests/expected_report.txt
+++ b/tests/expected_report.txt
@@ -14,26 +14,23 @@ tests/input/constants-var.php 7 0
@@ -16,26 +16,23 @@ tests/input/constants-var.php 7 0
tests/input/ControlStructures.php 28 0
tests/input/doc-comment-spacing.php 11 0
tests/input/duplicate-assignment-variable.php 1 0
Expand Down Expand Up @@ -37,7 +37,7 @@ index 151bce4..0afca8c 100644
tests/input/semicolon_spacing.php 3 0
tests/input/single-line-array-spacing.php 5 0
tests/input/spread-operator.php 6 0
@@ -42,17 +39,16 @@ tests/input/strings.php 1 0
@@ -44,17 +41,16 @@ tests/input/strings.php 1 0
tests/input/superfluous-naming.php 11 0
tests/input/test-case.php 8 0
tests/input/trailing_comma_on_array.php 1 0
Expand All @@ -51,19 +51,19 @@ index 151bce4..0afca8c 100644
-tests/input/UselessConditions.php 21 0
+tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
-A TOTAL OF 437 ERRORS AND 0 WARNINGS WERE FOUND IN 47 FILES
+A TOTAL OF 390 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
-A TOTAL OF 452 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
+A TOTAL OF 405 ERRORS AND 0 WARNINGS WERE FOUND IN 44 FILES
----------------------------------------------------------------------
-PHPCBF CAN FIX 370 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 323 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
-PHPCBF CAN FIX 374 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 327 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


diff --git a/tests/fixed/ControlStructures.php b/tests/fixed/ControlStructures.php
index f8f7f65..a653086 100644
--- a/tests/fixed/ControlStructures.php
+++ b/tests/fixed/ControlStructures.php
@@ -104,7 +104,7 @@ class ControlStructures
@@ -104,7 +104,7 @@ public function spaceBelowBlocks(): void

try {
echo 4;
Expand All @@ -76,7 +76,7 @@ diff --git a/tests/fixed/EarlyReturn.php b/tests/fixed/EarlyReturn.php
index fc734db..caf1dbb 100644
--- a/tests/fixed/EarlyReturn.php
+++ b/tests/fixed/EarlyReturn.php
@@ -11,7 +11,7 @@ class EarlyReturn
@@ -11,7 +11,7 @@ public function bar(): bool
return $bar === 'bar';
}

Expand Down Expand Up @@ -117,7 +117,7 @@ diff --git a/tests/fixed/NamingCamelCase.php b/tests/fixed/NamingCamelCase.php
index 5493471..57d9f2b 100644
--- a/tests/fixed/NamingCamelCase.php
+++ b/tests/fixed/NamingCamelCase.php
@@ -6,11 +6,14 @@ namespace Example;
@@ -6,11 +6,14 @@

class NamingCamelCase
{
Expand Down Expand Up @@ -155,7 +155,7 @@ diff --git a/tests/fixed/TrailingCommaOnFunctions.php b/tests/fixed/TrailingComm
index 4248238..f3ffa91 100644
--- a/tests/fixed/TrailingCommaOnFunctions.php
+++ b/tests/fixed/TrailingCommaOnFunctions.php
@@ -15,7 +15,7 @@ class TrailingCommaOnFunctions
@@ -15,7 +15,7 @@ public function a(int $arg): void
}

public function b(
Expand All @@ -164,7 +164,7 @@ index 4248238..f3ffa91 100644
): void {
}

@@ -28,7 +28,7 @@ class TrailingCommaOnFunctions
@@ -28,7 +28,7 @@ public function uses(): void
};

$multiLine = static function (int $arg) use (
Expand All @@ -173,7 +173,7 @@ index 4248238..f3ffa91 100644
): void {
var_dump($var);
};
@@ -37,9 +37,8 @@ class TrailingCommaOnFunctions
@@ -37,9 +37,8 @@ public function uses(): void

$class = new TrailingCommaOnFunctions();

Expand All @@ -188,7 +188,7 @@ diff --git a/tests/fixed/UselessConditions.php b/tests/fixed/UselessConditions.p
index 71e0cfb..2151b17 100644
--- a/tests/fixed/UselessConditions.php
+++ b/tests/fixed/UselessConditions.php
@@ -95,7 +95,7 @@ class UselessConditions
@@ -95,7 +95,7 @@ public function necessaryIfConditionWithMethodCall(): bool
return false;
}

Expand All @@ -201,7 +201,7 @@ diff --git a/tests/fixed/arrow-functions-format.php b/tests/fixed/arrow-function
index 4da39b8..a45074f 100644
--- a/tests/fixed/arrow-functions-format.php
+++ b/tests/fixed/arrow-functions-format.php
@@ -18,10 +18,10 @@ $returningObject = static fn () => new stdClass();
@@ -18,10 +18,10 @@

$multiLineArrowFunctions = Collection::from([1, 2])
->map(
Expand Down Expand Up @@ -244,7 +244,7 @@ index 4988dab..2df56a0 100644
$this->baxBax = $baxBax;

parent::__construct();
@@ -41,7 +54,7 @@ class Example extends ParentClass implements IteratorAggregate
@@ -41,7 +54,7 @@ public function __construct(private int|null $foo = null, private array $bar = [
/**
* Description
*/
Expand All @@ -257,7 +257,7 @@ diff --git a/tests/fixed/namespaces-spacing.php b/tests/fixed/namespaces-spacing
index 36cbae2..d42bbfe 100644
--- a/tests/fixed/namespaces-spacing.php
+++ b/tests/fixed/namespaces-spacing.php
@@ -16,5 +16,5 @@ use const DATE_RFC3339;
@@ -16,5 +16,5 @@
strrev(
(new DateTimeImmutable('@' . time(), new DateTimeZone('UTC')))
->sub(new DateInterval('P1D'))
Expand All @@ -268,7 +268,7 @@ diff --git a/tests/fixed/new_with_parentheses.php b/tests/fixed/new_with_parenth
index 47a06ec..6e81bbe 100644
--- a/tests/fixed/new_with_parentheses.php
+++ b/tests/fixed/new_with_parentheses.php
@@ -24,5 +24,5 @@ $y = [new stdClass()];
@@ -24,5 +24,5 @@

$z = new stdClass() ? new stdClass() : new stdClass();

Expand Down Expand Up @@ -302,7 +302,7 @@ diff --git a/tests/fixed/null_coalesce_operator.php b/tests/fixed/null_coalesce_
index 51c361c..8846dd1 100644
--- a/tests/fixed/null_coalesce_operator.php
+++ b/tests/fixed/null_coalesce_operator.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
@@ -4,7 +4,7 @@

$foo = $_GET['foo'] ?? 'foo';

Expand Down Expand Up @@ -373,7 +373,7 @@ diff --git a/tests/fixed/return_type_on_methods.php b/tests/fixed/return_type_on
index 0c897ae..8e2c6f7 100644
--- a/tests/fixed/return_type_on_methods.php
+++ b/tests/fixed/return_type_on_methods.php
@@ -31,7 +31,7 @@ class Test
@@ -31,7 +31,7 @@ public function f(
int $c,
int $d,
int $e,
Expand All @@ -382,7 +382,7 @@ index 0c897ae..8e2c6f7 100644
): void {
}

@@ -40,7 +40,7 @@ class Test
@@ -40,7 +40,7 @@ public function g(
int $c,
int $d,
int $e,
Expand All @@ -391,7 +391,7 @@ index 0c897ae..8e2c6f7 100644
): void {
}

@@ -49,7 +49,7 @@ class Test
@@ -49,7 +49,7 @@ public function h(
int $c,
int $d,
int $e,
Expand All @@ -400,7 +400,7 @@ index 0c897ae..8e2c6f7 100644
): void {
}

@@ -58,7 +58,7 @@ class Test
@@ -58,7 +58,7 @@ public function i(
int $c,
int $d,
int $e,
Expand All @@ -409,7 +409,7 @@ index 0c897ae..8e2c6f7 100644
): void {
}

@@ -67,7 +67,7 @@ class Test
@@ -67,7 +67,7 @@ public function j(
int $c,
int $d,
int $e,
Expand All @@ -422,7 +422,7 @@ diff --git a/tests/fixed/type-hints.php b/tests/fixed/type-hints.php
index 5e26ed8..10e6f34 100644
--- a/tests/fixed/type-hints.php
+++ b/tests/fixed/type-hints.php
@@ -10,7 +10,7 @@ use Traversable;
@@ -10,7 +10,7 @@
class TraversableTypeHints
{
/** @var Traversable */
Expand All @@ -431,7 +431,7 @@ index 5e26ed8..10e6f34 100644

/**
* @param Iterator $iterator
@@ -25,5 +25,6 @@ class TraversableTypeHints
@@ -25,5 +25,6 @@ public function get(Iterator $iterator): Traversable

class UnionTypeHints
{
Expand All @@ -443,7 +443,7 @@ diff --git a/tests/input/ControlStructures.php b/tests/input/ControlStructures.p
index 73944e3..a0e0b2e 100644
--- a/tests/input/ControlStructures.php
+++ b/tests/input/ControlStructures.php
@@ -93,7 +93,7 @@ class ControlStructures
@@ -93,7 +93,7 @@ public function spaceBelowBlocks(): void
}
try {
echo 4;
Expand Down Expand Up @@ -498,7 +498,7 @@ diff --git a/tests/input/TrailingCommaOnFunctions.php b/tests/input/TrailingComm
index fc5480e..f3ffa91 100644
--- a/tests/input/TrailingCommaOnFunctions.php
+++ b/tests/input/TrailingCommaOnFunctions.php
@@ -10,7 +10,7 @@ use function var_dump;
@@ -10,7 +10,7 @@

class TrailingCommaOnFunctions
{
Expand All @@ -507,7 +507,7 @@ index fc5480e..f3ffa91 100644
{
}

@@ -23,7 +23,7 @@ class TrailingCommaOnFunctions
@@ -23,7 +23,7 @@ public function uses(): void
{
$var = null;

Expand All @@ -516,7 +516,7 @@ index fc5480e..f3ffa91 100644
var_dump($var);
};

@@ -37,8 +37,7 @@ class TrailingCommaOnFunctions
@@ -37,8 +37,7 @@ public function uses(): void

$class = new TrailingCommaOnFunctions();

Expand All @@ -530,7 +530,7 @@ diff --git a/tests/input/arrow-functions-format.php b/tests/input/arrow-function
index d3903ff..8a358e8 100644
--- a/tests/input/arrow-functions-format.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why there are so many changes 🤔 something is wrong. Should I clean it up?

Copy link
Member

@greg0ire greg0ire Oct 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe what I did was wrong? How do you usually proceed? What I did was:

  1. edit the patch to remove the hunk about the expected report, so that it applies
  2. apply the patch
  3. set the correct php version in phpcs.xml.dist
  4. run vendor/bin/phpcs $(find tests/input/* | sort) --report=summary --report-file=tests/expected_report.txt

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends, I sometimes do it by hand. I have re-applied patches on 11.x branch and there are little changes only.

I guess this must be run on related php version vendor/bin/phpcs $(find tests/input/* | sort) --report=summary --report-file=tests/expected_report.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simPod I experienced the same issue, this is why I asked for somebody to finish it. Yes, it seems PHP version running the phpcs is relevant. I guess it also installs different versions of composer dependencies, especially for e.g. 7.2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe nobody here likes the patches but that's the best we've got. I'll look into it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there are sniffs that are enabled/disabled by detected PHP version - and it’s intentional here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forced the php version in step 3 of my process (otherwise the tests would be red)

Copy link
Contributor

@simPod simPod Oct 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have opened in as #297 #298 since I cannot push here. We can either merge that (I kept the author) or someone with permissions do some commit shuffling.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean #298

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I have done +1 but somebody opened a PR meanwhile :D

+++ b/tests/input/arrow-functions-format.php
@@ -18,10 +18,10 @@ $returningObject = static fn () => new stdClass();
@@ -18,10 +18,10 @@

$multiLineArrowFunctions = Collection::from([1, 2])
->map(
Expand All @@ -547,7 +547,7 @@ diff --git a/tests/input/namespaces-spacing.php b/tests/input/namespaces-spacing
index e7be018..e1ab639 100644
--- a/tests/input/namespaces-spacing.php
+++ b/tests/input/namespaces-spacing.php
@@ -11,5 +11,5 @@ use const DATE_RFC3339;
@@ -11,5 +11,5 @@
strrev(
(new DateTimeImmutable('@' . time(), new DateTimeZone('UTC')))
->sub(new DateInterval('P1D'))
Expand Down
Loading