Skip to content

Instantly share code, notes, and snippets.

@dorianneto
Created January 30, 2018 12:53
Show Gist options
  • Save dorianneto/14f7cd42981f0c24a77a2381b95bebaa to your computer and use it in GitHub Desktop.
Save dorianneto/14f7cd42981f0c24a77a2381b95bebaa to your computer and use it in GitHub Desktop.
Post: Refatorando código com Extract Method
<?php
$products = [
'Geladeira' => 1500.99,
'Fogão' => 350,
'Celular' => 1190.90
];
function format_currency($value): string {
return 'R$ ' . number_format($value, 2, ',', '.');
}
function email_to_sales_team() {
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
return mail($to, $subject, $message, $headers);
}
function before_add_shopping_cart(array $products): array {
$output = [
'amount' => count($products),
'total' => format_currency(array_sum($products))
];
$output['items'] = array_map('format_currency', $products);
email_to_sales_team();
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment