Skip to content

Commit a6bb5e1

Browse files
committed
Use ConstructorsHelper in ReadOnlyByPhpDocPropertyAssignRule and ReadOnlyPropertyAssignRule
1 parent 098c178 commit a6bb5e1

File tree

6 files changed

+66
-6
lines changed

6 files changed

+66
-6
lines changed

src/Rules/Properties/ReadOnlyByPhpDocPropertyAssignRule.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\PropertyAssignNode;
8+
use PHPStan\Reflection\ConstructorsHelper;
89
use PHPStan\Reflection\MethodReflection;
910
use PHPStan\Rules\Rule;
1011
use PHPStan\Rules\RuleErrorBuilder;
1112
use PHPStan\ShouldNotHappenException;
1213
use PHPStan\Type\ThisType;
14+
use function in_array;
1315
use function sprintf;
1416
use function strtolower;
1517

@@ -19,7 +21,10 @@
1921
class ReadOnlyByPhpDocPropertyAssignRule implements Rule
2022
{
2123

22-
public function __construct(private PropertyReflectionFinder $propertyReflectionFinder)
24+
public function __construct(
25+
private PropertyReflectionFinder $propertyReflectionFinder,
26+
private ConstructorsHelper $constructorsHelper,
27+
)
2328
{
2429
}
2530

@@ -67,7 +72,10 @@ public function processNode(Node $node, Scope $scope): array
6772
throw new ShouldNotHappenException();
6873
}
6974

70-
if (strtolower($scopeMethod->getName()) === '__construct' || strtolower($scopeMethod->getName()) === '__unserialize') {
75+
if (
76+
in_array($scopeMethod->getName(), $this->constructorsHelper->getConstructors($scopeClassReflection), true)
77+
|| strtolower($scopeMethod->getName()) === '__unserialize'
78+
) {
7179
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
7280
$errors[] = RuleErrorBuilder::message(sprintf('@readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
7381
}

src/Rules/Properties/ReadOnlyPropertyAssignRule.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\PropertyAssignNode;
8+
use PHPStan\Reflection\ConstructorsHelper;
89
use PHPStan\Reflection\MethodReflection;
910
use PHPStan\Rules\Rule;
1011
use PHPStan\Rules\RuleErrorBuilder;
1112
use PHPStan\ShouldNotHappenException;
1213
use PHPStan\Type\ThisType;
14+
use function in_array;
1315
use function sprintf;
1416
use function strtolower;
1517

@@ -19,7 +21,10 @@
1921
class ReadOnlyPropertyAssignRule implements Rule
2022
{
2123

22-
public function __construct(private PropertyReflectionFinder $propertyReflectionFinder)
24+
public function __construct(
25+
private PropertyReflectionFinder $propertyReflectionFinder,
26+
private ConstructorsHelper $constructorsHelper,
27+
)
2328
{
2429
}
2530

@@ -67,7 +72,10 @@ public function processNode(Node $node, Scope $scope): array
6772
throw new ShouldNotHappenException();
6873
}
6974

70-
if (strtolower($scopeMethod->getName()) === '__construct' || strtolower($scopeMethod->getName()) === '__unserialize') {
75+
if (
76+
in_array($scopeMethod->getName(), $this->constructorsHelper->getConstructors($scopeClassReflection), true)
77+
|| strtolower($scopeMethod->getName()) === '__unserialize'
78+
) {
7179
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
7280
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
7381
}

tests/PHPStan/Rules/Properties/ReadOnlyByPhpDocPropertyAssignRuleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Properties;
44

5+
use PHPStan\Reflection\ConstructorsHelper;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78

@@ -13,7 +14,14 @@ class ReadOnlyByPhpDocPropertyAssignRuleTest extends RuleTestCase
1314

1415
protected function getRule(): Rule
1516
{
16-
return new ReadOnlyByPhpDocPropertyAssignRule(new PropertyReflectionFinder());
17+
return new ReadOnlyByPhpDocPropertyAssignRule(
18+
new PropertyReflectionFinder(),
19+
new ConstructorsHelper(
20+
[
21+
'ReadonlyPropertyAssignPhpDoc\\TestCase::setUp',
22+
],
23+
),
24+
);
1725
}
1826

1927
public function testRule(): void

tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Properties;
44

5+
use PHPStan\Reflection\ConstructorsHelper;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78
use const PHP_VERSION_ID;
@@ -14,7 +15,14 @@ class ReadOnlyPropertyAssignRuleTest extends RuleTestCase
1415

1516
protected function getRule(): Rule
1617
{
17-
return new ReadOnlyPropertyAssignRule(new PropertyReflectionFinder());
18+
return new ReadOnlyPropertyAssignRule(
19+
new PropertyReflectionFinder(),
20+
new ConstructorsHelper(
21+
[
22+
'ReadonlyPropertyAssign\\TestCase::setUp',
23+
],
24+
),
25+
);
1826
}
1927

2028
public function testRule(): void

tests/PHPStan/Rules/Properties/data/readonly-assign-phpdoc.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,19 @@ public function __unserialize(array $data) : void
189189
}
190190

191191
}
192+
193+
class TestCase
194+
{
195+
196+
/**
197+
* @var int
198+
* @readonly
199+
*/
200+
private $foo;
201+
202+
protected function setUp(): void
203+
{
204+
$this->foo = 1; // additional constructor - fine
205+
}
206+
207+
}

tests/PHPStan/Rules/Properties/data/readonly-assign.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,15 @@ public function __unserialize(array $data) : void
184184
}
185185

186186
}
187+
188+
class TestCase
189+
{
190+
191+
private readonly int $foo;
192+
193+
protected function setUp(): void
194+
{
195+
$this->foo = 1; // additional constructor - fine
196+
}
197+
198+
}

0 commit comments

Comments
 (0)