-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented DuplicateDeclarationRule, fixes phpstan/phpstan#3475
- Loading branch information
1 parent
749f50b
commit 5e2ba1a
Showing
5 changed files
with
176 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,79 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function array_key_exists; | ||
use function sprintf; | ||
use function strtolower; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<\PHPStan\Node\InClassNode> | ||
*/ | ||
class DuplicateDeclarationRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $scope->getClassReflection(); | ||
if ($classReflection === null) { | ||
throw new \PHPStan\ShouldNotHappenException(); | ||
} | ||
|
||
$errors = []; | ||
|
||
$declaredClassConstants = []; | ||
foreach ($node->getOriginalNode()->getConstants() as $constDecl) { | ||
foreach ($constDecl->consts as $const) { | ||
if (array_key_exists($const->name->name, $declaredClassConstants)) { | ||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Cannot redeclare constant %s::%s.', | ||
$classReflection->getDisplayName(), | ||
$const->name->name | ||
))->line($const->getLine())->nonIgnorable()->build(); | ||
} else { | ||
$declaredClassConstants[$const->name->name] = true; | ||
} | ||
} | ||
} | ||
|
||
$declaredProperties = []; | ||
foreach ($node->getOriginalNode()->getProperties() as $propertyDecl) { | ||
foreach ($propertyDecl->props as $property) { | ||
if (array_key_exists($property->name->name, $declaredProperties)) { | ||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Cannot redeclare property %s::$%s.', | ||
$classReflection->getDisplayName(), | ||
$property->name->name | ||
))->line($property->getLine())->nonIgnorable()->build(); | ||
} else { | ||
$declaredProperties[$property->name->name] = true; | ||
} | ||
} | ||
} | ||
|
||
$declaredFunctions = []; | ||
foreach ($node->getOriginalNode()->getMethods() as $method) { | ||
if (array_key_exists(strtolower($method->name->name), $declaredFunctions)) { | ||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Cannot redeclare method %s::%s().', | ||
$classReflection->getDisplayName(), | ||
$method->name->name | ||
))->line($method->getStartLine())->nonIgnorable()->build(); | ||
} else { | ||
$declaredFunctions[strtolower($method->name->name)] = true; | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
tests/PHPStan/Rules/Classes/DuplicateDeclarationRuleTest.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,58 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<\PHPStan\Rules\Classes\DuplicateDeclarationRule> | ||
*/ | ||
class DuplicateDeclarationRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new DuplicateDeclarationRule(); | ||
} | ||
|
||
public function testDuplicateDeclarations(): void | ||
{ | ||
if (!self::$useStaticReflectionProvider) { | ||
$this->markTestSkipped('This test needs static reflection'); | ||
} | ||
|
||
$this->analyse( | ||
[ | ||
__DIR__ . '/data/duplicate-declarations.php', | ||
], | ||
[ | ||
[ | ||
'Cannot redeclare constant DuplicateDeclarations\Foo::CONST1.', | ||
8, | ||
], | ||
[ | ||
'Cannot redeclare constant DuplicateDeclarations\Foo::CONST2.', | ||
10, | ||
], | ||
[ | ||
'Cannot redeclare property DuplicateDeclarations\Foo::$prop1.', | ||
17, | ||
], | ||
[ | ||
'Cannot redeclare property DuplicateDeclarations\Foo::$prop2.', | ||
20, | ||
], | ||
[ | ||
'Cannot redeclare method DuplicateDeclarations\Foo::func1().', | ||
27, | ||
], | ||
[ | ||
'Cannot redeclare method DuplicateDeclarations\Foo::Func1().', | ||
35, | ||
], | ||
] | ||
); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
tests/PHPStan/Rules/Classes/data/duplicate-declarations.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,36 @@ | ||
<?php | ||
|
||
namespace DuplicateDeclarations; | ||
|
||
class Foo{ | ||
|
||
public const CONST1 = 1; | ||
public const CONST1 = 2; | ||
|
||
public const CONST2 = 2, CONST2 = 1; | ||
|
||
public const CONST3 = 1; | ||
|
||
/** @var int */ | ||
public $prop1; | ||
/** @var int */ | ||
public $prop1; | ||
|
||
/** @var int */ | ||
public $prop2, $prop2; | ||
|
||
/** @var int */ | ||
public $prop3; | ||
|
||
public function func1() : void{} | ||
|
||
public function func1() : int{ | ||
return 1; | ||
} | ||
|
||
public function func2() : int{ | ||
return 2; | ||
} | ||
|
||
public function Func1() : void{} | ||
} |