-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathPresenterInjectedPropertiesExtension.php
50 lines (38 loc) · 1.21 KB
/
PresenterInjectedPropertiesExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php declare(strict_types = 1);
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;
}
public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool
{
return $this->isInitialized($property, $propertyName);
}
public function isInitialized(PropertyReflection $property, string $propertyName): bool
{
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;
}
}