Skip to content

Commit

Permalink
AutowiredProperties: add support for property typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Jun 17, 2020
1 parent 3abd047 commit 8172f24
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ install:
script:
- vendor/bin/tester $COVERAGE -s -p ${TESTER_RUNTIME:-php} -c ./tests/php.ini-unix ./tests/KdybyTests/
- php vendor/bin/phpstan analyse --ansi --no-progress
- php /tmp/php-parallel-lint/parallel-lint.php -e php,phpt --exclude vendor .
- php /tmp/php-parallel-lint/parallel-lint.php -e php,phpt --exclude vendor --exclude tests/KdybyTests/Autowired/mocks/Php74PropertyTypesPresenter.php .

after_script:
- if [ "$COVERAGE" != "" ]; then php /tmp/coveralls.phar --verbose --config tests/.coveralls.yml || true; fi
Expand Down
15 changes: 15 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ parameters:
count: 1
path: tests/KdybyTests/Autowired/mocks/IntegrationPresenter.php

-
message: "#^Call to an undefined method Nette\\\\Reflection\\\\Property\\:\\:hasType\\(\\)\\.$#"
count: 1
path: tests/KdybyTests/Autowired/mocks/IntegrationPresenter.php

-
message: "#^Call to an undefined method Nette\\\\Reflection\\\\Property\\:\\:getType\\(\\)\\.$#"
count: 1
path: tests/KdybyTests/Autowired/mocks/IntegrationPresenter.php

-
message: "#^Call to an undefined method ReflectionType\\:\\:getName\\(\\)\\.$#"
count: 1
path: tests/KdybyTests/Autowired/mocks/IntegrationPresenter.php

-
message: "#^Method KdybyTests\\\\Autowired\\\\IntegrationPresenter\\:\\:createComponentSilly\\(\\) has no return typehint specified\\.$#"
count: 1
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ parameters:
# $this in Trait is resolved as NEVER
- '#Instanceof between \*NEVER\* and Nette\\Application\\UI\\Component will always evaluate to false#'

excludes_analyse:
- tests/KdybyTests/Autowired/mocks/Php74PropertyTypesPresenter.php # incompatible with PHP <7.4

reportUnmatchedIgnoredErrors: false

includes:
- vendor/phpstan/phpstan-nette/extension.neon
- vendor/phpstan/phpstan-nette/rules.neon
Expand Down
21 changes: 18 additions & 3 deletions src/Kdyby/Autowired/AutowireProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ private function findByTypeForProperty(string $type)
*/
private function resolveProperty(Property $prop): void
{
/** @var Nette\Reflection\Annotation $propAnnotation */
$propAnnotation = $prop->getAnnotation('var');
$type = $this->resolveAnnotationClass($prop, (string) $propAnnotation, 'var');
$type = $this->resolvePropertyType($prop);
$metadata = [
'value' => NULL,
'type' => $type,
Expand Down Expand Up @@ -177,6 +175,23 @@ private function resolveProperty(Property $prop): void



private function resolvePropertyType(Property $prop): string
{
if (PHP_VERSION_ID >= 70400 && $prop->hasType()) {
$type = $prop->getType()->getName();
if (!class_exists($type) && !interface_exists($type)) {
throw new MissingClassException("Class \"{$type}\" not found, please check the typehint on {$prop}.", $prop);
}
return $type;
}

/** @var Nette\Reflection\Annotation $propAnnotation */
$propAnnotation = $prop->getAnnotation('var');
return $this->resolveAnnotationClass($prop, (string) $propAnnotation, 'var');
}



/**
* @param Property|Method $prop
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/KdybyTests/Autowired/AutowireProperties.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use Nette\DI;
use Nette\PhpGenerator\PhpLiteral;
use Tester\Assert;
use KdybyTests\Autowired\UseExpansion\ImportedService as AliasedService;
use Tester\Environment;


require_once __DIR__ . '/../bootstrap.php';

Expand Down Expand Up @@ -151,6 +153,20 @@ class AutowirePropertiesTest extends ContainerTestCase
Assert::true($presenter->service instanceof SampleService);
}


public function testTypedProperty(): void
{
if (PHP_VERSION_ID < 70400) {
Environment::skip('Typed properties only in PHP >= 7.4');
}

require_once __DIR__ . '/mocks/Php74PropertyTypesPresenter.php';
$presenter = new Php74PropertyTypesPresenter();

$this->container->callMethod([$presenter, 'injectProperties']);
Assert::type(SampleService::class, $presenter->service);
}

}


Expand Down
1 change: 1 addition & 0 deletions tests/KdybyTests/Autowired/Extension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ExtensionTest extends Tester\TestCase
{
$config = new Nette\Configurator();
$config->setTempDirectory(TEMP_DIR);
$config->addConfig(__DIR__ . '/../config/application.neon');
Kdyby\Autowired\DI\AutowiredExtension::register($config);
$config->createContainer(); // init panel

Expand Down
18 changes: 18 additions & 0 deletions tests/KdybyTests/Autowired/mocks/Php74PropertyTypesPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace KdybyTests\Autowired;

use Kdyby;
use Nette;

class Php74PropertyTypesPresenter extends Nette\Application\UI\Presenter
{

use Kdyby\Autowired\AutowireProperties;

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

}
1 change: 1 addition & 0 deletions tests/KdybyTests/ContainerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected function compileContainer(?string $configFile = null): Container
$configurator = new Configurator;
$configurator->setTempDirectory(TEMP_DIR);
$configurator->addParameters(['container' => ['class' => 'SystemContainer_'.md5(TEMP_DIR)]]);
$configurator->addConfig(__DIR__ . '/config/application.neon');

if ($configFile !== null) {
$configurator->addConfig(__DIR__ . '/config/' . $configFile . '.neon');
Expand Down
2 changes: 2 additions & 0 deletions tests/KdybyTests/config/application.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
application:
scanDirs: false # prevent autoloading of Php74PropertyTypesPresenter incompatible with PHP <7.4

0 comments on commit 8172f24

Please sign in to comment.