Skip to content

Commit

Permalink
Detect properties declared in interface
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Nov 13, 2023
1 parent d736f4e commit 309885f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ lint:
--exclude tests/PHPStan/Rules/Functions/data/arrow-function-nullsafe-by-ref.php \
--exclude tests/PHPStan/Levels/data/namedArguments.php \
--exclude tests/PHPStan/Rules/Keywords/data/continue-break.php \
--exclude tests/PHPStan/Rules/Properties/data/bug-8915.php \
--exclude tests/PHPStan/Rules/Properties/data/properties-in-interface.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/read-only-property-readonly-class.php \
Expand Down
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ rules:
- PHPStan\Rules\Properties\AccessPropertiesInAssignRule
- PHPStan\Rules\Properties\AccessStaticPropertiesInAssignRule
- PHPStan\Rules\Properties\MissingReadOnlyPropertyAssignRule
- PHPStan\Rules\Properties\PropertiesInInterfaceRule
- PHPStan\Rules\Properties\PropertyAttributesRule
- PHPStan\Rules\Properties\ReadOnlyPropertyRule
- PHPStan\Rules\Traits\ConstantsInTraitsRule
Expand Down
33 changes: 33 additions & 0 deletions src/Rules/Properties/PropertiesInInterfaceRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Properties;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassPropertyNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<ClassPropertyNode>
*/
class PropertiesInInterfaceRule implements Rule
{

public function getNodeType(): string
{
return ClassPropertyNode::class;
}

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

return [
RuleErrorBuilder::message('Interfaces may not include properties.')->nonIgnorable()->build(),
];
}

}
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Properties/PropertiesInInterfaceRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Properties;

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

/**
* @extends RuleTestCase<PropertiesInInterfaceRule>
*/
class PropertiesInInterfaceRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new PropertiesInInterfaceRule();
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/properties-in-interface.php'], [
[
'Interfaces may not include properties.',
7,
],
[
'Interfaces may not include properties.',
9,
],
]);
}

}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Properties/data/properties-in-interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace PropertiesInInterface;

interface HelloWorld
{
public \DateTimeInterface $dateTime;

public static \Closure $callable;
}

0 comments on commit 309885f

Please sign in to comment.