generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Token classifier & refactor filters (#41)
- Loading branch information
Showing
28 changed files
with
696 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,4 @@ jobs: | |
os: >- | ||
['ubuntu-latest', 'windows-latest'] | ||
php: >- | ||
['8.0', '8.1'] | ||
['8.1', '8.2'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,4 @@ jobs: | |
os: >- | ||
['ubuntu-latest'] | ||
php: >- | ||
['8.0'] | ||
['8.1'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ jobs: | |
os: >- | ||
['ubuntu-latest'] | ||
php: >- | ||
['8.0'] | ||
['8.2'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,4 @@ jobs: | |
os: >- | ||
['ubuntu-latest'] | ||
php: >- | ||
['8.0', '8.1'] | ||
['8.1', '8.2'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ finder: | |
- vendor | ||
not-name: | ||
- wrong_file.php | ||
- namespace.php | ||
|
||
enabled: | ||
- alpha_ordered_traits | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Classifier\Filter; | ||
|
||
use ReflectionAttribute; | ||
use ReflectionClass; | ||
|
||
final class ClassAttributes implements FilterInterface | ||
{ | ||
private array $attributes; | ||
|
||
public function __construct(string ...$attributes) | ||
{ | ||
$this->attributes = $attributes; | ||
} | ||
|
||
public function match(ReflectionClass $reflectionClass): bool | ||
{ | ||
if (empty($this->attributes)) { | ||
return false; | ||
} | ||
|
||
$attributes = $reflectionClass->getAttributes(); | ||
$attributeNames = array_map( | ||
static fn(ReflectionAttribute $attribute) => $attribute->getName(), | ||
$attributes | ||
); | ||
|
||
return count(array_intersect($this->attributes, $attributeNames)) === count($this->attributes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Classifier\Filter; | ||
|
||
use ReflectionClass; | ||
|
||
final class ClassImplements implements FilterInterface | ||
{ | ||
private array $interfaces; | ||
|
||
public function __construct(string ...$interfaces) | ||
{ | ||
$this->interfaces = $interfaces; | ||
} | ||
|
||
public function match(ReflectionClass $reflectionClass): bool | ||
{ | ||
if (empty($this->interfaces) || $reflectionClass->isInterface()) { | ||
return false; | ||
} | ||
$interfaces = $reflectionClass->getInterfaceNames(); | ||
|
||
return count(array_intersect($this->interfaces, $interfaces)) === count($this->interfaces); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Classifier\Filter; | ||
|
||
use ReflectionClass; | ||
|
||
interface FilterInterface | ||
{ | ||
/** | ||
* Tests the filter against class. | ||
* | ||
* @param ReflectionClass $reflectionClass | ||
* | ||
* @return bool `true` if class matches against filter. Otherwise, `false`. | ||
*/ | ||
public function match(ReflectionClass $reflectionClass): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Classifier\Filter; | ||
|
||
use ReflectionClass; | ||
|
||
final class SubclassOf implements FilterInterface | ||
{ | ||
/** | ||
* @param class-string $class | ||
*/ | ||
public function __construct(private string $class) | ||
{ | ||
} | ||
|
||
public function match(ReflectionClass $reflectionClass): bool | ||
{ | ||
return $reflectionClass->isSubclassOf($this->class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Classifier\Filter; | ||
|
||
use ReflectionAttribute; | ||
use ReflectionClass; | ||
|
||
final class TargetAttribute implements FilterInterface | ||
{ | ||
/** | ||
* @param class-string $attribute | ||
*/ | ||
public function __construct(private string $attribute) | ||
{ | ||
} | ||
|
||
public function match(ReflectionClass $reflectionClass): bool | ||
{ | ||
$attributes = $reflectionClass->getAttributes($this->attribute, ReflectionAttribute::IS_INSTANCEOF); | ||
$attributeNames = array_map( | ||
Check warning on line 22 in src/Filter/TargetAttribute.php GitHub Actions / mutation / PHP 8.2-ubuntu-latest
Check warning on line 22 in src/Filter/TargetAttribute.php GitHub Actions / mutation / PHP 8.2-ubuntu-latest
|
||
static fn(ReflectionAttribute $attribute) => $attribute->getName(), | ||
$attributes | ||
); | ||
|
||
return !empty($attributeNames); | ||
} | ||
} |
Oops, something went wrong.