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 29, 2022
1 parent 4d3ada0 commit 735c822
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions build/ignore-by-php-version.neon.php
Expand Up @@ -13,6 +13,7 @@
$includes[] = __DIR__ . '/baseline-8.1.neon';
} else {
$includes[] = __DIR__ . '/enums.neon';
$includes[] = __DIR__ . '/readonly-property.neon';
}

if (PHP_VERSION_ID >= 70400) {
Expand Down
3 changes: 3 additions & 0 deletions build/readonly-property.neon
@@ -0,0 +1,3 @@
parameters:
excludePaths:
- ../tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php
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 735c822

Please sign in to comment.