Method: Puppet::Pops::Types::TypeMismatchDescriber#signature_string

Defined in:
lib/puppet/pops/types/type_mismatch_describer.rb

#signature_string(signature) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Produces a string for the signature(s)



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'lib/puppet/pops/types/type_mismatch_describer.rb', line 1041

def signature_string(signature)
  param_types = signature.type.param_types
  param_names = signature.parameter_names

  from, to = param_types.size_range
  if from == 0 && to == 0
    # No parameters function
    return ''
  end

  required_count = from
  types =
    case param_types
    when PTupleType
      param_types.types
    when PArrayType
      [param_types.element_type]
    end

  # join type with names (types are always present, names are optional)
  # separate entries with comma
  #
  param_names = Array.new(types.size, '') if param_names.empty?
  limit = param_names.size
  result = param_names.each_with_index.map do |name, index|
    type = types[index] || types[-1]
    indicator = ''
    if to == Float::INFINITY && index == limit - 1
      # Last is a repeated_param.
      indicator = from == param_names.size ? '+' : '*'
    elsif optional(index, required_count)
      indicator = '?'
      type = type.optional_type if type.is_a?(POptionalType)
    end
    "#{type} #{name}#{indicator}"
  end.join(', ')

  # If there is a block, include it
  case signature.type.block_type
  when POptionalType
    result << ', ' unless result == ''
    result << "#{signature.type.block_type.optional_type} #{signature.block_name}?"
  when PCallableType
    result << ', ' unless result == ''
    result << "#{signature.type.block_type} #{signature.block_name}"
  when NilClass
    # nothing
  end
  result
end