-
Notifications
You must be signed in to change notification settings - Fork 2
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
Check interfaces that extend other interfaces. #120
Merged
+52
−69
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PHPUnit\Util\Type; | ||
use PhpParser\Node\Stmt\Interface_; | ||
|
||
/** | ||
* Class InterfaceCheck | ||
|
@@ -50,7 +50,7 @@ function __construct(SymbolTable $symbolTable, OutputInterface $doc) { | |
* @return array | ||
*/ | ||
public function getCheckNodeTypes() { | ||
return [Class_::class]; | ||
return [Class_::class, Interface_::class]; | ||
} | ||
|
||
/** | ||
|
@@ -65,7 +65,7 @@ public function getCheckNodeTypes() { | |
* | ||
* @return void | ||
*/ | ||
protected function checkMethod($fileName, Class_ $class, Node\FunctionLike $astNode, MethodInterface $method, MethodInterface $parentMethod) { | ||
protected function checkMethod($fileName, Class_|Interface_ $class, Node\FunctionLike $astNode, MethodInterface $method, MethodInterface $parentMethod) { | ||
|
||
$visibility = $method->getAccessLevel(); | ||
$oldVisibility = $parentMethod->getAccessLevel(); | ||
|
@@ -96,7 +96,7 @@ protected function checkMethod($fileName, Class_ $class, Node\FunctionLike $astN | |
if (!$isContravariant) { | ||
$childParamType = TypeComparer::typeToString($childParam->getType()); | ||
$parentParamType = TypeComparer::typeToString($parentParam->getType()); | ||
$this->emitErrorOnLine($fileName, $method->getStartingLine(), self::TYPE_SIGNATURE_TYPE, "Child method parameter " . $childParam->getName() . " type mismatch " . $className . "::" . $method->getName() . " : $parentParamType -> $childParamType"); | ||
$this->emitError($fileName, $astNode, self::TYPE_SIGNATURE_TYPE, "Child method parameter " . $childParam->getName() . " type mismatch " . $className . "::" . $method->getName() . " : $parentParamType -> $childParamType"); | ||
} | ||
} | ||
if ($childParam->getName() != $parentParam->getName()) { | ||
|
@@ -163,34 +163,6 @@ private function assertParentChildReturnTypesMatch( | |
} | ||
} | ||
|
||
/** | ||
* implementsMethod | ||
* | ||
* @param Class_ $node Instance of ClassAbstraction | ||
* @param string $interfaceMethod The interface | ||
* | ||
* @return ClassMethod|null | ||
*/ | ||
protected function implementsMethod(Class_ $node, $interfaceMethod) { | ||
$current = new AbstractedClass_($node); | ||
while (true) { | ||
// Is it directly in the class | ||
$classMethod = $current->getMethod($interfaceMethod); | ||
if ($classMethod) { | ||
return $classMethod; | ||
} | ||
|
||
if ($current->getParentClassName()) { | ||
$current = $this->symbolTable->getAbstractedClass($current->getParentClassName()); | ||
if (!$current) { | ||
return null; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* run | ||
* | ||
|
@@ -203,13 +175,16 @@ protected function implementsMethod(Class_ $node, $interfaceMethod) { | |
*/ | ||
public function run($fileName, Node $node, ClassLike $inside = null, Scope $scope = null) { | ||
if ($node instanceof Class_) { | ||
if ($node->implements) { | ||
if ($node->implements || $node->extends) { | ||
$this->processNodeImplements($fileName, $node); | ||
} | ||
if ($node->extends) { | ||
$this->processNodeExtends($fileName, $node); | ||
} | ||
} | ||
if ($node instanceof Interface_ && $node->extends) { | ||
$this->processNodeImplements($fileName, $node); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a new check. If you have one interface that extends other interfaces, then we make sure that any common methods have compatible signatures. |
||
} | ||
|
||
/** | ||
|
@@ -220,30 +195,19 @@ public function run($fileName, Node $node, ClassLike $inside = null, Scope $scop | |
* | ||
* @return void | ||
*/ | ||
private function processNodeImplements($fileName, Class_ $node) { | ||
$arr = is_array($node->implements) ? $node->implements : [$node->implements]; | ||
foreach ($arr as $interface) { | ||
$name = $interface->toString(); | ||
if ($name) { | ||
$interface = $this->symbolTable->getAbstractedClass($name); | ||
if (!$interface) { | ||
$this->emitError($fileName, $node, ErrorConstants::TYPE_UNKNOWN_CLASS, $node->name . " implements unknown interface " . $name); | ||
} else { | ||
$this->processNodeImplementsNotAbstract($fileName, $node, $interface); | ||
} | ||
private function processNodeImplements(string $fileName, Class_|Interface_ $node):void { | ||
$arr = Util::findAllInterfaces(strval($node->namespacedName), $this->symbolTable); | ||
foreach ($arr as $name) { | ||
$interface = $this->symbolTable->getAbstractedClass($name); | ||
if (!$interface) { | ||
$this->emitError($fileName, $node, ErrorConstants::TYPE_UNKNOWN_CLASS, $node->name . " implements unknown interface " . $name); | ||
} else { | ||
$this->processNodeImplementsInterface($fileName, $node, $interface); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* processNodeExtends | ||
* | ||
* @param string $fileName The file name | ||
* @param Node $node Instance of Node | ||
* | ||
* @return void | ||
*/ | ||
private function processNodeExtends($fileName, Class_ $node) { | ||
private function processNodeExtends(string $fileName, Class_ $node):void { | ||
$class = new AbstractedClass_($node); | ||
$parentClass = $this->symbolTable->getAbstractedClass($node->extends); | ||
if (!$parentClass) { | ||
|
@@ -262,22 +226,24 @@ private function processNodeExtends($fileName, Class_ $node) { | |
} | ||
} | ||
|
||
private function processNodeImplementsNotAbstract($fileName, Class_ $node, ClassInterface $interface) { | ||
// Don't force abstract classes to implement all methods. | ||
if (!$node->isAbstract()) { | ||
foreach ($interface->getMethodNames() as $interfaceMethod) { | ||
$classMethod = $this->implementsMethod($node, $interfaceMethod); | ||
if (!$classMethod) { | ||
if (!$node->isAbstract()) { | ||
$this->emitError($fileName, $node, ErrorConstants::TYPE_UNIMPLEMENTED_METHOD, $node->name . " does not implement method " . $interfaceMethod); | ||
} | ||
} else { | ||
foreach($node->stmts as $stmt) { | ||
if ($stmt instanceof Node\Stmt\ClassMethod && strcasecmp($stmt->name, $interfaceMethod)==0) { | ||
$this->checkMethod($fileName, $node, $stmt, $classMethod, $interface->getMethod($interfaceMethod)); | ||
break; | ||
} | ||
} | ||
private function processNodeImplementsInterface(string $fileName, Class_|Interface_ $node, ClassInterface $interface):void { | ||
$methods = $interface->getMethodNames(); | ||
foreach($methods as $methodName) { | ||
$this->processInterfaceMethod($node, $methodName, $fileName, $interface); | ||
} | ||
} | ||
|
||
public function processInterfaceMethod(Class_|Interface_ $node, string $interfaceMethod, string $fileName, ClassInterface $interface) : void { | ||
$classMethod = Util::findAbstractedMethod($node->namespacedName, $interfaceMethod, $this->symbolTable); | ||
if (!$classMethod) { | ||
if ($node instanceof Node\Stmt\Class_ && !$node->isAbstract()) { | ||
$this->emitError($fileName, $node, ErrorConstants::TYPE_UNIMPLEMENTED_METHOD, $node->name . " does not implement method " . $interfaceMethod); | ||
} | ||
} else { | ||
foreach ($node->stmts as $stmt) { | ||
if ($stmt instanceof Node\Stmt\ClassMethod && strcasecmp($stmt->name, $interfaceMethod) == 0) { | ||
$this->checkMethod($fileName, $node, $stmt, $classMethod, $interface->getMethod($interfaceMethod)); | ||
break; | ||
} | ||
} | ||
} | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the meat of the change. If a class implements OR extends any class, then we build a deep list of interfaces and confirm that you implement all the methods.