From 9cd5118ab910dc009b921f08b4ccf54bff89dfc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mor=C3=A1vek?= Date: Sun, 11 Sep 2022 09:06:55 +0200 Subject: [PATCH] Deprecate phpdoc annotations in favor of native PHP attributes --- docs/en/index.md | 40 +++++++++---------- src/Kdyby/Autowired/AutowireProperties.php | 1 + .../AnnotationPresenter.php | 20 ++++++++++ .../DeprecationsFixtures/SampleService.php | 9 +++++ .../Autowired/DeprecationsTest.phpt | 15 +++++++ .../IntegrationPresenter.php | 9 ++--- tests/KdybyTests/config/deprecations.neon | 8 ++++ 7 files changed, 76 insertions(+), 26 deletions(-) create mode 100644 tests/KdybyTests/Autowired/DeprecationsFixtures/AnnotationPresenter.php create mode 100644 tests/KdybyTests/Autowired/DeprecationsFixtures/SampleService.php create mode 100644 tests/KdybyTests/config/deprecations.neon diff --git a/docs/en/index.md b/docs/en/index.md index b54ddf8..11895ac 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -42,7 +42,7 @@ abstract class BasePresenter extends Nette\Application\UI\Presenter Autowired properties -------------------- -Every `protected` or `public` property marked with `@autowire` annotation will be under control of `AutowireProperties` trait, we will call them "autowired properties". +Every `protected` or `public` property marked with `#[Autowire]` attribute will be under control of `AutowireProperties` trait, we will call them "autowired properties". The properties are analysed and result of the analysis is cached. This means, that you will see errors in your configuration instantly, and not after you use it in some conditional code, that might not even be executed every time. This is here to help you find errors, as early as possible. @@ -52,41 +52,41 @@ This behaviour is inspired by article [DI and property injection](http://phpfash ```php +use Kdyby\Autowired\Attributes\Autowire; +use Kdyby\Doctrine\EntityDao; +use Kdyby\Doctrine\EntityDaoFactory; +use App\Article; +use App\ArticleRepository; + class ArticlePresenter extends BasePresenter { - /** - * @autowire - */ - protected App\ArticleRepository $articleRepository; + #[Autowire] + protected ArticleRepository $articleRepository; - /** - * @autowire(MyApp\Blog\Article, factory=\Kdyby\Doctrine\EntityDaoFactory) - */ - protected Kdyby\Doctrine\EntityDao $factoryResult; + #[Autowire(factory: EntityDaoFactory::class, arguments: [Article::class])] + protected EntityDao $factoryResult; // .. } ``` -Or using PHP 8 attributes: +Or by using phpdoc annotation (deprecated): ```php -use Kdyby\Autowired\Attributes\Autowire; -use Kdyby\Doctrine\EntityDao; -use Kdyby\Doctrine\EntityDaoFactory; -use App\Article; -use App\ArticleRepository; - class ArticlePresenter extends BasePresenter { - #[Autowire] - protected ArticleRepository $articleRepository; + /** + * @autowire + */ + protected App\ArticleRepository $articleRepository; - #[Autowire(factory: EntityDaoFactory::class, arguments: [Article::class])] - protected EntityDao $factoryResult; + /** + * @autowire(MyApp\Blog\Article, factory=\Kdyby\Doctrine\EntityDaoFactory) + */ + protected Kdyby\Doctrine\EntityDao $factoryResult; // .. diff --git a/src/Kdyby/Autowired/AutowireProperties.php b/src/Kdyby/Autowired/AutowireProperties.php index 3d33509..dfdf722 100644 --- a/src/Kdyby/Autowired/AutowireProperties.php +++ b/src/Kdyby/Autowired/AutowireProperties.php @@ -163,6 +163,7 @@ private function resolveAutowireMetadata(\ReflectionProperty $property): ?array unset($annotationParameters['factory']); $metadata['arguments'] = array_values($annotationParameters); } + trigger_error('@autowire annotation is deprecated, migrate to native PHP attributes.', E_USER_DEPRECATED); } } diff --git a/tests/KdybyTests/Autowired/DeprecationsFixtures/AnnotationPresenter.php b/tests/KdybyTests/Autowired/DeprecationsFixtures/AnnotationPresenter.php new file mode 100644 index 0000000..928dc42 --- /dev/null +++ b/tests/KdybyTests/Autowired/DeprecationsFixtures/AnnotationPresenter.php @@ -0,0 +1,20 @@ +compileContainer('deprecations'); + $presenter = new AnnotationPresenter(); + + Assert::error( + function () use ($container, $presenter): void { + $container->callMethod([$presenter, 'injectProperties']); + }, + E_USER_DEPRECATED, + '@autowire annotation is deprecated, migrate to native PHP attributes.', + ); + } + } (new DeprecationsTest())->run(); diff --git a/tests/KdybyTests/Autowired/IntegrationFixtures/IntegrationPresenter.php b/tests/KdybyTests/Autowired/IntegrationFixtures/IntegrationPresenter.php index fe9be08..b32bd68 100644 --- a/tests/KdybyTests/Autowired/IntegrationFixtures/IntegrationPresenter.php +++ b/tests/KdybyTests/Autowired/IntegrationFixtures/IntegrationPresenter.php @@ -4,6 +4,7 @@ namespace KdybyTests\Autowired\IntegrationFixtures; use Kdyby; +use Kdyby\Autowired\Attributes\Autowire; use Nette; class IntegrationPresenter extends Nette\Application\UI\Presenter @@ -12,14 +13,10 @@ class IntegrationPresenter extends Nette\Application\UI\Presenter use Kdyby\Autowired\AutowireProperties; use Kdyby\Autowired\AutowireComponentFactories; - /** - * @autowire - */ + #[Autowire] public LoremService $service; - /** - * @autowire(factory=\KdybyTests\Autowired\IntegrationFixtures\DatagridFactory) - */ + #[Autowire(factory: DatagridFactory::class)] public DatagridComponent $factoryResult; protected function createComponentSilly(DatagridFactory $factory): DatagridComponent diff --git a/tests/KdybyTests/config/deprecations.neon b/tests/KdybyTests/config/deprecations.neon new file mode 100644 index 0000000..dab7b9a --- /dev/null +++ b/tests/KdybyTests/config/deprecations.neon @@ -0,0 +1,8 @@ +extensions: + autowired: Kdyby\Autowired\DI\AutowiredExtension + +autowired: + cacheStorage: KdybyTests\TestStorage + +services: + - KdybyTests\Autowired\DeprecationsFixtures\SampleService