Skip to content

Commit

Permalink
annotations @Inject is deprecated (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 29, 2024
1 parent 8e6eb57 commit b308c9b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/DI/Extensions/InjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public static function getInjectProperties(string $class): array
$rp->getAttributes(DI\Attributes\Inject::class)
|| DI\Helpers::parseAnnotation($rp, 'inject') !== null
) {
if (!$rp->getAttributes(DI\Attributes\Inject::class)) {
trigger_error('Annotation @inject is deprecated, use #[Nette\\DI\\Attributes\\Inject] (used in ' . Reflection::toString($rp) . ')', E_USER_DEPRECATED);
}
if (!$rp->isPublic() || $rp->isStatic() || $rp->isReadOnly()) {
throw new Nette\InvalidStateException(sprintf('Property %s for injection must not be static, readonly and must be public.', Reflection::toString($rp)));
}
Expand Down
7 changes: 4 additions & 3 deletions tests/DI/Container.inject.properties.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
declare(strict_types=1);

use Nette\DI;
use Nette\DI\Attributes\Inject;
use Tester\Assert;


Expand All @@ -23,16 +24,16 @@ class Foo implements IFoo

class Test1
{
/** @inject */
#[Inject]
public stdClass $varA;
}

class Test2 extends Test1
{
/** @inject */
#[Inject]
public stdClass $varC;

/** @inject */
#[Inject]
public IFoo $varD;
}

Expand Down
7 changes: 4 additions & 3 deletions tests/DI/InjectExtension.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
declare(strict_types=1);

use Nette\DI;
use Nette\DI\Attributes\Inject;
use Nette\DI\Definitions\Reference;
use Nette\DI\Definitions\Statement;
use Tester\Assert;
Expand All @@ -31,7 +32,7 @@ class ConcreteDependencyB extends AbstractDependency

class ParentClass
{
/** @inject */
#[Inject]
public stdClass $a;


Expand All @@ -47,10 +48,10 @@ class ParentClass

class Service extends ParentClass
{
/** @inject */
#[Inject]
public stdClass $c;

/** @inject */
#[Inject]
public AbstractDependency $e;


Expand Down
11 changes: 6 additions & 5 deletions tests/DI/InjectExtension.errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
declare(strict_types=1);

use Nette\DI;
use Nette\DI\Attributes\Inject;
use Nette\InvalidStateException;
use Tester\Assert;

Expand All @@ -16,35 +17,35 @@ require __DIR__ . '/../bootstrap.php';

class ServiceA
{
/** @inject */
#[Inject]
public DateTimeImmutable $a;
}


class ServiceB
{
/** @inject */
#[Inject]
public Unknown $a;
}


class ServiceC
{
/** @inject */
#[Inject]
public $a;
}


class ServiceD
{
/** @inject */
#[Inject]
protected $a;
}


class ServiceE
{
/** @inject */
#[Inject]
public static $a;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/DI/InjectExtension.getInjectProperties().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use Tester\Assert;

class AClass
{
/** @inject */
#[Inject]
public AInjected $varA;

/** @inject */
#[Inject]
public BInjected $varB;

public $varD;
Expand All @@ -27,7 +27,7 @@ class AClass

class BadClass
{
/** @inject */
#[Inject]
public AClass|stdClass $var;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/DI/InjectExtension.getInjectProperties().traits.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ namespace A
namespace B
{
use A\AInjected;
use Nette\DI\Attributes\Inject;

trait BTrait
{
/** @inject */
#[Inject]
public AInjected $varA;
}
}
Expand Down

0 comments on commit b308c9b

Please sign in to comment.