第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PhpStorm 中是否存在邏輯問題或錯誤?

PhpStorm 中是否存在邏輯問題或錯誤?

PHP
瀟瀟雨雨 2022-07-29 16:58:21
是否有我沒有看到的邏輯錯誤/案例?我正在使用 PHP 語言級別 7.1 和 CLI Interpreter PHP 7.1.8 的 PhpStorm 2017.1我嘗試了以下情況:User有和有carrinho(DB)和沒有carrinho_idrequestUser沒有和有carrinho(DB)和沒有carrinho_idrequestcarrinho_idrequest沒有_usercarrinho_id有錯user(not logged)$user = Auth::guard('api')->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);}if($carrinho->addProduto($data)) //"$carrinho" Variable might have not been defined    return response()->json([        'carrinho_id' => $carrinho->id    ]);return response()->json(['msg' => "O produto já está no seu carrinho"],422);兩種可能的情況$user存在if(!(true && ($carrinho = $user->getCarrinho()) != null))2 兩種可能的路徑1 - 有$carrinhoif(!(true && true)) -> !(true && true) -> !(true) -> false -> skip if and load the $carrinho from the user2 - 沒有$carrinhoif(!(true && false)) -> !(true && false) -> !(false) -> true -> Go inside the if and define the $carrinho第一個里面的代碼if總是有一個$carrinho問題出在第一個if。我怎么知道?因為如果我這樣做,警告就會消失。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);}else{    $carrinho = Carrinho::salvar([]);}
查看完整描述

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>


查看完整回答
反對 回復 2022-07-29
?
慕的地6264312

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


查看完整回答
反對 回復 2022-07-29
?
慕絲7291255

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

我不會接受這個答案,因為我仍然不相信代碼有錯誤,因為沒有人能找到任何錯誤。但我將把它留在這里以表明硬代碼沒有幫助。


查看完整回答
反對 回復 2022-07-29
  • 3 回答
  • 0 關(guān)注
  • 152 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號