-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from DaveLiddament/feature/restrict-trait-to
ADD RestrictTraitTo
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RestrictTraitTo; | ||
|
||
use DaveLiddament\PhpLanguageExtensions\RestrictTraitTo; | ||
|
||
trait UseAnywhere {} | ||
|
||
#[RestrictTraitTo(Interface1::class)] | ||
trait UseOnlyOnInterface1 {} | ||
|
||
#[RestrictTraitTo(AbstractClass1::class)] | ||
trait UseOnlyOnAbstractClass1 {} | ||
|
||
#[RestrictTraitTo(Class2::class)] | ||
trait UseOnlyOnClass2 {} | ||
|
||
|
||
interface Interface1 {} | ||
|
||
|
||
class AClass { | ||
use UseAnywhere; // OK | ||
use UseOnlyOnInterface1; // ERROR RestrictTraitTo\Interface1 | ||
use UseOnlyOnAbstractClass1; // ERROR RestrictTraitTo\AbstractClass1 | ||
use UseOnlyOnClass2; // ERROR RestrictTraitTo\Class2 | ||
} | ||
|
||
|
||
class ImplementsInterface1 implements Interface1 { | ||
use UseAnywhere; // OK | ||
use UseOnlyOnInterface1; // OK | ||
use UseOnlyOnAbstractClass1; // ERROR RestrictTraitTo\AbstractClass1 | ||
use UseOnlyOnClass2; // ERROR RestrictTraitTo\Class2 | ||
} | ||
|
||
abstract class AbstractClass1 | ||
{ | ||
|
||
} | ||
|
||
class ExtendsAbstractClass1 extends AbstractClass1 { | ||
use UseAnywhere; // OK | ||
use UseOnlyOnInterface1; // ERROR RestrictTraitTo\Interface1 | ||
use UseOnlyOnAbstractClass1; // OK | ||
use UseOnlyOnClass2; // ERROR RestrictTraitTo\Class2 | ||
} | ||
|
||
class Class2 { | ||
use UseAnywhere; // OK | ||
use UseOnlyOnInterface1; // ERROR RestrictTraitTo\Interface1 | ||
use UseOnlyOnAbstractClass1; // ERROR RestrictTraitTo\AbstractClass1 | ||
use UseOnlyOnClass2; // OK | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DaveLiddament\PhpLanguageExtensions; | ||
|
||
/** | ||
* Enforces that the trait can only be used on the specified class, or children of that class. | ||
* | ||
* E.g. this trait is limited to classes that are or extend `Controller` | ||
* | ||
* ``` | ||
* #[RestrictTraitTo(Controller::class)] | ||
* trait ControllerHelpers {} | ||
* ``` | ||
* | ||
* This would be allowed: | ||
* ``` | ||
* class LoginController extends Controller { | ||
* use ControllerHelpers; | ||
* } | ||
* ``` | ||
* | ||
* But this would NOT be allowed: | ||
* ``` | ||
* class Repository { | ||
* use ControllerHelpers; | ||
* } | ||
* ``` | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class RestrictTraitTo | ||
{ | ||
/** @param class-string $className */ | ||
public function __construct( | ||
public string $className, | ||
) { | ||
} | ||
} |