如何减少PHP中的代码行?

时间:2016-09-23 08:12:07

标签: php function class coding-style lines-of-code

我是一名Web开发人员,我在PHP / Laravel框架中编写代码。我一直在追求编写代码的最佳实践,我知道在函数中编写15-20行代码并在类中编写最多200行代码是一种很好的做法。但每次我最终在函数中写入最少40-50行。例如,这里是我编写的代码片段,用于获取客户端和分配用户的详细信息。

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);
    }

请告诉我如何减少班级和功能中的loc以及避免编写这么长的函数的最佳实践。 TIA

3 个答案:

答案 0 :(得分:0)

而不是

max-age=number of seconds that you want objects to stay in a CloudFront edge cache

你可以这样做:

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

答案 1 :(得分:0)

首先,对于null''empty()true,所以你可以这样做:

if (!empty($client->first_name)) { // if not empty
    $client_name = $client->first_name." ".$client->last_name;
} else {
    $client_name = $client->username;
}

然后你也可以使用三元运算符:

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

然后,对于某些语句,还有or语句可用:

$client_email     = $client->email or 'Not available';
$client_mobile    = $client->code . $client->mobile or 'Not available';
$assigned_address = $assigned->address or 'Not available';

这些or语句等于:

if(!empty($assigned->address)){
   $assigned_address = $assigned->address;
} else {
   $assigned_address = 'Not available';
}

// Or the equivalent ternary
$assigned_address = !empty($assigned->address) ? $assigned->address : 'Not available';

我对“某些”的意思是:

$client->first_name = null;
$client->last_name  = null;
echo empty($client->first_name." ".$client->last_name); // false 
echo isset($client->first_name." ".$client->last_name); // true
即使两个变量都为空,

也不为空,因为" "空格会导致isset()

现在要小心这些或语句,因为!empty()并不总是给出与isset()相反的结果,其中isset([])为真且empty([])也为真。

答案 2 :(得分:0)

正如其他人已经建议的那样,您可以使用empty()代替!= null!= ''支票。此外,您可以省略大多数语句中的else部分,例如:

$assigned_name = $assigned->username;
if (!empty($assigned->first_name)) {
    $assigned_name = $assigned->first_name." ".$assigned->last_name;
}

默认情况下,这会将$assigned_name设置为您之前的else值,如果符合条件,$assigned_name会被覆盖。我不建议使用三元运算符,因为它不是那种可读的IMO。

只要代码可读且高效,我就不会过多担心代码行。