Skip to content

Commit

Permalink
Merge pull request #236 from carlosas/deprecate-selectors
Browse files Browse the repository at this point in the history
Deprecate unclear selectors
  • Loading branch information
carlosas authored Sep 27, 2023
2 parents 4d7db7c + 290a65d commit 8732c57
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 18 deletions.
10 changes: 10 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
18 changes: 9 additions & 9 deletions docs/documentation/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.

<br />
Expand Down
62 changes: 60 additions & 2 deletions src/Selector/SelectorPrimitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/architecture/AssertionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
)
;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/architecture/CleanClassesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8732c57

Please sign in to comment.