0

I wrote a PHP class to manage some statistic functions but there are some functions that need a minimum of elements i.e. >0. So I added a try catch and I still get the same error.

/**
 * Calculates the minimum for a given set of values.
 *
 * @param array $values The input values
 * @return float|int The minimum of values as an integer or float
 */
public static function min($values)
{
    try {
        min($values);
    } catch (Exception $e) {
        return 0;
    }
}

And I get:

ErrorException in Statistics.php line 41: min(): Array must contain at least one element

Thanks in advance.

Marco Santana
  • 121
  • 1
  • 13

3 Answers3

1
public static function min($values)
{
    if (count($values) < 1) {
        return 0;
    }

    return min($values);
}

If you are looking to return 0 when the array is empty, then a simple if statement will suffice. Personally I'd handle this problem higher up in the stack instead of creating my own min function to handle it differently to the built-in function.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
0

Your ErrorException is being thrown by your framework. (something like Can I try/catch a warning?)

Normally, min triggers a Warning

As per the min() docs:

min() returns the parameter value considered "lowest" according to standard comparisons. If multiple values of different types evaluate as equal (e.g. 0 and 'abc') the first provided to the function will be returned.

If an empty array is passed, then FALSE will be returned and an E_WARNING error will be emitted.

In your case, $values is an empty array.

To avoid that, you can use this:

public static function min($values)
{
    if (empty($values)){
        return null; // better than 0. 0 is a valid minimum
    }

    return min($values);
}
Community
  • 1
  • 1
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
  • Ok this seems to be as the other answer the way to go. Maybe I was looking for something far more complex than it should. – Marco Santana Jul 06 '17 at 16:33
0

Since I am using Laravel as Alex noticed I was missing a use Exception to handle this Exception. After that I was able to catch that error. Thanks all of you guys

Marco Santana
  • 121
  • 1
  • 13
  • you don't need to post an answer to your own question to mark the solution. You just need to upvote and/or accept the answer that solves your issue ;) – Alex Tartan Jul 06 '17 at 17:13
  • 1
    @MarcoSantana Its great that you found the problem, but I should still advise you not to use exceptions this way. You have defined the behaviour of this method as returning zero if the array is empty, but instead of checking the length of the array you are allowing an error to be thrown, then catching the error. Exceptions are only meant to be thrown/handled in cases where the logic flow of the method is broken. In this instance you are actually "expecting" an error, this introduces an inefficiency in your code (small perhaps, but still pretty bad). – Flosculus Jul 07 '17 at 08:56
  • Thank you I will see into it – Marco Santana Jul 08 '17 at 15:36