-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RequireExtends and RequireImplements attributes
- Loading branch information
1 parent
c0a2543
commit 4ad035e
Showing
7 changed files
with
223 additions
and
21 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
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,28 @@ | ||
# `RequireExtends` Attribute | ||
|
||
This attribute is the equivalent of the `@require-extends` annotation. It can be applied to a trait to specify that the class using it must extend a specific class. | ||
|
||
## Arguments | ||
|
||
The attribute accepts one string that defines the class that needs to be extended. The attribute itself does not have a knowledge of which classes are valid and which are not and this will depend on the implementation for each particular tool. | ||
|
||
We aim to accept all the classes accepted by static analysis tools for the `@require-extends` annotation. | ||
|
||
## Example usage | ||
|
||
```php | ||
<?php | ||
|
||
use PhpStaticAnalysis\Attributes\RequireExtends; | ||
|
||
abstract class Parent { | ||
} | ||
|
||
#[RequireExtends('ParentClass')] | ||
trait myTrait { | ||
} | ||
|
||
class Child extends Parent { | ||
use myTrait; | ||
} | ||
``` |
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,34 @@ | ||
# `RequireImplements` Attribute | ||
|
||
This attribute is the equivalent of the `@require-implements` annotation. It can be applied to a trait to indicate that the class using it should implement one or more interfaces. | ||
|
||
## Arguments | ||
|
||
The attribute accepts one or more strings that define the interfaces that need to be implemented. The attribute itself does not have a knowledge of which interfaces are valid and which are not and this will depend on the implementation for each particular tool. | ||
|
||
We aim to accept all the interface names accepted by static analysis tools for the `@require-implements` annotation. | ||
|
||
The arguments need to be unnamed arguments. | ||
|
||
If the class has more than one interface that we want to require, the different interfaces can either be declared as a list of strings for a single `RequireInterface` attribute or as a list of `RequireInterface` attributes (or even a combination of both, though we don't expect this to be actually used). | ||
|
||
## Example usage | ||
|
||
```php | ||
<?php | ||
|
||
use PhpStaticAnalysis\Attributes\RequireImplements; | ||
|
||
interface RequireInterface | ||
{ | ||
} | ||
|
||
#[RequireImplements('RequireInterface')] | ||
trait MyTrait | ||
{ | ||
} | ||
|
||
class MyClass implements RequireInterface { | ||
use MyTrait; | ||
} | ||
``` |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpStaticAnalysis\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute( | ||
Attribute::TARGET_CLASS | ||
)] | ||
final class RequireExtends | ||
{ | ||
public function __construct( | ||
string $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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpStaticAnalysis\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute( | ||
Attribute::TARGET_CLASS | | ||
Attribute::IS_REPEATABLE | ||
)] | ||
final class RequireImplements | ||
{ | ||
public function __construct( | ||
string ...$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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpStaticAnalysis\Attributes\RequireExtends; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RequireExtendsTest extends TestCase | ||
{ | ||
public function testClassRequireExtends(): void | ||
{ | ||
$reflection = new ReflectionClass(RequireMyTrait::class); | ||
$this->assertEquals('RequireParentClass', self::getRequireExtendssFromReflection($reflection)); | ||
} | ||
|
||
public static function getRequireExtendssFromReflection( | ||
ReflectionClass $reflection | ||
): string { | ||
$attributes = $reflection->getAttributes(); | ||
$extends = ''; | ||
foreach ($attributes as $attribute) { | ||
if ($attribute->getName() === RequireExtends::class) { | ||
$attribute->newInstance(); | ||
$extends = $attribute->getArguments()[0]; | ||
} | ||
} | ||
|
||
return $extends; | ||
} | ||
} | ||
|
||
class RequireParentClass | ||
{ | ||
} | ||
|
||
#[RequireExtends('RequireParentClass')] | ||
trait RequireMyTrait | ||
{ | ||
} | ||
|
||
class RequireChildClass extends RequireParentClass | ||
{ | ||
use RequireMyTrait; | ||
} |
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpStaticAnalysis\Attributes\RequireImplements; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RequireImplementsTest extends TestCase implements RequireTestInterface, RequireTestInterface2, RequireTestInterface3 | ||
{ | ||
use RequireInterfaceTrait; | ||
|
||
public function testClassRequireImplements(): void | ||
{ | ||
$reflection = new ReflectionClass(RequireInterfaceTrait::class); | ||
$this->assertEquals([ | ||
'RequireTestInterface', | ||
'RequireTestInterface2', | ||
'RequireTestInterface3', | ||
], self::getRequireImplementssFromReflection($reflection)); | ||
} | ||
|
||
public static function getRequireImplementssFromReflection( | ||
ReflectionClass $reflection | ||
): array { | ||
$attributes = $reflection->getAttributes(); | ||
$implements = []; | ||
foreach ($attributes as $attribute) { | ||
if ($attribute->getName() === RequireImplements::class) { | ||
$attribute->newInstance(); | ||
$implements = array_merge($implements, $attribute->getArguments()); | ||
} | ||
} | ||
|
||
return $implements; | ||
} | ||
} | ||
|
||
#[RequireImplements('RequireTestInterface')] | ||
#[RequireImplements( | ||
'RequireTestInterface2', | ||
'RequireTestInterface3' | ||
)] | ||
trait RequireInterfaceTrait | ||
{ | ||
} | ||
|
||
interface RequireTestInterface | ||
{ | ||
} | ||
|
||
interface RequireTestInterface2 | ||
{ | ||
} | ||
|
||
interface RequireTestInterface3 | ||
{ | ||
} |