Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Node/ClassPropertiesNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,9 @@ public function getUninitializedProperties(
$initializedInConstructor = array_diff_key($uninitializedProperties, $this->collectUninitializedProperties([$classReflection->getConstructor()->getName()], $uninitializedProperties));
}

$methodsCalledFromConstructor = $this->getMethodsCalledFromConstructor($classReflection, $initialInitializedProperties, $initializedProperties, $constructors, $initializedInConstructor);
$methodsCalledFromConstructor = $this->getMethodsCalledFromConstructor($classReflection, $initialInitializedProperties, $initializedProperties, $constructors, $initializedInConstructor, $constructors);
$prematureAccess = [];
$additionalAssigns = [];

foreach ($this->getPropertyUsages() as $usage) {
$fetch = $usage->getFetch();
if (!$fetch instanceof PropertyFetch) {
Expand Down Expand Up @@ -211,7 +210,10 @@ public function getUninitializedProperties(

if ($usage instanceof PropertyWrite) {
if (array_key_exists($propertyName, $initializedPropertiesMap)) {
$hasInitialization = $initializedPropertiesMap[$propertyName]->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName)));
$hasInitialization = $initializedPropertiesMap[$propertyName];
if (in_array($function->getName(), $constructors, true)) {
$hasInitialization = $hasInitialization->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName)));
}
if (
!$hasInitialization->no()
&& !$usage->isPromotedPropertyWrite()
Expand Down Expand Up @@ -318,6 +320,7 @@ private function collectUninitializedProperties(array $constructors, array $unin
* @param array<string, TrinaryLogic> $initialInitializedProperties
* @param array<string, array<string, TrinaryLogic>> $initializedProperties
* @param array<string, ClassPropertyNode> $initializedInConstructorProperties
* @param string[] $originalConstructors
*
* @return array<string, array<string, TrinaryLogic>>
*/
Expand All @@ -327,6 +330,7 @@ private function getMethodsCalledFromConstructor(
array $initializedProperties,
array $methods,
array $initializedInConstructorProperties,
array $originalConstructors,
): array
{
$originalMap = $initializedProperties;
Expand Down Expand Up @@ -363,7 +367,7 @@ private function getMethodsCalledFromConstructor(
continue;
}

if ($inMethod->getName() !== '__construct') {
if ($inMethod->getName() !== '__construct' && in_array($inMethod->getName(), $originalConstructors, true)) {
foreach (array_keys($initializedInConstructorProperties) as $propertyName) {
$initializedProperties[$inMethod->getName()][$propertyName] = TrinaryLogic::createYes();
}
Expand Down Expand Up @@ -391,7 +395,7 @@ private function getMethodsCalledFromConstructor(
return $initializedProperties;
}

return $this->getMethodsCalledFromConstructor($classReflection, $initialInitializedProperties, $initializedProperties, $methods, $initializedInConstructorProperties);
return $this->getMethodsCalledFromConstructor($classReflection, $initialInitializedProperties, $initializedProperties, $methods, $initializedInConstructorProperties, $originalConstructors);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected function getRule(): Rule
'Bug10523\\Controller::init',
'Bug10523\\MultipleWrites::init',
'Bug10523\\SingleWriteInConstructorCalledMethod::init',
'Bug12253\\PayloadWithAdditionalConstructor::setUp',
],
),
);
Expand Down Expand Up @@ -342,4 +343,10 @@ public function testBug11828(): void
$this->analyse([__DIR__ . '/data/bug-11828.php'], []);
}

#[RequiresPhp('>= 8.4')]
public function testBug12253(): void
{
$this->analyse([__DIR__ . '/data/bug-12253.php'], []);
}

}
131 changes: 131 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-12253.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php // lint >= 8.4

declare(strict_types = 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add another test with the same problem, but 1 additionalConstructors configured

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're out of extra usage · resets 5pm (UTC)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add another test with the same problem, but 1 additionalConstructors configured

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I've addressed staabm's review by adding a PayloadWithAdditionalConstructor test class to bug-12253.php that reproduces the same readonly property false positive scenario but with setUp() configured as an additional constructor via ConstructorsHelper. The test verifies no false positive is reported when a helper method (called from the additional constructor) assigns a readonly property while also calling other methods.


namespace Bug12253;

use stdClass;

class Payload
{
/** @var array<array<string, mixed>> */
private(set) readonly array $validation;

/** @var array<string, string> */
private array $ids = [];

public function __construct(private readonly stdClass $payload)
{
$this->parseValidation();
}

private function parseValidation(): void
{
$validations = [];

foreach ($this->payload->validation as $key => $validation) {
$validations[] = [
'id' => $key,
'field_id' => $this->ids[$validation->field_id],
'rule' => $validation->rule,
'value' => $this->validationValue($validation->value),
'message' => $validation->message,
];
}

$this->validation = $validations;
}

private function validationValue(mixed $value): mixed
{
if (is_null($value)) {
return null;
}

return $this->ids[$value] ?? $value;
}
}

class PayloadWithAdditionalConstructor
{
/** @var array<array<string, mixed>> */
private(set) readonly array $validation;

/** @var array<string, string> */
private array $ids = [];

public function __construct(private readonly stdClass $payload)
{
}

public function setUp(): void
{
$this->parseValidation();
}

private function parseValidation(): void
{
$validations = [];

foreach ($this->payload->validation as $key => $validation) {
$validations[] = [
'id' => $key,
'field_id' => $this->ids[$validation->field_id],
'rule' => $validation->rule,
'value' => $this->validationValue($validation->value),
'message' => $validation->message,
];
}

$this->validation = $validations;
}

private function validationValue(mixed $value): mixed
{
if (is_null($value)) {
return null;
}

return $this->ids[$value] ?? $value;
}
}

class PayloadWithoutAsymmetricVisibility
{
/** @var array<array<string, mixed>> */
private readonly array $validation;

/** @var array<string, string> */
private array $ids = [];

public function __construct(private readonly stdClass $payload)
{
$this->parseValidation();
}

private function parseValidation(): void
{
$validations = [];

foreach ($this->payload->validation as $key => $validation) {
$validations[] = [
'id' => $key,
'field_id' => $this->ids[$validation->field_id],
'rule' => $validation->rule,
'value' => $this->validationValue($validation->value),
'message' => $validation->message,
];
}

$this->validation = $validations;
}

private function validationValue(mixed $value): mixed
{
if (is_null($value)) {
return null;
}

return $this->ids[$value] ?? $value;
}
}
Loading