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 Apr 29, 2024
1 parent 7c7362e commit 8c392ab
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public static function prefixServiceName(mixed $config, string $namespace): mixe

/**
* Returns an annotation value.
* @deprecated
*/
public static function parseAnnotation(\Reflector $ref, string $name): ?string
{
Expand All @@ -211,6 +212,8 @@ public static function parseAnnotation(\Reflector $ref, string $name): ?string

$re = '#[\s*]@' . preg_quote($name, '#') . '(?=\s|$)(?:[ \t]+([^@\s]\S*))?#';
if ($ref->getDocComment() && preg_match($re, trim($ref->getDocComment(), '/*'), $m)) {
$alt = $name === 'inject' ? '#[Nette\DI\Attributes\Inject]' : 'alternative';
trigger_error("Annotation @$name is deprecated, use $alt (used in " . Reflection::toString($ref) . ')', E_USER_DEPRECATED);
return $m[1] ?? '';
}

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
22 changes: 9 additions & 13 deletions tests/DI/Helpers.parseAnnotation().phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/**
* Test: Nette\DI\Helpers::expand()
*/

declare(strict_types=1);

use Nette\DI\Helpers;
Expand All @@ -22,10 +18,10 @@ class TestClass

$rc = new ReflectionClass(TestClass::class);

Assert::same('', Helpers::parseAnnotation($rc, 'inject'));
Assert::same(null, Helpers::parseAnnotation($rc, 'injec'));
Assert::same('type', Helpers::parseAnnotation($rc, 'var'));
Assert::same('bool|int', Helpers::parseAnnotation($rc, 'return'));
Assert::same('', @Helpers::parseAnnotation($rc, 'inject'));
Assert::same(null, @Helpers::parseAnnotation($rc, 'injec'));
Assert::same('type', @Helpers::parseAnnotation($rc, 'var'));
Assert::same('bool|int', @Helpers::parseAnnotation($rc, 'return'));


/** @return*/
Expand All @@ -35,7 +31,7 @@ class TestClass2

$rc = new ReflectionClass(TestClass2::class);

Assert::same('', Helpers::parseAnnotation($rc, 'return'));
Assert::same('', @Helpers::parseAnnotation($rc, 'return'));


/** @return
Expand All @@ -47,7 +43,7 @@ class TestClass3

$rc = new ReflectionClass(TestClass3::class);

Assert::same('', Helpers::parseAnnotation($rc, 'return'));
Assert::same('', @Helpers::parseAnnotation($rc, 'return'));


/**
Expand All @@ -59,6 +55,6 @@ class TestClass4

$rc = new ReflectionClass(TestClass4::class);

Assert::same(null, Helpers::parseAnnotation($rc, 'inject'));
Assert::same(null, Helpers::parseAnnotation($rc, 'injec'));
Assert::same(null, Helpers::parseAnnotation($rc, 'var'));
Assert::same(null, @Helpers::parseAnnotation($rc, 'inject'));
Assert::same(null, @Helpers::parseAnnotation($rc, 'injec'));
Assert::same(null, @Helpers::parseAnnotation($rc, 'var'));
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 8c392ab

Please sign in to comment.