HiveBrain v1.2.0
Get Started
← Back to all entries
patternphplaravelMinor

Generating statistical reports

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
reportsgeneratingstatistical

Problem

I want to generate statistical reports and I have many different where clauses so the function became long. I did much refactoring, but it is not enough. Can someone help me with some techniques that these techniques make the function short and easily readable?

public static function allRejUserByProv($prov = '', $gender = '', $dist = '', $ttc = '')
    {
        if (!empty($gender) && !empty($prov) && !empty($dist))
        {
            return self::where('decision', '3')->where('gender', $gender)->where('p_province', $prov)->where('p_district', $dist)->count();
        }
        if (!empty($prov) && !empty($dist))
        {
            return self::where('decision', '3')->where('p_province', $prov)->where('p_district', $dist)->count();
        }
        if (!empty($prov) && !empty($gender))
        {
            return self::where('decision', '3')->where('p_province', $prov)->where('gender', $gender)->count();
        }
        if(!empty($gender) && !empty($ttc))
        {
            return self::where('decision', '3')->where('ttc_name', $ttc)->where('gender', $gender)->count();
        }
        if(!empty($ttc))
        {
            return self::where('decision', '3')->where('ttc_name', $ttc)->count();
        }
        if (!empty($gender))
        {
            return self::where('decision', '3')->where('gender', $gender)->count();
        }
        if (!empty($prov))
        {
            return self::where('decision', '3')->where('p_province', $prov)->count();
        }

        return self::where('decision', '3')->count();
    }


I am calling the function like this:

public function resultTTC($prov, $dist, $ttc)
{
return [
  'rejected'             => number_format(self::allRejUserByProv($prov, '', $dist, $ttc)),
        'rejected_male'        => number_format(self::allRejUserByProv($prov, '1', $dist, $ttc)),
        'rejected_female'      => number_format(self::allRejUserByProv($prov, '2', $dist, $ttc)),

]

Solution

If you just want to do the where for every non-empty value. You can do the following:

public static function allRejUserByProv($prov = '', $gender = '', $dist = '', $ttc = '')
{
    $fields = [
        'gender' => $gender,
        'p_district' => $dist,
        'p_province' => $prov,
        'ttc_name' => $ttc
    ];

    $result = self::where('decision', '3');

    foreach ($fields as $attr => $value) {
        if(! empty($value)) {
            $result = $result->where($attr, $value);
        }
    }

    return $result->count();
}


This way you will remove the multiple ifs.

If you will always use number_format to format the return value why not just adding it to the function:

return number_format($result->count());


Hope this helps :)

Code Snippets

public static function allRejUserByProv($prov = '', $gender = '', $dist = '', $ttc = '')
{
    $fields = [
        'gender' => $gender,
        'p_district' => $dist,
        'p_province' => $prov,
        'ttc_name' => $ttc
    ];

    $result = self::where('decision', '3');

    foreach ($fields as $attr => $value) {
        if(! empty($value)) {
            $result = $result->where($attr, $value);
        }
    }

    return $result->count();
}
return number_format($result->count());

Context

StackExchange Code Review Q#123240, answer score: 3

Revisions (0)

No revisions yet.