3 回答

TA貢獻1942條經(jīng)驗 獲得超3個贊
這段代碼非常難以閱讀,因為您將副作用(賦值)與涉及多個布爾運算符的復雜條件結(jié)合在一起。讓我們試著把它寫成一組離散的操作:
// First condition to be evaluated is isset($user)
$haveUser = isset($user);
// If that condition is false, the && will lazily skip the next part
if ( $haveUser ) {
// Now we conditionally assign $carrinho ...
$carrinho = $user->getCarrinho();
// ... and test its value
$haveCarrinho = ($carrinho != null);
}
// Having done all that, we combine the two conditions
$haveBoth = $haveUser && $haveCarrinho;
// Now we invert that condition for our if statement
if ( ! $haveBoth ) {
// We know here that $carrinho has either never been set (because $haveUser was false) ...
// ... or it was null (setting $haveCarrinho to false)
// Line 3 - another if statement to unpack
$haveIdInData = isset($data['carrinho_id']);
// If that condition is false, the || will shortcut
if ( ! $haveIdInData ) {
$carrinho = Carrinho::salvar([]);
}
// If we don't short-cut, the rest of line 3 runs
else {
// Try finding it by the ID in $data
$carrinho = Carrinho::find($data['carrinho_id']);
// If it's null, we short-cut at the next ||
if ($carrinho == null) {
$carrinho = Carrinho::salvar([]);
}
else {
// Else we make the next check
if ($carrinho->usuario_id != null) {
$carrinho = Carrinho::salvar([]);
}
}
}
// On to line 4! Reusing our condition from above, since the state of $user won't have changed
if ( $haveUser ) {
// This will give a horrible error if $carrinho is null
$carrinho->setUsuario($user->id);
}
}
// We've reached line 5, and expect $carrinho to be set, but we have no guarantee of that at all!
4 行代碼就包含了很多邏輯!
稍微整理一下,沒有讓它像原來的那樣神秘,我認為這是等價的:
$carrinho = null;
if ( isset($user) ) {
// Now we conditionally assign $carrinho ...
$carrinho = $user->getCarrinho();
}
if ( $carrinho == null ) {
if ( isset($data['carrinho_id']) ) {
$carrinho = Carrinho::find($data['carrinho_id']);
if ($carrinho == null || $carrinho->usuario_id != null) {
$carrinho = Carrinho::salvar([]);
}
}
if(isset($user)) {
$carrinho->setUsuario($user->id);
}
}
現(xiàn)在我們可以看到可能的意圖:該Carrinho::salvar行應該是任何其他未定義狀態(tài)的后備,而不是嵌套在其他條件中。
稍加努力,我們就可以完全消除嵌套條件,從而提供更具可讀性的內(nèi)容,如下所示:
// Initialise variables to a known state
$carrinho = null;
$loadedFromUser = false;
// Try on user object
if ( isset($user) ) {
$carrinho = $user->getCarrinho();
$loadedFromUser = ($carrinho != null);
}
// Not found? Try looking up by input data
if ( $carrinho == null && isset($data['carrinho_id']) ) {
$carrinho = Carrinho::find($data['carrinho_id']);
}
// Discard if it already has a user ID, but wasn't loaded from user
if (!$loadedFromUser && $carrinho != null && $carrinho->usuario_id != null) {
$carrinho = null;
}
// Still not found? Create an empty one
if ($carrinho == null) {
$carrinho = Carrinho::salvar([]);
}
// Now we know we have an object some way or another, and can assign a user ID to it
if(isset($user) && !$loadedFromUser) {
$carrinho->setUsuario($user->id);
}
其中一些條件可能并不完全符合預期,但通過將它們分開,我們現(xiàn)在可以更輕松地遵循邏輯并進行適當?shù)母摹?/p>

TA貢獻1817條經(jīng)驗 獲得超6個贊
您的代碼在執(zhí)行過程中可能有問題,PHPStorm 對此進行了說明。在某些情況下你不會有$carrinho對象,例如,如果$user變量存在
if(!(isset($user) && ($carrinho = $user->getCarrinho()) != null)){
if(!isset($data['carrinho_id']) || (($carrinho = Carrinho::find($data['carrinho_id'])) == null) || ($carrinho->usuario_id != null))
$carrinho = Carrinho::salvar([]);
if(isset($user))
$carrinho->setUsuario($user->id);
}
并且代碼$carrinho->addProduto($data)將失敗。
你需要修復它。例如,您可以將代碼移動到條件塊中
if(!(isset($user) && ($carrinho = $user->getCarrinho()) != null)){
if(!isset($data['carrinho_id']) || (($carrinho = Carrinho::find($data['carrinho_id'])) == null) || ($carrinho->usuario_id != null)) {
$carrinho = Carrinho::salvar([]);
}
if(isset($user)) {
carrinho->setUsuario($user->id);
}
if($carrinho->addProduto($data)) {
return response()->json([
'carrinho_id' => $carrinho->id
]);
}
}
return response()->json(['msg' => "O produto já está no seu carrinho"],422);

TA貢獻1859條經(jīng)驗 獲得超6個贊
經(jīng)過一些質(zhì)疑和研究,我得出的結(jié)論是,硬代碼對任何人都沒有幫助,在團隊合作和項目發(fā)展方面。我決定通過以下方式實現(xiàn)代碼:
$user = Auth::guard('api')->user();
$hasUser = isset($user);
if($hasUser)
$carrinho = $user->getCarrinho();
if(!isset($carrinho)){
if(isset($data['carrinho_id']))
$carrinho = Carrinho::find($data['carrinho_id']);
if(!isset($carrinho) || $carrinho->usuario_id != null)
$carrinho = Carrinho::salvar([]);
if($hasUser)
$carrinho->setUsuario($user->id);
}
if($carrinho->addProduto($data))
return response()->json([
'carrinho_id' => $carrinho->id
]);
return response()->json(['msg' => "O produto já está no seu carrinho"],422);
我不會接受這個答案,因為我仍然不相信代碼有錯誤,因為沒有人能找到任何錯誤。但我將把它留在這里以表明硬代碼沒有幫助。
- 3 回答
- 0 關(guān)注
- 152 瀏覽
添加回答
舉報