Skip to content

Commit

Permalink
Use ReadWritePropertiesExtensions in MissingReadOnlyPropertyAssignRule
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed May 28, 2022
1 parent 64652cb commit acaeb2b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
Expand Up @@ -19,6 +19,7 @@ class MissingReadOnlyByPhpDocPropertyAssignRule implements Rule

public function __construct(
private ConstructorsHelper $constructorsHelper,
private ReadWritePropertiesExtensionProvider $extensionProvider,
)
{
}
Expand All @@ -34,7 +35,7 @@ public function processNode(Node $node, Scope $scope): array
throw new ShouldNotHappenException();
}
$classReflection = $scope->getClassReflection();
[$properties, $prematureAccess, $additionalAssigns] = $node->getUninitializedProperties($scope, $this->constructorsHelper->getConstructors($classReflection), []);
[$properties, $prematureAccess, $additionalAssigns] = $node->getUninitializedProperties($scope, $this->constructorsHelper->getConstructors($classReflection), $this->extensionProvider->getExtensions());

$errors = [];
foreach ($properties as $propertyName => $propertyNode) {
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Properties/MissingReadOnlyPropertyAssignRule.php
Expand Up @@ -19,6 +19,7 @@ class MissingReadOnlyPropertyAssignRule implements Rule

public function __construct(
private ConstructorsHelper $constructorsHelper,
private ReadWritePropertiesExtensionProvider $extensionProvider,
)
{
}
Expand All @@ -34,7 +35,7 @@ public function processNode(Node $node, Scope $scope): array
throw new ShouldNotHappenException();
}
$classReflection = $scope->getClassReflection();
[$properties, $prematureAccess, $additionalAssigns] = $node->getUninitializedProperties($scope, $this->constructorsHelper->getConstructors($classReflection), []);
[$properties, $prematureAccess, $additionalAssigns] = $node->getUninitializedProperties($scope, $this->constructorsHelper->getConstructors($classReflection), $this->extensionProvider->getExtensions());

$errors = [];
foreach ($properties as $propertyName => $propertyNode) {
Expand Down
Expand Up @@ -3,8 +3,10 @@
namespace PHPStan\Rules\Properties;

use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use function in_array;
use const PHP_VERSION_ID;

/**
Expand All @@ -19,6 +21,32 @@ protected function getRule(): Rule
new ConstructorsHelper([
'MissingReadOnlyPropertyAssignPhpDoc\\TestCase::setUp',
]),
new DirectReadWritePropertiesExtensionProvider([
new class() implements ReadWritePropertiesExtension {

public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool
{
return $this->isEntityId($property, $propertyName);
}

public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool
{
return $this->isEntityId($property, $propertyName);
}

public function isInitialized(PropertyReflection $property, string $propertyName): bool
{
return $this->isEntityId($property, $propertyName);
}

private function isEntityId(PropertyReflection $property, string $propertyName): bool
{
return $property->getDeclaringClass()->getName() === 'MissingReadOnlyPropertyAssignPhpDoc\\Entity'
&& in_array($propertyName, ['id'], true);
}

},
]),
);
}

Expand Down
Expand Up @@ -3,8 +3,10 @@
namespace PHPStan\Rules\Properties;

use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use function in_array;
use const PHP_VERSION_ID;

/**
Expand All @@ -19,6 +21,32 @@ protected function getRule(): Rule
new ConstructorsHelper([
'MissingReadOnlyPropertyAssign\\TestCase::setUp',
]),
new DirectReadWritePropertiesExtensionProvider([
new class() implements ReadWritePropertiesExtension {

public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool
{
return $this->isEntityId($property, $propertyName);
}

public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool
{
return $this->isEntityId($property, $propertyName);
}

public function isInitialized(PropertyReflection $property, string $propertyName): bool
{
return $this->isEntityId($property, $propertyName);
}

private function isEntityId(PropertyReflection $property, string $propertyName): bool
{
return $property->getDeclaringClass()->getName() === 'MissingReadOnlyPropertyAssign\\Entity'
&& in_array($propertyName, ['id'], true);
}

},
]),
);
}

Expand Down
Expand Up @@ -196,3 +196,12 @@ public function __construct()
}

}


class Entity
{

/** @readonly */
private int $id; // does not complain about being uninitialized because of a ReadWritePropertiesExtension

}
Expand Up @@ -153,3 +153,10 @@ public function __construct(
}

}

class Entity
{

private readonly int $id; // does not complain about being uninitialized because of a ReadWritePropertiesExtension

}

0 comments on commit acaeb2b

Please sign in to comment.