module RuboCop
  module Cop
    module Custom
      # This cop checks for the use of reflection methods like const_get and
      # send, which are unnecessarily complex for simple automation scripts
      # and often obfuscate refactors and cleanup
      class AvoidReflection < Cop
        MSG = "Avoid using reflection in basic scripting frameworks.".freeze

        BLACKLIST = %i[const_get define_method].freeze # methods that shouldn't be used in general
        # methods that shouldn't be used outside of their own class.
        # add #send eventually.
        RECEIVER_BLACKLIST = [].freeze

        def on_send(node)
          receiver, method_name, _args = *node

          on_blacklist = BLACKLIST.include?(method_name)
          has_receiver = !receiver.nil?
          on_receiver_blacklist = RECEIVER_BLACKLIST.include?(method_name)

          return unless on_blacklist || (on_receiver_blacklist && has_receiver)

          add_offense(node, :selector)
        end
      end
    end
  end
end
