Skip to content

Commit

Permalink
Use ConstructorsHelper in ReadOnlyByPhpDocPropertyAssignRule and …
Browse files Browse the repository at this point in the history
…`ReadOnlyPropertyAssignRule`
  • Loading branch information
herndlm committed May 18, 2022
1 parent 098c178 commit 822138d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/Rules/Properties/ReadOnlyByPhpDocPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ThisType;
use function in_array;
use function sprintf;
use function strtolower;

Expand All @@ -19,7 +21,10 @@
class ReadOnlyByPhpDocPropertyAssignRule implements Rule
{

public function __construct(private PropertyReflectionFinder $propertyReflectionFinder)
public function __construct(
private PropertyReflectionFinder $propertyReflectionFinder,
private ConstructorsHelper $constructorsHelper,
)
{
}

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

if (strtolower($scopeMethod->getName()) === '__construct' || strtolower($scopeMethod->getName()) === '__unserialize') {
if (
in_array($scopeMethod->getName(), $this->constructorsHelper->getConstructors($scopeClassReflection), true)
|| strtolower($scopeMethod->getName()) === '__unserialize'
) {
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
$errors[] = RuleErrorBuilder::message(sprintf('@readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
}
Expand Down
12 changes: 10 additions & 2 deletions src/Rules/Properties/ReadOnlyPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ThisType;
use function in_array;
use function sprintf;
use function strtolower;

Expand All @@ -19,7 +21,10 @@
class ReadOnlyPropertyAssignRule implements Rule
{

public function __construct(private PropertyReflectionFinder $propertyReflectionFinder)
public function __construct(
private PropertyReflectionFinder $propertyReflectionFinder,
private ConstructorsHelper $constructorsHelper,
)
{
}

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

if (strtolower($scopeMethod->getName()) === '__construct' || strtolower($scopeMethod->getName()) === '__unserialize') {
if (
in_array($scopeMethod->getName(), $this->constructorsHelper->getConstructors($scopeClassReflection), true)
|| strtolower($scopeMethod->getName()) === '__unserialize'
) {
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Properties;

use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

Expand All @@ -13,7 +14,14 @@ class ReadOnlyByPhpDocPropertyAssignRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new ReadOnlyByPhpDocPropertyAssignRule(new PropertyReflectionFinder());
return new ReadOnlyByPhpDocPropertyAssignRule(
new PropertyReflectionFinder(),
new ConstructorsHelper(
[
'ReadonlyPropertyAssignPhpDoc\\TestCase::setUp',
],
),
);
}

public function testRule(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Properties;

use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;
Expand All @@ -14,7 +15,14 @@ class ReadOnlyPropertyAssignRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new ReadOnlyPropertyAssignRule(new PropertyReflectionFinder());
return new ReadOnlyPropertyAssignRule(
new PropertyReflectionFinder(),
new ConstructorsHelper(
[
'ReadonlyPropertyAssign\\TestCase::setUp',
],
),
);
}

public function testRule(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ public function __construct(int $foo)
}

}

class TestCase
{

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

protected function setUp(): void
{
$this->foo = 1; // additional constructor - fine
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Properties/data/readonly-assign-phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,16 @@ public function __unserialize(array $data) : void
}

}

class TestCase
{

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

protected function setUp(): void
{
$this->foo = 1; // additional constructor - fine
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Properties/data/readonly-assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,15 @@ public function __unserialize(array $data) : void
}

}

class TestCase
{

private readonly int $foo;

protected function setUp(): void
{
$this->foo = 1; // additional constructor - fine
}

}

0 comments on commit 822138d

Please sign in to comment.