Is there a logic error/case that I'm not seeing?
I'm using PhpStorm 2017.1 with PHP language level 7.1 and CLI Interpreter PHP 7.1.8
I tried the following cases:
User
withcarrinho(DB)
and with and withoutcarrinho_id
onrequest
User
withoutcarrinho(DB)
and with and withoutcarrinho_id
onrequest
carrinho_id
onrequest
with nouser
carrinho_id
with wronguser(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);
Two possible cases
$user
exist
if(!(true && ($carrinho = $user->getCarrinho()) != null))
2 two possible path
1 - Has $carrinho
if(!(true && true)) -> !(true && true) -> !(true) -> false -> skip if and load the $carrinho from the user
2 - doesn't have $carrinho
if(!(true && false)) -> !(true && false) -> !(false) -> true -> Go inside the if and define the $carrinho
The code inside the first if
will allways have a $carrinho
The problem lies on the first if
. How do i know that?
Because if I do this, the warning goes off.
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([]);
}