Skip to main content
added 519 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

Use boolean expressions directly

Rules.php is full of ternaries that return true or false, like this one:

public static function minlen($str, $value)
{
    return mb_strlen(trim($str)) < $value ? true : false;
}

You can return boolean expressions directly:

    return mb_strlen(trim($str)) < $value;

 

Don't repeat yourself

All the rules trim the input. Instead of writing trim in all of them, it would be simpler to trim once before using the rule methods, so that you don't have to repeatedly write trim so many times in every single method.

Don't return two types of values

The validateName may return two kinds of values: an array of errors, or true (boolean).

    if (isset($errors['name'])) {
        return $errors['name'];
    } else {
        return true;
    }

This is poor design. Return one type of value. If there are no errors, return an empty array. That way the return type will be consistently an array, which is a good thing. The code snippet above will become simpler too:

return $errors['name'];

Rules.php is full of ternaries that return true or false, like this one:

public static function minlen($str, $value)
{
    return mb_strlen(trim($str)) < $value ? true : false;
}

You can return boolean expressions directly:

    return mb_strlen(trim($str)) < $value;

 

All the rules trim the input. Instead of writing trim in all of them, it would be simpler to trim once before using the rule methods, so that you don't have to repeatedly write trim so many times in every single method.

Use boolean expressions directly

Rules.php is full of ternaries that return true or false, like this one:

public static function minlen($str, $value)
{
    return mb_strlen(trim($str)) < $value ? true : false;
}

You can return boolean expressions directly:

    return mb_strlen(trim($str)) < $value;

Don't repeat yourself

All the rules trim the input. Instead of writing trim in all of them, it would be simpler to trim once before using the rule methods, so that you don't have to repeatedly write trim so many times in every single method.

Don't return two types of values

The validateName may return two kinds of values: an array of errors, or true (boolean).

    if (isset($errors['name'])) {
        return $errors['name'];
    } else {
        return true;
    }

This is poor design. Return one type of value. If there are no errors, return an empty array. That way the return type will be consistently an array, which is a good thing. The code snippet above will become simpler too:

return $errors['name'];
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

Rules.php is full of ternaries that return true or false, like this one:

public static function minlen($str, $value)
{
    return mb_strlen(trim($str)) < $value ? true : false;
}

You can return boolean expressions directly:

    return mb_strlen(trim($str)) < $value;

All the rules trim the input. Instead of writing trim in all of them, it would be simpler to trim once before using the rule methods, so that you don't have to repeatedly write trim so many times in every single method.