Generating statistical reports 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) &amp;&amp; !empty($prov) &amp;&amp; !empty($dist))
        {
            return self::where('decision', '3')-&gt;where('gender', $gender)-&gt;where('p_province', $prov)-&gt;where('p_district', $dist)-&gt;count();
        }
        if (!empty($prov) &amp;&amp; !empty($dist))
        {
            return self::where('decision', '3')-&gt;where('p_province', $prov)-&gt;where('p_district', $dist)-&gt;count();
        }
        if (!empty($prov) &amp;&amp; !empty($gender))
        {
            return self::where('decision', '3')-&gt;where('p_province', $prov)-&gt;where('gender', $gender)-&gt;count();
        }
        if(!empty($gender) &amp;&amp; !empty($ttc))
        {
            return self::where('decision', '3')-&gt;where('ttc_name', $ttc)-&gt;where('gender', $gender)-&gt;count();
        }
        if(!empty($ttc))
        {
            return self::where('decision', '3')-&gt;where('ttc_name', $ttc)-&gt;count();
        }
        if (!empty($gender))
        {
            return self::where('decision', '3')-&gt;where('gender', $gender)-&gt;count();
        }
        if (!empty($prov))
        {
            return self::where('decision', '3')-&gt;where('p_province', $prov)-&gt;count();
        }

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


I am calling the function like this:

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

]
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' =&gt; $gender,
        'p_district' =&gt; $dist,
        'p_province' =&gt; $prov,
        'ttc_name' =&gt; $ttc
    ];

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

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

    return $result-&gt;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-&gt;count());


Hope this helps :)