Skip to content

Commit

Permalink
Deprecate support for non-typed autowired properties
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Sep 11, 2022
1 parent 6482673 commit 8743fbb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Kdyby/Autowired/AutowireProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ private function resolvePropertyType(\ReflectionProperty $prop): string
$varType = Nette\DI\Helpers::parseAnnotation($prop, 'var');
if ($varType !== NULL && $varType !== '') {
$type = Reflection::expandClassName($varType, Reflection::getPropertyDeclaringClass($prop));
trigger_error(sprintf('Resolving property type from @var annotation is deprecated, change %s to a typed property.', Reflection::toString($prop)), E_USER_DEPRECATED);
}
}

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

namespace KdybyTests\Autowired\DeprecationsFixtures;

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


class NonTypedPropertyPresenter extends Nette\Application\UI\Presenter
{

use Kdyby\Autowired\AutowireProperties;

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
* @var SampleService
*/
#[Autowire]
public $service;

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

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

public function testNonTypedProperty(): void
{
$container = $this->compileContainer('deprecations');
$presenter = new NonTypedPropertyPresenter();

Assert::error(
function () use ($container, $presenter): void {
$container->callMethod([$presenter, 'injectProperties']);
},
E_USER_DEPRECATED,
'Resolving property type from @var annotation is deprecated, change KdybyTests\Autowired\DeprecationsFixtures\NonTypedPropertyPresenter::$service to a typed property.',
);
}

}

(new DeprecationsTest())->run();

0 comments on commit 8743fbb

Please sign in to comment.