From 23fdaa4d122a25a5f15354c8084d3fc89b1fe88b Mon Sep 17 00:00:00 2001 From: Dave Liddament Date: Thu, 8 Aug 2024 11:34:19 +0100 Subject: [PATCH] ADD RestrictTraitTo --- README.md | 28 ++++++++++ examples/restrictTraitTo/restrictTraitTo.php | 56 ++++++++++++++++++++ src/RestrictTraitTo.php | 39 ++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 examples/restrictTraitTo/restrictTraitTo.php create mode 100644 src/RestrictTraitTo.php diff --git a/README.md b/README.md index 1b7d511..389ea39 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ The intention, at least initially, is that these extra language features are enf - [NamespaceVisibility](#namespaceVisibility) - [InjectableVersion](#injectableVersion) - [Override](#override) +- [RestrictTraitTo](#restricttraitto) - [Sealed](#sealed) - [TestTag](#testtag) @@ -34,6 +35,7 @@ The intention, at least initially, is that these extra language features are enf - [NamespaceVisibility](#namespaceVisibility) - [InjectableVersion](#injectableVersion) - [Override](#override) + - [RestrictTraitTo](#restricttraitto) - [Sealed](#sealed) - [TestTag](#testtag) - Deprecated @@ -417,6 +419,32 @@ NOTE: - If you are using PHP 8.3 then use the real `#[Override]` attribute. - This implementation doesn't consider traits. +## RestrictTraitTo + +This limits the use of a Trait to only be used by a specified class of a child of that class. + +E.g. this trait is limited to classes that are or extend `Controller` + +```php +#[RestrictTraitTo(Controller::class)] +trait ControllerHelpers {} +``` + +This would be allowed: +```php +class LoginController extends Controller { + use ControllerHelpers; +} +``` + +But this would NOT be allowed: +```php +class Repository { + use ControllerHelpers; +} +``` + + ## Sealed This is inspired by the rejected [sealed classes RFC](https://wiki.php.net/rfc/sealed_classes) diff --git a/examples/restrictTraitTo/restrictTraitTo.php b/examples/restrictTraitTo/restrictTraitTo.php new file mode 100644 index 0000000..cab54fd --- /dev/null +++ b/examples/restrictTraitTo/restrictTraitTo.php @@ -0,0 +1,56 @@ +