From e28ebe40a911f6d2732763df683155690289adf8 Mon Sep 17 00:00:00 2001 From: Carlos A Sastre Date: Wed, 27 Sep 2023 22:07:09 +0200 Subject: [PATCH 1/2] Deprecate unclear selectors --- docs/documentation/selectors.md | 18 +++---- src/Selector/SelectorPrimitive.php | 62 ++++++++++++++++++++++++- tests/architecture/AssertionTest.php | 6 +-- tests/architecture/CleanClassesTest.php | 8 ++-- 4 files changed, 76 insertions(+), 18 deletions(-) diff --git a/docs/documentation/selectors.md b/docs/documentation/selectors.md index e53137d4..298f5403 100644 --- a/docs/documentation/selectors.md +++ b/docs/documentation/selectors.md @@ -22,12 +22,12 @@ The first two selectors will select the `App\User\Domain\UserEntity` class. The third one will select all classes whose name ends in `Handler`. -## Selector::namespace() +## Selector::inNamespace() Selects classes in the given namespace. ```php -Selector::namespace('App\User\Domain') -Selector::namespace('/^App\\\\.+\\\\Domain/', true) +Selector::inNamespace('App\User\Domain') +Selector::inNamespace('/^App\\\\.+\\\\Domain/', true) ``` The first selector will select all classes in the `App\User\Domain` namespace. @@ -40,25 +40,25 @@ Select classes that implement the given interface. ## Selector::extends() Select classes that extend the given class. -## Selector::interface() +## Selector::isInterface() Select all interfaces. ## Selector::hasAttribute() Select classes that has the given attribute. -## Selector::enum() +## Selector::isEnum() Select all enums. -## Selector::abstract() +## Selector::isAbstract() Select all abstract classes. -## Selector::final() +## Selector::isFinal() Select all final classes. -## Selector::readonly() +## Selector::isReadonly() Select all readonly classes. -## Selector::attribute() +## Selector::isAttribute() Select all attribute classes.
diff --git a/src/Selector/SelectorPrimitive.php b/src/Selector/SelectorPrimitive.php index e912ea85..b0654e65 100644 --- a/src/Selector/SelectorPrimitive.php +++ b/src/Selector/SelectorPrimitive.php @@ -9,56 +9,114 @@ public static function all(): All return new All(); } + /** + * @deprecated use Selector::isInterface() + */ public static function interface(): IsInterface + { + return self::isInterface(); + } + + public static function isInterface(): IsInterface { return new IsInterface(); } + /** + * @deprecated use Selector::isAbstract() + */ public static function abstract(): IsAbstract + { + return self::isAbstract(); + } + + public static function isAbstract(): IsAbstract { return new IsAbstract(); } /** - * @deprecated use Selector::NOT(Selector::abstract()) + * @deprecated use Selector::NOT(Selector::isAbstract()) */ public static function notAbstract(): IsNotAbstract { return new IsNotAbstract(); } + /** + * @deprecated use Selector::isFinal() + */ public static function final(): IsFinal + { + return self::isFinal(); + } + + public static function isFinal(): IsFinal { return new IsFinal(); } + /** + * @deprecated use Selector::isReadonly() + */ public static function readonly(): IsReadonly + { + return self::isReadonly(); + } + + public static function isReadonly(): IsReadonly { return new IsReadonly(); } /** - * @deprecated use Selector::NOT(Selector::final()) + * @deprecated use Selector::NOT(Selector::isFinal()) */ public static function notFinal(): IsNotFinal { return new IsNotFinal(); } + /** + * @deprecated use Selector::isEnum() + */ public static function enum(): IsEnum + { + return self::isEnum(); + } + + public static function isEnum(): IsEnum { return new IsEnum(); } + /** + * @deprecated use Selector::isAttribute() + */ public static function attribute(): IsAttribute + { + return self::isAttribute(); + } + + public static function isAttribute(): IsAttribute { return new IsAttribute(); } /** * @param class-string|non-empty-string $namespace + * + * @deprecated use Selector::inNamespace() */ public static function namespace(string $namespace, bool $regex = false): ClassNamespace + { + return self::inNamespace($namespace, $regex); + } + + /** + * @param class-string|non-empty-string $namespace + */ + public static function inNamespace(string $namespace, bool $regex = false): ClassNamespace { return new ClassNamespace($namespace, $regex); } diff --git a/tests/architecture/AssertionTest.php b/tests/architecture/AssertionTest.php index 121ad02d..f7d7c8da 100644 --- a/tests/architecture/AssertionTest.php +++ b/tests/architecture/AssertionTest.php @@ -33,14 +33,14 @@ public function test_rules_dependencies(): Rule { return PHPat::rule() ->classes(Selector::implements(Assertion::class)) - ->excluding(Selector::abstract()) + ->excluding(Selector::isAbstract()) ->canOnlyDependOn() ->classes( Selector::classname(TypeNodeParser::class), Selector::extends(RelationAssertion::class), Selector::extends(DeclarationAssertion::class), - Selector::namespace('PhpParser'), - Selector::namespace('PHPStan'), + Selector::inNamespace('PhpParser'), + Selector::inNamespace('PHPStan'), ) ; } diff --git a/tests/architecture/CleanClassesTest.php b/tests/architecture/CleanClassesTest.php index f75ff635..5c86c00e 100644 --- a/tests/architecture/CleanClassesTest.php +++ b/tests/architecture/CleanClassesTest.php @@ -15,12 +15,12 @@ public function test_non_abstract_classes_are_final(): Rule { return PHPat::rule() ->classes( - Selector::namespace('PHPat'), - Selector::namespace('Tests\PHPat\architecture') + Selector::inNamespace('PHPat'), + Selector::inNamespace('Tests\PHPat\architecture') ) ->excluding( - Selector::abstract(), - Selector::interface(), + Selector::isAbstract(), + Selector::isInterface(), Selector::extends(AbstractStep::class), Selector::classname(SelectorPrimitive::class), Selector::classname(TestParser::class) From 290a65d0db1314e0660932cfdddda171445e7894 Mon Sep 17 00:00:00 2001 From: Carlos A Sastre Date: Wed, 27 Sep 2023 22:19:42 +0200 Subject: [PATCH 2/2] Update CHANGELOG --- docs/CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ad983010..74b915fb 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,16 @@ # Changelog All notable changes to this project will be documented in this file. +## 0.10.9 +* Add `hasAttribute()` selector. +* Deprecate `namespace()` selector in favor of `inNamespace()`. +* Deprecate `interface()` selector in favor of `isInterface()`. +* Deprecate `abstract()` selector in favor of `isAbstract()`. +* Deprecate `final()` selector in favor of `isFinal()`. +* Deprecate `readonly()` selector in favor of `isReadonly()`. +* Deprecate `enum()` selector in favor of `isEnum()`. +* Deprecate `attribute()` selector in favor of `isAttribute()`. + ## 0.10.8 * Fix `Should-` rules ignoring classes with empty findings.