-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixes bug with old version where duplicate traits and methods could…
… be inserted into a class. - Won't import identical abstract methods from a trait, but will let you declare them. - Throw an error when importing the same method from multiple traits (use aliasing and insteadof to avoid). - Duplicate properties must be identical type, visibility, and default literal value in order to be imported from a trait. - Duplicate constants must be identical type, visibility, and default literal value in order to be import from a trait. - All classes and enums checked for duplicate method names. - Todo: index phase silently discards errors and tries to continue, but analysis phase emits errors.
- Loading branch information
1 parent
52dc249
commit 0ddb726
Showing
8 changed files
with
365 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace BambooHR\Guardrail\Checks; | ||
|
||
use BambooHR\Guardrail\Output\OutputInterface; | ||
use BambooHR\Guardrail\Scope; | ||
use BambooHR\Guardrail\SymbolTable\SymbolTable; | ||
use PhpParser\Builder\Interface_; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\Enum_; | ||
|
||
class DuplicateMemberCheck extends BaseCheck { | ||
|
||
function getCheckNodeTypes() { | ||
return [ Class_::class, Interface_::class, Enum_::class]; | ||
} | ||
|
||
function run($fileName, Node $node, ClassLike $inside = null, Scope $scope = null) { | ||
if ($node instanceof ClassLike) { | ||
$members = ["method"=>[], "property"=>[], "constant"=>[]]; | ||
|
||
foreach ($node->stmts as $stmt) { | ||
if ($stmt instanceof Node\Stmt\ClassMethod) { | ||
$this->addOrEmitError($fileName, $stmt, $members, $stmt->name->name, "method"); | ||
} else if ($stmt instanceof Node\Stmt\Property) { | ||
foreach ($stmt->props as $prop) { | ||
$this->addOrEmitError($fileName, $prop, $members, $prop->name->name, "property"); | ||
} | ||
} else if ($stmt instanceof Node\Stmt\ClassConst) { | ||
foreach ($stmt->consts as $const) { | ||
$this->addOrEmitError($fileName, $const, $members, $const->name->name, "constant"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function addOrEmitError(string $fileName, Node $node, array &$members, string $name,string $type) { | ||
$normalizedName = ($type == 'method') ? strtolower($name) : $name; | ||
if (!isset($members[$type][$normalizedName])) { | ||
$members[$type][$normalizedName] = $node->getLine(); | ||
} else { | ||
$this->emitError($fileName, $node, ErrorConstants::TYPE_DUPLICATE_NAME, "Duplicate $type " . $name . " first declared on line " . $members[$type][$normalizedName]); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.