I have this method rated B on Scrutinizer (direct link: https://scrutinizer-ci.com/g/sineverba/domotic-panel/inspections/76996c9f-543f-43b4-9475-c64fe810a278/code-structure/operation/App%5CHttp%5CControllers%5CApi%5CPublicIpController%3A%3Aupdate)
public function update()
{
try {
$data = array();
$update = false;
$string_previous_public_ip = null;
$current_public_ip = $this->getGateway()->fetchPublicIp($this->getIp());
$previous_public_ip = $this->getGateway()->getLastRecord();
$data[ 'ip_address' ] = $current_public_ip;
if (isset($previous_public_ip->ip_address)) {
$string_previous_public_ip = $previous_public_ip->ip_address;
$data[ 'id' ] = $previous_public_ip->id;
}
if ($current_public_ip != $string_previous_public_ip) {
$update = $this->updateOrCreate($data);
}
return response()->json([
'updated' => $update
], 200);
} catch (ConnectionError $e) {
// Will return error 500
} catch (ServiceError $e) {
// Will return error 500
} catch (\Exception $e) {
// Will return error 500
}
return response()->json([
'updated' => $update
], 500);
}
Can I lower the cyclomatic complexity? I did move yet a if/else for the update (updateOrCreate method I think it is clear his work), but it is not sufficient.
Thank you!