-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect properties declared in interface
- Loading branch information
1 parent
2fa2aa0
commit 4f967e8
Showing
6 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
tests/PHPStan/Rules/Properties/PropertiesInInterfaceRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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', | ||
__DIR__ . '/data/bug-8915.php', | ||
], [ | ||
[ | ||
'Interfaces may not include properties.', | ||
7, | ||
], | ||
[ | ||
'Interfaces may not include properties.', | ||
9, | ||
], | ||
[ | ||
'Interfaces may not include properties.', | ||
7, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug8915; | ||
|
||
interface HelloWorld | ||
{ | ||
public \DateTimeInterface $pp; | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/PHPStan/Rules/Properties/data/properties-in-interface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |