-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge - cross-check generic interface implementations
- Loading branch information
1 parent
1443c5e
commit 284af50
Showing
12 changed files
with
224 additions
and
14 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Generics; | ||
|
||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
class CrossCheckInterfacesHelper | ||
{ | ||
|
||
/** | ||
* @return RuleError[] | ||
*/ | ||
public function check(ClassReflection $classReflection): array | ||
{ | ||
$interfaceTemplateTypeMaps = []; | ||
$errors = []; | ||
$check = static function (ClassReflection $classReflection) use (&$interfaceTemplateTypeMaps, &$check, &$errors): void { | ||
foreach ($classReflection->getInterfaces() as $interface) { | ||
if (!$interface->isGeneric()) { | ||
continue; | ||
} | ||
|
||
if (array_key_exists($interface->getName(), $interfaceTemplateTypeMaps)) { | ||
$otherMap = $interfaceTemplateTypeMaps[$interface->getName()]; | ||
foreach ($interface->getActiveTemplateTypeMap()->getTypes() as $name => $type) { | ||
$otherType = $otherMap->getType($name); | ||
if ($otherType === null) { | ||
continue; | ||
} | ||
|
||
if ($type->equals($otherType)) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'%s specifies template type %s of interface %s as %s but it\'s already specified as %s.', | ||
$classReflection->isInterface() ? sprintf('Interface %s', $classReflection->getName()) : sprintf('Class %s', $classReflection->getName()), | ||
$name, | ||
$interface->getName(), | ||
$type->describe(VerbosityLevel::value()), | ||
$otherType->describe(VerbosityLevel::value()) | ||
))->build(); | ||
} | ||
continue; | ||
} | ||
|
||
$interfaceTemplateTypeMaps[$interface->getName()] = $interface->getActiveTemplateTypeMap(); | ||
} | ||
|
||
$parent = $classReflection->getParentClass(); | ||
while ($parent !== false) { | ||
$check($parent); | ||
$parent = $parent->getParentClass(); | ||
} | ||
|
||
foreach ($classReflection->getInterfaces() as $interface) { | ||
$check($interface); | ||
} | ||
}; | ||
|
||
$check($classReflection); | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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
30 changes: 30 additions & 0 deletions
30
tests/PHPStan/Rules/Generics/data/cross-check-interfaces-interfaces.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,30 @@ | ||
<?php | ||
|
||
namespace CrossCheckInterfacesInInterfaces; | ||
|
||
final class Item | ||
{ | ||
} | ||
|
||
/** | ||
* @extends \Traversable<int, Item> | ||
*/ | ||
interface ItemListInterface extends \Traversable | ||
{ | ||
} | ||
|
||
/** | ||
* @extends \IteratorAggregate<int, string> | ||
*/ | ||
interface ItemList extends \IteratorAggregate, ItemListInterface | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @extends \IteratorAggregate<int, Item> | ||
*/ | ||
interface ItemList2 extends \IteratorAggregate, ItemListInterface | ||
{ | ||
|
||
} |
Oops, something went wrong.