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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ lint:
--exclude tests/PHPStan/Levels/data/namedArguments.php \
--exclude tests/PHPStan/Rules/Keywords/data/continue-break.php \
--exclude tests/PHPStan/Rules/Properties/data/read-only-property.php \
--exclude tests/PHPStan/Rules/Properties/data/read-only-property-phpdoc-and-native.php \
--exclude tests/PHPStan/Rules/Properties/data/overriding-property.php \
--exclude tests/PHPStan/Rules/Constants/data/overriding-final-constant.php \
--exclude tests/PHPStan/Rules/Properties/data/intersection-types.php \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array

$errors = [];
foreach ($properties as $propertyName => $propertyNode) {
if (!$propertyNode->isReadOnlyByPhpDoc()) {
if (!$propertyNode->isReadOnlyByPhpDoc() || $propertyNode->isReadOnly()) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
Expand All @@ -49,7 +49,7 @@ public function processNode(Node $node, Scope $scope): array
}

foreach ($prematureAccess as [$propertyName, $line, $propertyNode]) {
if (!$propertyNode->isReadOnlyByPhpDoc()) {
if (!$propertyNode->isReadOnlyByPhpDoc() || $propertyNode->isReadOnly()) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
Expand All @@ -60,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array
}

foreach ($additionalAssigns as [$propertyName, $line, $propertyNode]) {
if (!$propertyNode->isReadOnlyByPhpDoc()) {
if (!$propertyNode->isReadOnlyByPhpDoc() || $propertyNode->isReadOnly()) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
if (!$scope->canAccessProperty($propertyReflection)) {
continue;
}
if (!$nativeReflection->isReadOnlyByPhpDoc()) {
if (!$nativeReflection->isReadOnlyByPhpDoc() || $nativeReflection->isReadOnly()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function processNode(Node $node, Scope $scope): array
if (!$scope->canAccessProperty($propertyReflection)) {
continue;
}
if (!$nativeReflection->isReadOnlyByPhpDoc()) {
if (!$nativeReflection->isReadOnlyByPhpDoc() || $nativeReflection->isReadOnly()) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Properties/ReadOnlyByPhpDocPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$node->isReadOnlyByPhpDoc()) {
if (!$node->isReadOnlyByPhpDoc() || $node->isReadOnly()) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,13 @@ public function testRule(): void
]);
}

public function testRuleIgnoresNativeReadonly(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/missing-readonly-property-assign-phpdoc-and-native.php'], []);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<ReadOnlyByPhpDocPropertyAssignRefRule>
Expand Down Expand Up @@ -58,4 +59,13 @@ public function testRule(): void
]);
}

public function testRuleIgnoresNativeReadonly(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/readonly-assign-ref-phpdoc-and-native.php'], []);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ public function testRule(): void
]);
}

public function testRuleIgnoresNativeReadonly(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/readonly-assign-phpdoc-and-native.php'], []);
}

public function testBug7361(): void
{
if (PHP_VERSION_ID < 80100) {
Expand All @@ -121,4 +130,13 @@ public function testBug7361(): void
]);
}

public function testFeature7648(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/feature-7648.php'], []);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public function testRule(): void
]);
}

public function testRuleIgnoresNativeReadonly(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/read-only-property-phpdoc-and-native.php'], []);
}

}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,18 @@ public function testRule(): void
]);
}

public function testFeature7648(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/feature-7648.php'], [
[
'Readonly property Feature7648\Request::$offset is assigned outside of the constructor.',
23,
],
]);
}

}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Properties/data/feature-7648.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace Feature7648;

/** @immutable */
class Request
{
use OffsetTrait;

public function __construct(int $offset)
{
$this->populateOffsets($offset);
}
}

/** @immutable */
trait OffsetTrait
{
public readonly int $offset;

private function populateOffsets(int $offset): void
{
$this->offset = $offset;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php // lint >= 8.1

namespace MissingReadOnlyPropertyAssignPhpDocAndNative;

class Foo
{

/** @readonly */
private readonly int $assigned;

private int $unassignedButNotReadOnly;

private int $readBeforeAssignedNotReadOnly;

/** @readonly */
private readonly int $unassigned;

/** @readonly */
private readonly int $unassigned2;

/** @readonly */
private readonly int $readBeforeAssigned;

/** @readonly */
private readonly int $doubleAssigned;

private int $doubleAssignedNotReadOnly;

public function __construct()
{
$this->assigned = 1;

echo $this->readBeforeAssignedNotReadOnly;
$this->readBeforeAssignedNotReadOnly = 1;

echo $this->readBeforeAssigned;
$this->readBeforeAssigned = 1;

$this->doubleAssigned = 1;
$this->doubleAssigned = 2;

$this->doubleAssignedNotReadOnly = 1;
$this->doubleAssignedNotReadOnly = 2;
}

public function setUnassigned2(int $i): void
{
$this->unassigned2 = $i;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ReadOnlyPropertyAndNative;

class Foo
{

/** @readonly */
private readonly int $foo;
/** @readonly */
private readonly $bar;
/** @readonly */
private readonly int $baz = 0;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php // lint >= 8.1

namespace ReadonlyPropertyAssignPhpDocAndNative;

class Foo
{

/** @readonly */
private readonly int $foo;

/** @readonly */
protected readonly int $bar;

/** @readonly */
public readonly int $baz;

public function __construct(int $foo)
{
$this->foo = $foo; // constructor - fine
}

public function setFoo(int $foo): void
{
$this->foo = $foo; // setter - report
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php // lint >= 8.1

namespace ReadOnlyPropertyAssignRefPhpDocAndNative;

class Foo
{

/** @readonly */
private readonly int $foo;

/** @readonly */
public readonly int $bar;

public function doFoo()
{
$foo = &$this->foo;
$bar = &$this->bar;
}

}