3 回答
TA贡献1942条经验 获得超3个赞
这段代码非常难以阅读,因为您将副作用(赋值)与涉及多个布尔运算符的复杂条件结合在一起。让我们试着把它写成一组离散的操作:
// 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);
}
}
现在我们可以看到可能的意图:该Carrinho::salvar行应该是任何其他未定义状态的后备,而不是嵌套在其他条件中。
稍加努力,我们就可以完全消除嵌套条件,从而提供更具可读性的内容,如下所示:
// 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);
}
其中一些条件可能并不完全符合预期,但通过将它们分开,我们现在可以更轻松地遵循逻辑并进行适当的更改。
TA贡献1817条经验 获得超6个赞
您的代码在执行过程中可能有问题,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条经验 获得超6个赞
经过一些质疑和研究,我得出的结论是,硬代码对任何人都没有帮助,在团队合作和项目发展方面。我决定通过以下方式实现代码:
$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 关注
- 125 浏览
添加回答
举报