From 63b7890746f2cf0b75668418b61a00022ef5e0ee Mon Sep 17 00:00:00 2001 From: Carlos A Sastre Date: Wed, 27 Sep 2023 21:35:03 +0200 Subject: [PATCH 1/5] Add hasAttribute() selector --- src/Selector/HasAttribute.php | 58 ++++++++++++++++++++++++++++++ src/Selector/SelectorPrimitive.php | 8 +++++ 2 files changed, 66 insertions(+) create mode 100644 src/Selector/HasAttribute.php diff --git a/src/Selector/HasAttribute.php b/src/Selector/HasAttribute.php new file mode 100644 index 00000000..a529579b --- /dev/null +++ b/src/Selector/HasAttribute.php @@ -0,0 +1,58 @@ +classname = $classname; + $this->isRegex = $isRegex; + } + + public function getName(): string + { + return $this->classname; + } + + public function matches(ClassReflection $classReflection): bool + { + /** @var list $attributes */ + $attributes = $classReflection->getNativeReflection()->getAttributes(); + + if ($this->isRegex) { + return $this->matchesRegex($attributes); + } + + foreach ($attributes as $attribute) { + if ($attribute->getName() === $this->classname) { + return true; + } + } + + return false; + } + + /** + * @param list $attributes + */ + private function matchesRegex(array $attributes): bool + { + foreach ($attributes as $attribute) { + if (preg_match($this->classname, $attribute->getName()) === 1) { + return true; + } + } + + return false; + } +} diff --git a/src/Selector/SelectorPrimitive.php b/src/Selector/SelectorPrimitive.php index ee385d8e..e912ea85 100644 --- a/src/Selector/SelectorPrimitive.php +++ b/src/Selector/SelectorPrimitive.php @@ -86,4 +86,12 @@ public static function extends(string $fqcn, bool $regex = false): ClassExtends { return new ClassExtends($fqcn, $regex); } + + /** + * @param class-string|non-empty-string $fqcn + */ + public static function hasAttribute(string $fqcn, bool $regex = false): HasAttribute + { + return new HasAttribute($fqcn, $regex); + } } From 5ffeefe9d7493310b1a107c28252f78457d2810d Mon Sep 17 00:00:00 2001 From: Carlos A Sastre Date: Wed, 27 Sep 2023 21:36:10 +0200 Subject: [PATCH 2/5] Apply new coding standards --- src/Rule/Assertion/Assertion.php | 4 +--- src/Test/Builder/BuildStep.php | 4 +--- tests/fixtures/Simple/SimpleAbstractClass.php | 4 +--- tests/fixtures/Simple/SimpleAbstractClassTwo.php | 4 +--- tests/fixtures/Simple/SimpleAttribute.php | 6 +----- tests/fixtures/Simple/SimpleClass.php | 4 +--- tests/fixtures/Simple/SimpleClassFive.php | 4 +--- tests/fixtures/Simple/SimpleClassFour.php | 4 +--- tests/fixtures/Simple/SimpleClassSix.php | 4 +--- tests/fixtures/Simple/SimpleClassThree.php | 4 +--- tests/fixtures/Simple/SimpleClassTwo.php | 4 +--- tests/fixtures/Simple/SimpleException.php | 4 +--- tests/fixtures/Simple/SimpleExceptionTwo.php | 4 +--- tests/fixtures/Simple/SimpleFinalClass.php | 4 +--- tests/fixtures/Simple/SimpleInterface.php | 4 +--- tests/fixtures/Simple/SimpleInterfaceTwo.php | 4 +--- tests/fixtures/Simple/SimpleTrait.php | 4 +--- tests/fixtures/Special/ClassImplementing.php | 4 +--- tests/fixtures/Special/ClassWithStaticMethod.php | 4 +--- tests/fixtures/Special/ClassWithStaticMethodTwo.php | 4 +--- 20 files changed, 20 insertions(+), 62 deletions(-) diff --git a/src/Rule/Assertion/Assertion.php b/src/Rule/Assertion/Assertion.php index 06a25f76..c023bba7 100644 --- a/src/Rule/Assertion/Assertion.php +++ b/src/Rule/Assertion/Assertion.php @@ -8,6 +8,4 @@ /** * @extends PHPStanRule */ -interface Assertion extends PHPStanRule -{ -} +interface Assertion extends PHPStanRule {} diff --git a/src/Test/Builder/BuildStep.php b/src/Test/Builder/BuildStep.php index e2a8a5cb..db839def 100644 --- a/src/Test/Builder/BuildStep.php +++ b/src/Test/Builder/BuildStep.php @@ -2,6 +2,4 @@ namespace PHPat\Test\Builder; -class BuildStep extends AbstractStep -{ -} +class BuildStep extends AbstractStep {} diff --git a/tests/fixtures/Simple/SimpleAbstractClass.php b/tests/fixtures/Simple/SimpleAbstractClass.php index 508b3061..302c53c0 100644 --- a/tests/fixtures/Simple/SimpleAbstractClass.php +++ b/tests/fixtures/Simple/SimpleAbstractClass.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -abstract class SimpleAbstractClass -{ -} +abstract class SimpleAbstractClass {} diff --git a/tests/fixtures/Simple/SimpleAbstractClassTwo.php b/tests/fixtures/Simple/SimpleAbstractClassTwo.php index 056bb1a2..053e3ebc 100644 --- a/tests/fixtures/Simple/SimpleAbstractClassTwo.php +++ b/tests/fixtures/Simple/SimpleAbstractClassTwo.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -abstract class SimpleAbstractClassTwo -{ -} +abstract class SimpleAbstractClassTwo {} diff --git a/tests/fixtures/Simple/SimpleAttribute.php b/tests/fixtures/Simple/SimpleAttribute.php index 4b6a1ac2..c9ea9a47 100644 --- a/tests/fixtures/Simple/SimpleAttribute.php +++ b/tests/fixtures/Simple/SimpleAttribute.php @@ -2,9 +2,5 @@ namespace Tests\PHPat\fixtures\Simple; -use Attribute; - #[\Attribute(\Attribute::TARGET_CLASS)] -class SimpleAttribute -{ -} +class SimpleAttribute {} diff --git a/tests/fixtures/Simple/SimpleClass.php b/tests/fixtures/Simple/SimpleClass.php index 4c0f790e..b2b0eea8 100644 --- a/tests/fixtures/Simple/SimpleClass.php +++ b/tests/fixtures/Simple/SimpleClass.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -class SimpleClass -{ -} +class SimpleClass {} diff --git a/tests/fixtures/Simple/SimpleClassFive.php b/tests/fixtures/Simple/SimpleClassFive.php index 446326de..5218b901 100644 --- a/tests/fixtures/Simple/SimpleClassFive.php +++ b/tests/fixtures/Simple/SimpleClassFive.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -class SimpleClassFive -{ -} +class SimpleClassFive {} diff --git a/tests/fixtures/Simple/SimpleClassFour.php b/tests/fixtures/Simple/SimpleClassFour.php index 5201d570..eb19986e 100644 --- a/tests/fixtures/Simple/SimpleClassFour.php +++ b/tests/fixtures/Simple/SimpleClassFour.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -class SimpleClassFour -{ -} +class SimpleClassFour {} diff --git a/tests/fixtures/Simple/SimpleClassSix.php b/tests/fixtures/Simple/SimpleClassSix.php index de160884..d83ff845 100644 --- a/tests/fixtures/Simple/SimpleClassSix.php +++ b/tests/fixtures/Simple/SimpleClassSix.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -class SimpleClassSix -{ -} +class SimpleClassSix {} diff --git a/tests/fixtures/Simple/SimpleClassThree.php b/tests/fixtures/Simple/SimpleClassThree.php index d434939e..6288e21e 100644 --- a/tests/fixtures/Simple/SimpleClassThree.php +++ b/tests/fixtures/Simple/SimpleClassThree.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -class SimpleClassThree -{ -} +class SimpleClassThree {} diff --git a/tests/fixtures/Simple/SimpleClassTwo.php b/tests/fixtures/Simple/SimpleClassTwo.php index c7de2379..5102a742 100644 --- a/tests/fixtures/Simple/SimpleClassTwo.php +++ b/tests/fixtures/Simple/SimpleClassTwo.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -class SimpleClassTwo -{ -} +class SimpleClassTwo {} diff --git a/tests/fixtures/Simple/SimpleException.php b/tests/fixtures/Simple/SimpleException.php index c2a45959..1329ea20 100644 --- a/tests/fixtures/Simple/SimpleException.php +++ b/tests/fixtures/Simple/SimpleException.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -class SimpleException extends \Exception -{ -} +class SimpleException extends \Exception {} diff --git a/tests/fixtures/Simple/SimpleExceptionTwo.php b/tests/fixtures/Simple/SimpleExceptionTwo.php index 8add3e88..0e371472 100644 --- a/tests/fixtures/Simple/SimpleExceptionTwo.php +++ b/tests/fixtures/Simple/SimpleExceptionTwo.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -class SimpleExceptionTwo extends \Exception -{ -} +class SimpleExceptionTwo extends \Exception {} diff --git a/tests/fixtures/Simple/SimpleFinalClass.php b/tests/fixtures/Simple/SimpleFinalClass.php index 2f996c03..35cf34d3 100644 --- a/tests/fixtures/Simple/SimpleFinalClass.php +++ b/tests/fixtures/Simple/SimpleFinalClass.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -final class SimpleFinalClass -{ -} +final class SimpleFinalClass {} diff --git a/tests/fixtures/Simple/SimpleInterface.php b/tests/fixtures/Simple/SimpleInterface.php index b7f917c7..2f3e2f70 100644 --- a/tests/fixtures/Simple/SimpleInterface.php +++ b/tests/fixtures/Simple/SimpleInterface.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -interface SimpleInterface -{ -} +interface SimpleInterface {} diff --git a/tests/fixtures/Simple/SimpleInterfaceTwo.php b/tests/fixtures/Simple/SimpleInterfaceTwo.php index a444300c..a00b5e3f 100644 --- a/tests/fixtures/Simple/SimpleInterfaceTwo.php +++ b/tests/fixtures/Simple/SimpleInterfaceTwo.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -interface SimpleInterfaceTwo -{ -} +interface SimpleInterfaceTwo {} diff --git a/tests/fixtures/Simple/SimpleTrait.php b/tests/fixtures/Simple/SimpleTrait.php index 67875a3f..b9022fe7 100644 --- a/tests/fixtures/Simple/SimpleTrait.php +++ b/tests/fixtures/Simple/SimpleTrait.php @@ -2,6 +2,4 @@ namespace Tests\PHPat\fixtures\Simple; -trait SimpleTrait -{ -} +trait SimpleTrait {} diff --git a/tests/fixtures/Special/ClassImplementing.php b/tests/fixtures/Special/ClassImplementing.php index 01911fc7..361ebf0c 100644 --- a/tests/fixtures/Special/ClassImplementing.php +++ b/tests/fixtures/Special/ClassImplementing.php @@ -4,6 +4,4 @@ use Tests\PHPat\fixtures\Simple\SimpleInterface; -class ClassImplementing implements SimpleInterface -{ -} +class ClassImplementing implements SimpleInterface {} diff --git a/tests/fixtures/Special/ClassWithStaticMethod.php b/tests/fixtures/Special/ClassWithStaticMethod.php index 7b93af95..7e44cb0d 100644 --- a/tests/fixtures/Special/ClassWithStaticMethod.php +++ b/tests/fixtures/Special/ClassWithStaticMethod.php @@ -4,7 +4,5 @@ class ClassWithStaticMethod { - public static function staticMethod(): void - { - } + public static function staticMethod(): void {} } diff --git a/tests/fixtures/Special/ClassWithStaticMethodTwo.php b/tests/fixtures/Special/ClassWithStaticMethodTwo.php index e2150926..983901ce 100644 --- a/tests/fixtures/Special/ClassWithStaticMethodTwo.php +++ b/tests/fixtures/Special/ClassWithStaticMethodTwo.php @@ -4,7 +4,5 @@ class ClassWithStaticMethodTwo { - public static function staticMethod(): void - { - } + public static function staticMethod(): void {} } From 485e1f30df4506d3f35a4f083757dc423c043b00 Mon Sep 17 00:00:00 2001 From: Carlos A Sastre Date: Wed, 27 Sep 2023 21:52:21 +0200 Subject: [PATCH 3/5] Add hasAttribute selector to docs --- docs/documentation/selectors.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/documentation/selectors.md b/docs/documentation/selectors.md index ad5568b6..e53137d4 100644 --- a/docs/documentation/selectors.md +++ b/docs/documentation/selectors.md @@ -35,14 +35,17 @@ The first selector will select all classes in the `App\User\Domain` namespace. The second one will select all classes in a namespace matching the regular expression. ## Selector::implements() -Select classes that implement a given interface. +Select classes that implement the given interface. ## Selector::extends() -Select classes that extend a given class. +Select classes that extend the given class. ## Selector::interface() Select all interfaces. +## Selector::hasAttribute() +Select classes that has the given attribute. + ## Selector::enum() Select all enums. From cbddb2ff86e287794183e849c4c1c657ee69d498 Mon Sep 17 00:00:00 2001 From: Carlos A Sastre Date: Wed, 27 Sep 2023 21:52:40 +0200 Subject: [PATCH 4/5] Ignore Psalm internal failure --- ci/psalm.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ci/psalm.xml b/ci/psalm.xml index 352a607c..4ce7657b 100644 --- a/ci/psalm.xml +++ b/ci/psalm.xml @@ -55,5 +55,9 @@ + + + + From 38584624171c415cb2d6dde4d3f6162840e6443b Mon Sep 17 00:00:00 2001 From: Carlos A Sastre Date: Wed, 27 Sep 2023 21:54:47 +0200 Subject: [PATCH 5/5] Ignore Psalm internal failure --- ci/psalm.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/psalm.xml b/ci/psalm.xml index 4ce7657b..23c71f86 100644 --- a/ci/psalm.xml +++ b/ci/psalm.xml @@ -58,6 +58,7 @@ +