Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enforceReadOnlyPublicProperty: fix false positive on PHP <= 8.0 #87

Merged
merged 2 commits into from
Feb 14, 2023
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
12 changes: 12 additions & 0 deletions src/Rule/EnforceReadonlyPublicPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassPropertyNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;

/**
Expand All @@ -13,6 +14,13 @@
class EnforceReadonlyPublicPropertyRule implements Rule
{

private PhpVersion $phpVersion;

public function __construct(PhpVersion $phpVersion)
{
$this->phpVersion = $phpVersion;
}

public function getNodeType(): string
{
return ClassPropertyNode::class;
Expand All @@ -24,6 +32,10 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$this->phpVersion->supportsReadOnlyProperties()) {
return [];
}

if (!$node->isPublic() || $node->isReadOnly()) {
return [];
}
Expand Down
25 changes: 18 additions & 7 deletions tests/Rule/EnforceReadonlyPublicPropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@

namespace ShipMonk\PHPStan\Rule;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<EnforceReadonlyPublicPropertyRule>
*/
class EnforceReadonlyPublicPropertyRuleTest extends RuleTestCase
{

private ?PhpVersion $phpVersion = null;

protected function getRule(): Rule
{
return new EnforceReadonlyPublicPropertyRule();
self::assertNotNull($this->phpVersion);
return new EnforceReadonlyPublicPropertyRule($this->phpVersion);
}

public function testPhp81(): void
{
$this->phpVersion = $this->createPhpVersion(80_100);
$this->analyseFile(__DIR__ . '/data/EnforceReadonlyPublicPropertyRule/code-81.php');
}

public function test(): void
public function testPhp80(): void
{
if (PHP_VERSION_ID < 80_100) {
self::markTestSkipped('Requires PHP 8.1');
}
$this->phpVersion = $this->createPhpVersion(80_000);
$this->analyseFile(__DIR__ . '/data/EnforceReadonlyPublicPropertyRule/code-80.php');
}

$this->analyseFile(__DIR__ . '/data/EnforceReadonlyPublicPropertyRule/code.php');
private function createPhpVersion(int $version): PhpVersion
{
return new PhpVersion($version); // @phpstan-ignore-line ignore bc promise
}

}
29 changes: 29 additions & 0 deletions tests/Rule/data/EnforceReadonlyPublicPropertyRule/code-80.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace EnforceReadonlyPublicPropertyRule80;

trait MyTrait {

public ?string $public;

protected string $protected;

private string $private;

}

class MyClass {

use MyTrait;

public ?int $foo;

public int $bar;

protected int $baz;

private int $bag;

}


Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace EnforceReadonlyPublicPropertyRule;
namespace EnforceReadonlyPublicPropertyRule81;

trait MyTrait {

Expand Down