Skip to content

Commit

Permalink
Deprecate phpdoc annotations in favor of native PHP attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Sep 11, 2022
1 parent 0513fed commit 9cd5118
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 26 deletions.
40 changes: 20 additions & 20 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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;

// ..

Expand Down
1 change: 1 addition & 0 deletions src/Kdyby/Autowired/AutowireProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace KdybyTests\Autowired\DeprecationsFixtures;

use Kdyby;
use Nette;


class AnnotationPresenter extends Nette\Application\UI\Presenter
{

use Kdyby\Autowired\AutowireProperties;

/**
* @autowire
*/
public SampleService $typedService;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace KdybyTests\Autowired\DeprecationsFixtures;

final class SampleService
{

}
15 changes: 15 additions & 0 deletions tests/KdybyTests/Autowired/DeprecationsTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare(strict_types=1);

namespace KdybyTests\Autowired;

use KdybyTests\Autowired\DeprecationsFixtures\AnnotationPresenter;
use KdybyTests\Autowired\DeprecationsFixtures\SimplePresenter;
use KdybyTests\ContainerTestCase;
use Tester\Assert;
Expand Down Expand Up @@ -37,6 +38,20 @@ final class DeprecationsTest extends ContainerTestCase
);
}

public function testAnnotations(): void
{
$container = $this->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();
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace KdybyTests\Autowired\IntegrationFixtures;

use Kdyby;
use Kdyby\Autowired\Attributes\Autowire;
use Nette;

class IntegrationPresenter extends Nette\Application\UI\Presenter
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/KdybyTests/config/deprecations.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extensions:
autowired: Kdyby\Autowired\DI\AutowiredExtension

autowired:
cacheStorage: KdybyTests\TestStorage

services:
- KdybyTests\Autowired\DeprecationsFixtures\SampleService

0 comments on commit 9cd5118

Please sign in to comment.