*/ class DisallowEnumComparisonRule implements Rule{ public function getNodeType() : string{ return BinaryOp::class; } public function processNode(Node $node, Scope $scope) : array{ if(!($node instanceof Identical) and !($node instanceof NotIdentical)){ return []; } $leftType = $scope->getType($node->left); $rightType = $scope->getType($node->right); $leftEnum = $this->checkForEnumTypes($leftType); $rightEnum = $this->checkForEnumTypes($rightType); if($leftEnum && $rightEnum){ return [RuleErrorBuilder::message(sprintf( 'Strict comparison using %s involving enum types %s and %s is not reliable.', $node instanceof Identical ? '===' : '!==', $leftType->describe(VerbosityLevel::value()), $rightType->describe(VerbosityLevel::value()) ))->build()]; } 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; } }