How can I reduce the cyclomatic complexity of a function that returns a value dependent on the cartesian product of 3 booleans? How can I make the following code look more clean?
This is for a school project, and is not really a requirement for the assignment itself, but I usually find myself writing functions that rely on a - sometimes - rather complex truth table. I'm thinking that this cannot be the best way to do it.
public function getDiscount( $values ) {
$res = new stdClass();
$res->code = 400;
if ( ! is_bool( $values['new_customer'] ) || ! is_bool( $values['loyalty_card'] ) || ! is_bool( $values['coupon'] ) ) {
$res->data = "Missing inputs";
return $res;
}
if ( $values['new_customer'] == true && $values['loyalty_card'] == true && $values['coupon'] == true ) {
$res->data = "Invalid input";
return $res;
}
if ( $values['new_customer'] == true && $values['loyalty_card'] == true && $values['coupon'] == false ) {
$res->data = "Invalid input";
return $res;
}
$res->code = 200;
if ( $values['new_customer'] == true && $values['loyalty_card'] == false && $values['coupon'] == true ) {
$res->data = 20;
return $res;
}
if ( $values['new_customer'] == true && $values['loyalty_card'] == false && $values['coupon'] == false ) {
$res->data = 15;
return $res;
}
if ( $values['new_customer'] == false && $values['loyalty_card'] == true && $values['coupon'] == true ) {
$res->data = 30;
return $res;
}
if ( $values['new_customer'] == false && $values['loyalty_card'] == true && $values['coupon'] == false ) {
$res->data = 10;
return $res;
}
if ( $values['new_customer'] == false && $values['loyalty_card'] == false && $values['coupon'] == true ) {
$res->data = 20;
return $res;
}
if ( $values['new_customer'] == false && $values['loyalty_card'] == false && $values['coupon'] == false ) {
$res->data = 0;
return $res;
}
$res->code = 400;
$res->data = "Invalid input";
return $res;
}