Skip to content

Commit

Permalink
@annotations are deprecated (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 20, 2024
1 parent 0d6a547 commit e2c2688
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 44 deletions.
14 changes: 12 additions & 2 deletions src/Application/UI/ComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class ComponentReflection extends \ReflectionClass


/**
* Returns array of class properties that are public and have attribute #[Persistent] or #[Parameter] or annotation @persistent.
* Returns array of class properties that are public and have attribute #[Persistent] or #[Parameter].
* @return array<string, array{def: mixed, type: string, since: ?string}>
*/
public function getParameters(): array
Expand Down Expand Up @@ -75,7 +75,7 @@ public function getParameters(): array


/**
* Returns array of persistent properties. They are public and have attribute #[Persistent] or annotation @persistent.
* Returns array of persistent properties. They are public and have attribute #[Persistent].
* @return array<string, array{def: mixed, type: string, since: string}>
*/
public function getPersistentParams(): array
Expand Down Expand Up @@ -189,6 +189,7 @@ public function getSignalMethod(string $signal): ?\ReflectionMethod

/**
* Returns an annotation value.
* @deprecated
*/
public static function parseAnnotation(\Reflector $ref, string $name): ?array
{
Expand All @@ -206,12 +207,20 @@ public static function parseAnnotation(\Reflector $ref, string $name): ?array
}
}

$alt = match ($name) {
'persistent' => '#[Nette\Application\Attributes\Persistent]',
'deprecated' => '#[Nette\Application\Attributes\Deprecated]',
'crossOrigin' => '#[Nette\Application\Attributes\Request(sameOrigin: false)]',
default => 'alternative'
};
trigger_error("Annotation @$name is deprecated, use $alt (used in " . Nette\Utils\Reflection::toString($ref) . ')', E_USER_DEPRECATED);
return $res;
}


/**
* Has class specified annotation?
* @deprecated
*/
public function hasAnnotation(string $name): bool
{
Expand All @@ -221,6 +230,7 @@ public function hasAnnotation(string $name): bool

/**
* Returns an annotation value.
* @deprecated
*/
public function getAnnotation(string $name): mixed
{
Expand Down
4 changes: 4 additions & 0 deletions src/Application/UI/MethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ final class MethodReflection extends \ReflectionMethod
{
/**
* Has method specified annotation?
* @deprecated
*/
public function hasAnnotation(string $name): bool
{
trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
return (bool) ComponentReflection::parseAnnotation($this, $name);
}


/**
* Returns an annotation value.
* @deprecated
*/
public function getAnnotation(string $name): mixed
{
trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
$res = ComponentReflection::parseAnnotation($this, $name);
return $res ? end($res) : null;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/UI/ComponentReflection.getParameters().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class OnePresenter extends Presenter
public static $no1;
public $no2;

/** @persistent */
#[Persistent]
public $yes1;

#[Persistent, Parameter]
Expand Down
13 changes: 0 additions & 13 deletions tests/UI/ComponentReflection.getPersistentComponents().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ class NonePresenter extends Presenter
}


/**
* @persistent(a, b)
*/
class AnnotationPresenter extends Presenter
{
}


#[Persistent('a', 'b')]
class AttributePresenter extends Presenter
{
Expand All @@ -39,11 +31,6 @@ class MethodPresenter extends AttributePresenter

Assert::same([], NonePresenter::getReflection()->getPersistentComponents());

Assert::same([
'a' => ['since' => AnnotationPresenter::class],
'b' => ['since' => AnnotationPresenter::class],
], AnnotationPresenter::getReflection()->getPersistentComponents());

Assert::same([
'a' => ['since' => AttributePresenter::class],
'b' => ['since' => AttributePresenter::class],
Expand Down
16 changes: 8 additions & 8 deletions tests/UI/ComponentReflection.parseAnnotation.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class TestClass

$rc = new ReflectionClass('TestClass');

Assert::same(['value ="Johno\'s addendum"', 'mode=True', true, true], Reflection::parseAnnotation($rc, 'title'));
Assert::null(Reflection::parseAnnotation($rc, 'public'));
Assert::null(Reflection::parseAnnotation($rc, 'private'));
Assert::same(['item 1'], Reflection::parseAnnotation($rc, 'components'));
Assert::same([true, false, null], Reflection::parseAnnotation($rc, 'persistent'));
Assert::same([true], Reflection::parseAnnotation($rc, 'renderable'));
Assert::same(['loggedIn'], Reflection::parseAnnotation($rc, 'Secured\User'));
Assert::null(Reflection::parseAnnotation($rc, 'missing'));
Assert::same(['value ="Johno\'s addendum"', 'mode=True', true, true], @Reflection::parseAnnotation($rc, 'title'));
Assert::null(@Reflection::parseAnnotation($rc, 'public'));
Assert::null(@Reflection::parseAnnotation($rc, 'private'));
Assert::same(['item 1'], @Reflection::parseAnnotation($rc, 'components'));
Assert::same([true, false, null], @Reflection::parseAnnotation($rc, 'persistent'));
Assert::same([true], @Reflection::parseAnnotation($rc, 'renderable'));
Assert::same(['loggedIn'], @Reflection::parseAnnotation($rc, 'Secured\User'));
Assert::null(@Reflection::parseAnnotation($rc, 'missing'));
17 changes: 9 additions & 8 deletions tests/UI/Presenter.link().persistent.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
declare(strict_types=1);

use Nette\Application;
use Nette\Application\Attributes\Persistent;
use Nette\Http;
use Tester\Assert;

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

trait PersistentParam1
{
/** @persistent */
#[Persistent]
public $t1;
}

trait PersistentParam2A
{
/** @persistent */
#[Persistent]
public $t2;
}

Expand All @@ -33,15 +34,15 @@ trait PersistentParam2B

trait PersistentParam3
{
/** @persistent */
#[Persistent]
public $t3;
}

class BasePresenter extends Application\UI\Presenter
{
use PersistentParam1;

/** @persistent */
#[Persistent]
public $p1;
}

Expand All @@ -50,7 +51,7 @@ class TestPresenter extends BasePresenter
{
use PersistentParam2B;

/** @persistent */
#[Persistent]
public $p2;


Expand Down Expand Up @@ -81,10 +82,10 @@ class SecondPresenter extends BasePresenter
{
use PersistentParam3;

/** @persistent */
#[Persistent]
public $p1 = 20;

/** @persistent */
#[Persistent]
public $p3;
}

Expand All @@ -97,7 +98,7 @@ class ThirdPresenter extends BasePresenter

class FourthPresenter extends BasePresenter
{
#[Application\Attributes\Persistent]
#[Persistent]
public $p1;
}

Expand Down
23 changes: 12 additions & 11 deletions tests/UI/Presenter.link().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
declare(strict_types=1);

use Nette\Application;
use Nette\Application\Attributes\Persistent;
use Nette\Http;
use Tester\Assert;

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

class TestControl extends Application\UI\Control
{
/** @persistent array */
public $order = [];
#[Persistent]
public array $order = [];

/** @persistent int */
public $round = 0;
#[Persistent]
public int $round = 0;


public function handleClick($x, $y)
Expand Down Expand Up @@ -50,22 +51,22 @@ class TestControl extends Application\UI\Control

class TestPresenter extends Application\UI\Presenter
{
/** @persistent */
#[Persistent]
public $p;

/** @persistent */
#[Persistent]
public $pint = 10;

/** @persistent */
#[Persistent]
public $parr = [];

/** @persistent */
#[Persistent]
public $pbool = true;

/** @persistent */
#[Persistent]
public array $parrn;

/** @persistent */
#[Persistent]
public ?bool $pbooln = null;


Expand Down Expand Up @@ -294,7 +295,7 @@ class TestPresenter extends Application\UI\Presenter

class OtherPresenter extends TestPresenter
{
/** @persistent */
#[Persistent]
public $p = 20;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/UI/fixtures/ParamPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ParamPresenter extends Nette\Application\UI\Presenter
{
/** @persistent */
#[Nette\Application\Attributes\Persistent]
public $bool = true;


Expand Down

0 comments on commit e2c2688

Please sign in to comment.