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 IsException, IsError, and IsThrowable selectors #288

Merged
merged 5 commits into from
Sep 29, 2024
Merged
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
9 changes: 9 additions & 0 deletions docs/documentation/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ 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::isError()
Select classes that extend the `\Error` class.

## Selector::isException()
Select classes that extend the `\Exception` class.

## Selector::isThrowable()
Select classes that implement the `\Throwable` interface.

## Selector::implements()
Select classes that implement the given interface.

Expand Down
18 changes: 18 additions & 0 deletions src/Selector/IsError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace PHPat\Selector;

use PHPStan\Reflection\ClassReflection;

final class IsError implements SelectorInterface
{
public function getName(): string
{
return '-all errors-';
}

public function matches(ClassReflection $classReflection): bool
{
return $classReflection->isSubclassOf(\Error::class);
}
}
18 changes: 18 additions & 0 deletions src/Selector/IsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace PHPat\Selector;

use PHPStan\Reflection\ClassReflection;

final class IsException implements SelectorInterface
{
public function getName(): string
{
return '-all exceptions-';
}

public function matches(ClassReflection $classReflection): bool
{
return $classReflection->isSubclassOf(\Exception::class);
}
}
18 changes: 18 additions & 0 deletions src/Selector/IsThrowable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace PHPat\Selector;

use PHPStan\Reflection\ClassReflection;

final class IsThrowable implements SelectorInterface
{
public function getName(): string
{
return '-all throwables-';
}

public function matches(ClassReflection $classReflection): bool
{
return $classReflection->implementsInterface(\Throwable::class);
}
}
15 changes: 15 additions & 0 deletions src/Selector/SelectorPrimitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ public static function all(): All
return new All();
}

public static function isException(): IsException
{
return new IsException();
}

public static function isThrowable(): IsThrowable
{
return new IsThrowable();
}

public static function isError(): IsError
{
return new IsError();
}

/**
* @deprecated
* @see Selector::isInterface()
Expand Down
Loading