Fix credo warnings and ensure clean compilation

- Renamed predicate functions to follow Elixir conventions:
  - is_finite? → finite?
  - is_finite_number? → finite_number?
  - is_finite_float? → finite_float?
- Fixed all function returns to ensure proper nil handling
- Applied mix format to all files
- All tests pass with no compilation warnings

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Graham McIntire 2025-07-14 10:25:01 -05:00
parent 94f3a62539
commit b065404803
No known key found for this signature in database
3 changed files with 80 additions and 68 deletions

View file

@ -101,7 +101,7 @@ defmodule Aprsme.EncodingUtils do
""" """
@spec to_float(any()) :: float() | nil @spec to_float(any()) :: float() | nil
def to_float(value) when is_float(value) do def to_float(value) when is_float(value) do
if is_finite_float?(value), do: value, else: nil if finite_float?(value), do: value, else: nil
end end
def to_float(value) when is_integer(value) do def to_float(value) when is_integer(value) do
@ -115,7 +115,7 @@ defmodule Aprsme.EncodingUtils do
def to_float(%Decimal{} = value) do def to_float(%Decimal{} = value) do
float = Decimal.to_float(value) float = Decimal.to_float(value)
if is_finite_float?(float), do: float, else: nil if finite_float?(float), do: float, else: nil
end end
def to_float(value) when is_binary(value) do def to_float(value) when is_binary(value) do
@ -124,11 +124,13 @@ defmodule Aprsme.EncodingUtils do
value value
|> sanitize_string() |> sanitize_string()
|> to_string() |> to_string()
|> String.slice(0, 30) # Reasonable max length for a number # Reasonable max length for a number
|> String.slice(0, 30)
case Float.parse(sanitized) do case Float.parse(sanitized) do
{float, _} when is_float(float) -> {float, _} when is_float(float) ->
if is_finite_float?(float), do: float, else: nil if finite_float?(float), do: float, else: nil
:error -> :error ->
nil nil
end end
@ -137,11 +139,12 @@ defmodule Aprsme.EncodingUtils do
def to_float(_), do: nil def to_float(_), do: nil
# Helper to check if a float is finite (not infinity or NaN) # Helper to check if a float is finite (not infinity or NaN)
defp is_finite_float?(float) when is_float(float) do defp finite_float?(float) when is_float(float) do
float != :infinity and float != :neg_infinity and float == float # NaN != NaN # NaN != NaN
float != :infinity and float != :neg_infinity and float == float
end end
defp is_finite_float?(_), do: false defp finite_float?(_), do: false
@doc """ @doc """
Converts various types to Decimal for database storage. Converts various types to Decimal for database storage.

View file

@ -62,10 +62,12 @@ defmodule AprsmeWeb.MapLive.Index do
case Float.parse(sanitized) do case Float.parse(sanitized) do
{val, ""} when val >= min and val <= max -> {val, ""} when val >= min and val <= max ->
if is_finite?(val), do: val, else: default if finite?(val), do: val, else: default
{val, _remainder} when val >= min and val <= max -> {val, _remainder} when val >= min and val <= max ->
# Accept even with trailing characters, but validate the number # Accept even with trailing characters, but validate the number
if is_finite?(val), do: val, else: default if finite?(val), do: val, else: default
_ -> _ ->
default default
end end
@ -80,9 +82,11 @@ defmodule AprsmeWeb.MapLive.Index do
case Integer.parse(sanitized) do case Integer.parse(sanitized) do
{val, ""} when val >= min and val <= max -> {val, ""} when val >= min and val <= max ->
val val
{val, _remainder} when val >= min and val <= max -> {val, _remainder} when val >= min and val <= max ->
# Accept even with trailing characters # Accept even with trailing characters
val val
_ -> _ ->
default default
end end
@ -96,7 +100,8 @@ defmodule AprsmeWeb.MapLive.Index do
str str
|> String.trim() |> String.trim()
|> String.replace(~r/[^\d\.\-eE]/, "") |> String.replace(~r/[^\d\.\-eE]/, "")
|> limit_string_length(20) # Prevent extremely long inputs # Prevent extremely long inputs
|> limit_string_length(20)
end end
defp sanitize_numeric_string(_), do: "" defp sanitize_numeric_string(_), do: ""
@ -109,12 +114,12 @@ defmodule AprsmeWeb.MapLive.Index do
defp limit_string_length(str, _), do: str defp limit_string_length(str, _), do: str
# Check if a float is finite (not infinity or NaN) # Check if a float is finite (not infinity or NaN)
defp is_finite?(float) when is_float(float) do defp finite?(float) when is_float(float) do
float != :infinity and float != :neg_infinity and float == float # NaN != NaN # NaN != NaN
float != :infinity and float != :neg_infinity and float == float
end end
defp is_finite?(_), do: false defp finite?(_), do: false
@impl true @impl true
def mount(params, session, socket) do def mount(params, session, socket) do
@ -614,7 +619,7 @@ defmodule AprsmeWeb.MapLive.Index do
end end
defp safe_parse_coordinate(value, default, min, max) when is_number(value) do defp safe_parse_coordinate(value, default, min, max) when is_number(value) do
if is_finite_number?(value) do if finite_number?(value) do
clamp_coordinate(value, min, max) clamp_coordinate(value, min, max)
else else
default default
@ -644,11 +649,12 @@ defmodule AprsmeWeb.MapLive.Index do
defp clamp_zoom(_), do: @default_zoom defp clamp_zoom(_), do: @default_zoom
# Helper to check if a number is finite # Helper to check if a number is finite
defp is_finite_number?(num) when is_number(num) do defp finite_number?(num) when is_number(num) do
is_finite?(num / 1.0) # Convert integer to float for check # Convert integer to float for check
finite?(num / 1.0)
end end
defp is_finite_number?(_), do: false defp finite_number?(_), do: false
# Validate that coordinates are within reasonable ranges # Validate that coordinates are within reasonable ranges
defp valid_coordinates?(lat, lng) when is_number(lat) and is_number(lng) do defp valid_coordinates?(lat, lng) when is_number(lat) and is_number(lng) do
@ -785,6 +791,7 @@ defmodule AprsmeWeb.MapLive.Index do
case Integer.parse(duration) do case Integer.parse(duration) do
{hours, ""} when hours in [1, 6, 12, 24, 48, 168] -> {hours, ""} when hours in [1, 6, 12, 24, 48, 168] ->
hours hours
_ -> _ ->
# Default to 1 hour if invalid # Default to 1 hour if invalid
1 1
@ -798,6 +805,7 @@ defmodule AprsmeWeb.MapLive.Index do
case Integer.parse(hours) do case Integer.parse(hours) do
{h, ""} when h in [1, 3, 6, 12, 24] -> {h, ""} when h in [1, 3, 6, 12, 24] ->
h h
_ -> _ ->
# Default to 1 hour if invalid # Default to 1 hour if invalid
1 1
@ -2380,7 +2388,8 @@ defmodule AprsmeWeb.MapLive.Index do
# Limit length and remove any potentially dangerous characters # Limit length and remove any potentially dangerous characters
# APRS paths should only contain callsigns, commas, asterisks, and dashes # APRS paths should only contain callsigns, commas, asterisks, and dashes
path path
|> String.slice(0, 200) # Reasonable max length for APRS path # Reasonable max length for APRS path
|> String.slice(0, 200)
|> String.replace(~r/[^A-Za-z0-9,\-\*\s]/, "") |> String.replace(~r/[^A-Za-z0-9,\-\*\s]/, "")
end end

View file

@ -95,6 +95,7 @@ defmodule AprsmeWeb.MapLive.MovementTest do
"west" => -96.50 "west" => -96.50
} }
} }
assert render_hook(view, "bounds_changed", bounds_params) assert render_hook(view, "bounds_changed", bounds_params)
# Wait for initial load to complete # Wait for initial load to complete
@ -182,5 +183,4 @@ defmodule AprsmeWeb.MapLive.MovementTest do
assert distance3 > 20 assert distance3 > 20
end end
end end
end end