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
3 changes: 2 additions & 1 deletion src/Node/ClassPropertiesNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getClassReflection(): ClassReflection
/**
* @param string[] $constructors
* @param ReadWritePropertiesExtension[]|null $extensions
* @return array{array<string, ClassPropertyNode>, array<array{string, int, ClassPropertyNode}>, array<array{string, int, ClassPropertyNode}>}
* @return array{array<string, ClassPropertyNode>, array<array{string, int, ClassPropertyNode, string}>, array<array{string, int, ClassPropertyNode}>}
*/
public function getUninitializedProperties(
Scope $scope,
Expand Down Expand Up @@ -212,6 +212,7 @@ public function getUninitializedProperties(
$propertyName,
$fetch->getLine(),
$originalProperties[$propertyName],
$usageScope->getFileDescription(),
];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public function processNode(Node $node, Scope $scope): array
))->line($propertyNode->getLine())->build();
}

foreach ($prematureAccess as [$propertyName, $line, $propertyNode]) {
foreach ($prematureAccess as [$propertyName, $line, $propertyNode, $file]) {
if (!$propertyNode->isReadOnlyByPhpDoc() || $propertyNode->isReadOnly()) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'Access to an uninitialized @readonly property %s::$%s.',
$classReflection->getDisplayName(),
$propertyName,
))->line($line)->build();
))->line($line)->file($file)->build();
}

foreach ($additionalAssigns as [$propertyName, $line, $propertyNode]) {
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Properties/MissingReadOnlyPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public function processNode(Node $node, Scope $scope): array
))->line($propertyNode->getLine())->build();
}

foreach ($prematureAccess as [$propertyName, $line, $propertyNode]) {
foreach ($prematureAccess as [$propertyName, $line, $propertyNode, $file]) {
if (!$propertyNode->isReadOnly()) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'Access to an uninitialized readonly property %s::$%s.',
$classReflection->getDisplayName(),
$propertyName,
))->line($line)->build();
))->line($line)->file($file)->build();
}

foreach ($additionalAssigns as [$propertyName, $line, $propertyNode]) {
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Properties/UninitializedPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public function processNode(Node $node, Scope $scope): array
))->line($propertyNode->getLine())->build();
}

foreach ($prematureAccess as [$propertyName, $line, $propertyNode]) {
foreach ($prematureAccess as [$propertyName, $line, $propertyNode, $file]) {
if ($propertyNode->isReadOnly() || $propertyNode->isReadOnlyByPhpDoc()) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'Access to an uninitialized property %s::$%s.',
$classReflection->getDisplayName(),
$propertyName,
))->line($line)->build();
))->line($line)->file($file)->build();
}

return $errors;
Expand Down
47 changes: 47 additions & 0 deletions tests/PHPStan/Analyser/AnalyserTraitsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use PHPStan\File\FileHelper;
use PHPStan\Testing\PHPStanTestCase;
use function array_map;
use function array_merge;
use function array_unique;
use function sprintf;
use function usort;
use const PHP_VERSION_ID;

class AnalyserTraitsIntegrationTest extends PHPStanTestCase
{
Expand Down Expand Up @@ -165,6 +169,36 @@ public function testMissingReturnInAbstractTraitMethod(): void
$this->assertNoErrors($errors);
}

public function testUnititializedReadonlyPropertyAccessedInTrait(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped();
}

$errors = $this->runAnalyse([
__DIR__ . '/traits/uninitializedProperty/FooClass.php',
__DIR__ . '/traits/uninitializedProperty/FooTrait.php',
]);
$this->assertCount(3, $errors);
usort($errors, static fn (Error $a, Error $b) => $a->getLine() <=> $b->getLine());
$expectedFile = sprintf('%s (in context of class TraitsUnititializedProperty\FooClass)', $this->fileHelper->normalizePath(__DIR__ . '/traits/uninitializedProperty/FooTrait.php'));

$error = $errors[0];
$this->assertSame('Access to an uninitialized readonly property TraitsUnititializedProperty\FooClass::$x.', $error->getMessage());
$this->assertSame(15, $error->getLine());
$this->assertSame($expectedFile, $error->getFile());

$error = $errors[1];
$this->assertSame('Access to an uninitialized @readonly property TraitsUnititializedProperty\FooClass::$y.', $error->getMessage());
$this->assertSame(16, $error->getLine());
$this->assertSame($expectedFile, $error->getFile());

$error = $errors[2];
$this->assertSame('Access to an uninitialized property TraitsUnititializedProperty\FooClass::$z.', $error->getMessage());
$this->assertSame(17, $error->getLine());
$this->assertSame($expectedFile, $error->getFile());
}

/**
* @param string[] $files
* @return Error[]
Expand All @@ -178,4 +212,17 @@ private function runAnalyse(array $files): array
return $analyser->analyse($files)->getErrors();
}

public static function getAdditionalConfigFiles(): array
{
return array_unique(
array_merge(
parent::getAdditionalConfigFiles(),
[
__DIR__ . '/../../../conf/bleedingEdge.neon',
__DIR__ . '/traits-integration.neon',
],
),
);
}

}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/traits-integration.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
checkUninitializedProperties: true
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/traits/uninitializedProperty/FooClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace TraitsUnititializedProperty;

class FooClass
{
use FooTrait;

public function __construct()
{
$this->foo();
$this->x = 5;
$this->y = 5;
$this->z = 5;
}
}
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/traits/uninitializedProperty/FooTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php // lint >= 8.1

namespace TraitsUnititializedProperty;

trait FooTrait
{
protected readonly int $x;

/** @readonly */
protected int $y;
protected int $z;

public function foo(): void
{
echo $this->x;
echo $this->y;
echo $this->z;
}
}