Skip to content

Commit

Permalink
Prevent reporting property.inInterface error when PHP version is `8…
Browse files Browse the repository at this point in the history
….4` or later
  • Loading branch information
jakubtobiasz committed Nov 23, 2024
1 parent c0bfae6 commit 6ffbffd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ lint:
--exclude tests/PHPStan/Rules/Classes/data/extends-readonly-class.php \
--exclude tests/PHPStan/Rules/Classes/data/instantiation-promoted-properties.php \
--exclude tests/PHPStan/Rules/Classes/data/bug-11592.php \
--exclude tests/PHPStan/Rules/Properties/data/properties-in-interface-ignore-property-in-interface.php \
src tests

cs:
Expand Down
5 changes: 5 additions & 0 deletions src/Rules/Properties/PropertiesInInterfaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
final class PropertiesInInterfaceRule implements Rule
{
private const PHP_8_4_VERSION_ID = 80400;

public function getNodeType(): string
{
Expand All @@ -25,6 +26,10 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (PHP_VERSION_ID >= self::PHP_8_4_VERSION_ID) {
return [];
}

return [
RuleErrorBuilder::message('Interfaces may not include properties.')
->nonIgnorable()
Expand Down
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Properties/PropertiesInInterfaceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Properties;

use PHPStan\Analyser\Error;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

Expand All @@ -10,6 +11,7 @@
*/
class PropertiesInInterfaceRuleTest extends RuleTestCase
{
private const PHP_8_4_VERSION_ID = 80400;

protected function getRule(): Rule
{
Expand All @@ -30,4 +32,36 @@ public function testRule(): void
]);
}

public function testIgnoringPropertyInInterfaceOnPHPBelow84(): void
{
if (PHP_VERSION_ID >= self::PHP_8_4_VERSION_ID) {
$this->markTestSkipped('Test is valid only on PHP 8.3 or earlier');
}

$errors = $this->gatherAnalyserErrors([__DIR__ . '/data/properties-in-interface-ignore-property-in-interface.php']);

/** @var array<Error> $propertyInInterfaceErrors */
$propertyInInterfaceErrors = array_filter($errors, static function (Error $error) {
return $error->getIdentifier() === 'property.inInterface';
});

$this->assertCount(1, $propertyInInterfaceErrors);
$this->assertFalse($propertyInInterfaceErrors[0]->canBeIgnored());
}

public function testIgnoringPropertyInInterfaceOnPHP84OrAbove(): void
{
if (PHP_VERSION_ID < self::PHP_8_4_VERSION_ID) {
$this->markTestSkipped('Test is valid only on PHP 8.4 or later');
}

$errors = $this->gatherAnalyserErrors([__DIR__ . '/data/properties-in-interface-ignore-property-in-interface.php']);

/** @var array<Error> $propertyInInterfaceErrors */
$propertyInInterfaceErrors = array_filter($errors, static function (Error $error) {
return $error->getIdentifier() === 'property.inInterface';
});

$this->assertCount(0, $propertyInInterfaceErrors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace PHPStan\Rules\Properties\data;

interface HelloWorld
{
public string $name;
}

0 comments on commit 6ffbffd

Please sign in to comment.