是否有我没有看到的逻辑错误/案例?
我正在使用 PHP 语言级别 7.1 和 CLI Interpreter PHP 7.1.8 的 PhpStorm 2017.1
我尝试了以下情况:
User
有和有carrinho(DB)
和没有carrinho_id
request
User
没有和有carrinho(DB)
和没有carrinho_id
request
carrinho_id
request
没有_user
carrinho_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 - 有$carrinho
if(!(true && true)) -> !(true && true) -> !(true) -> false -> skip if and load the $carrinho from the user
2 - 没有$carrinho
if(!(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([]);
}
手掌心
慕的地6264312
慕丝7291255