Search code examples
phpfunctionclasscoding-stylelines-of-code

How can I reduce lines of code in PHP?


I am a web developer and I write code in PHP/Laravel framework. I've been tying to follow best practice for writing code and I know it's a good practice to write 15-20 lines of code in functions and maximum 200 lines of code in Classes. But every time I end up writing minimum 40-50 lines in a function. for example here the code snippet which I wrote to get details of client and assigned users.

public function preMessageSend($client, $assigned)
{
    $ticket_number = $client->ticket_number;
    $title = $client->title;
    $department = $client->department;
    $priority = $client->priority;
    if ($client->first_name !== null || $client->first_name !== '') {
        $client_name = $client->first_name." ".$client->last_name;
    } else {
        $client_name = $client->username;
    }
    if ($client->email !== '' || $client->email !== null) {
        $client_email = $client->email;
    } else {
        $client->email = 'Not available';
    }
    if($client->mobile !== null || $client->mobile !== '') {
        $client_mobile = $client->code."".$client->mobile; 
    } else {
        $client_mobile = 'Not available';
    }
    if($assigned != null) {
        if ($assigned->first_name !== null || $assigned->first_name !== '') {
            $assigned_name = $assigned->first_name." ".$assigned->last_name;
        } else {
            $assigned_name = $assigned->username;
        }
        if ($assigned->email !== '' || $assigned->email !== null) {
            $assigned_email = $assigned->email;
        } else {
            $assigned->email = 'Not available';
        }
        if($assigned->mobile !== null || $assigned->mobile !== '') {
            $assigned_mobile = $assigned->code."".$assigned->mobile; 
        } else {
            $assigned_mobile = 'Not available';
        }
        if ($assigned->address !== null || $assigned->address !== '') {
            $assigned_address = $assigned->address;
        } else {
            $assigned_address = 'Not available';
        }
        $this->sendMessageWithAssigned($ticket_number, $title, $department, $priority, $client_name, $client_email, $client_mobile, $assigned_name, $assigned_email, $assigned_mobile, $assigned_address);
    } else {
        $this->sendMessageWithoutAssigned($ticket_number, $title, $department, $priority, $client_name, $client_email, $client_mobile);
    }

Please tell me how can I reduce the loc in my class and functions and what are the best practices to avoid writing such long functions. TIA


Solution

  • Instead of

    if ($client->first_name !== null || $client->first_name !== '') {
        $client_name = $client->first_name." ".$client->last_name;
    } else {
        $client_name = $client->username;
    }
    

    You could do:

    $client_name = ($client->first_name !== null || $client->first_name !== '') ? $client->first_name." ".$client->last_name : $client->username;