Skip to content
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

Use ReadWritePropertiesExtensions in MissingReadOnlyPropertyAssignRule #1357

Merged
merged 2 commits into from May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

}