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

Detect properties declared in interface #2731

Merged
merged 2 commits into from
Nov 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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/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;
}