gpt4 book ai didi

php - 如何减少 PHP 中的代码行数?

转载 作者:行者123 更新时间:2023-12-04 13:41:39 25 4
gpt4 key购买 nike

<分区>

我是一名网络开发人员,我在 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);
}

请告诉我如何减少我的类和函数中的位置,以及避免编写如此长的函数的最佳实践是什么。时间差

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com