Skip to content

Property with #[Inject] attribute is initialized #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 1.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This extension provides following features:
* Magic [Nette\Object and Nette\SmartObject](https://doc.nette.org/en/2.4/php-language-enhancements) properties
* Event listeners through the `on*` properties
* Defines early terminating method calls for Presenter methods to prevent `Undefined variable` errors
* `@inject` annotation and `#[Nette\DI\Attributes\Inject]` attribute initialize properties

It also contains these framework-specific rules (can be enabled separately):

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"require-dev": {
"nette/application": "^3.0",
"nette/di": "^2.3.0 || ^3.0.0",
"nette/forms": "^3.0",
"nette/utils": "^2.3.0 || ^3.0.0",
"nikic/php-parser": "^4.13.2",
Expand Down
26 changes: 24 additions & 2 deletions src/Rule/Nette/PresenterInjectedPropertiesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

namespace PHPStan\Rule\Nette;

use Nette\DI\Attributes\Inject;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\Properties\ReadWritePropertiesExtension;
use function strpos;

class PresenterInjectedPropertiesExtension implements ReadWritePropertiesExtension
{

/** @var PhpVersion */
private $phpVersion;

public function __construct(PhpVersion $phpVersion)
{
$this->phpVersion = $phpVersion;
}

public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool
{
return false;
Expand All @@ -21,8 +31,20 @@ public function isAlwaysWritten(PropertyReflection $property, string $propertyNa

public function isInitialized(PropertyReflection $property, string $propertyName): bool
{
return $property->isPublic() &&
strpos($property->getDocComment() ?? '', '@inject') !== false;
if (!$property->isPublic()) {
return false;
}

if (strpos($property->getDocComment() ?? '', '@inject') !== false) {
return true;
}

$nativeProperty = $property->getDeclaringClass()->getNativeProperty($propertyName)->getNativeReflection();
if ($this->phpVersion->getVersionId() >= 80000 && $nativeProperty->getAttributes(Inject::class) !== []) {
return true;
}

return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ public function dataTopics(): array
self::markTestSkipped('Only for PHP 7.4+');
}

return [
$topics = [
['presenterInject'],
];

if (PHP_VERSION_ID >= 80000) {
$topics[] = ['presenterInjectAttribute'];
}

return $topics;
}

public function getDataPath(): string
Expand Down
14 changes: 14 additions & 0 deletions tests/Rule/Nette/data/presenterInjectAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Nette\DI\Attributes\Inject;

class Service
{

}

class InjectAttributePresenter
{
#[Inject]
public Service $service;
}