mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-01 07:43:03 +00:00
DisallowEnumComparisonRule: detect bugs involving union types (Enum|null compared to Enum|null)
This commit is contained in:
parent
4ade7b6225
commit
b534ae050e
@ -31,6 +31,8 @@ use PHPStan\Analyser\Scope;
|
|||||||
use PHPStan\Rules\Rule;
|
use PHPStan\Rules\Rule;
|
||||||
use PHPStan\Rules\RuleErrorBuilder;
|
use PHPStan\Rules\RuleErrorBuilder;
|
||||||
use PHPStan\Type\ObjectType;
|
use PHPStan\Type\ObjectType;
|
||||||
|
use PHPStan\Type\Type;
|
||||||
|
use PHPStan\Type\UnionType;
|
||||||
use PHPStan\Type\VerbosityLevel;
|
use PHPStan\Type\VerbosityLevel;
|
||||||
use pocketmine\utils\EnumTrait;
|
use pocketmine\utils\EnumTrait;
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
@ -48,23 +50,41 @@ class DisallowEnumComparisonRule implements Rule{
|
|||||||
if(!($node instanceof Identical) and !($node instanceof NotIdentical)){
|
if(!($node instanceof Identical) and !($node instanceof NotIdentical)){
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = [];
|
$leftType = $scope->getType($node->left);
|
||||||
foreach([$node->left, $node->right] as $n){
|
$rightType = $scope->getType($node->right);
|
||||||
$type = $scope->getType($n);
|
$leftEnum = $this->checkForEnumTypes($leftType);
|
||||||
if(!($type instanceof ObjectType)){
|
$rightEnum = $this->checkForEnumTypes($rightType);
|
||||||
continue;
|
if($leftEnum && $rightEnum){
|
||||||
}
|
return [RuleErrorBuilder::message(sprintf(
|
||||||
$class = $type->getClassReflection();
|
'Strict comparison using %s involving enum types %s and %s is not reliable.',
|
||||||
if($class === null or !$class->hasTraitUse(EnumTrait::class)){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$result[] = RuleErrorBuilder::message(sprintf(
|
|
||||||
'Strict comparison using %s involving enum %s is not reliable.',
|
|
||||||
$node instanceof Identical ? '===' : '!==',
|
$node instanceof Identical ? '===' : '!==',
|
||||||
$type->describe(VerbosityLevel::value())
|
$leftType->describe(VerbosityLevel::value()),
|
||||||
))->build();
|
$rightType->describe(VerbosityLevel::value())
|
||||||
|
))->build()];
|
||||||
}
|
}
|
||||||
return $result;
|
return [];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private function checkForEnumTypes(Type $comparedType) : bool{
|
||||||
|
//TODO: what we really want to do here is iterate over the contained types, but there's no universal way to
|
||||||
|
//do that. This might break with other circumstances.
|
||||||
|
if($comparedType instanceof ObjectType){
|
||||||
|
$types = [$comparedType];
|
||||||
|
}elseif($comparedType instanceof UnionType){
|
||||||
|
$types = $comparedType->getTypes();
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
foreach($types as $containedType){
|
||||||
|
if(!($containedType instanceof ObjectType)){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$class = $containedType->getClassReflection();
|
||||||
|
if($class !== null and $class->hasTraitUse(EnumTrait::class)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user