Created
December 31, 2018 02:03
-
-
Save joaorobertopb/95d6aff2b31788a401b7027a4b9a77f3 to your computer and use it in GitHub Desktop.
Princípio da responsabilidade única do SOLID sendo aplicando na refatoração de uma função.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Ruim: | |
function emailClients(array $clients): void | |
{ | |
foreach ($clients as $client) { | |
$clientRecord = $db->find($client); | |
if ($clientRecord->isActive()) { | |
email($client); | |
} | |
} | |
} | |
// Bom: | |
function emailClients(array $clients): void | |
{ | |
$activeClients = activeClients($clients); | |
array_walk($activeClients, 'email'); | |
} | |
function activeClients(array $clients): array | |
{ | |
return array_filter($clients, 'isClientActive'); | |
} | |
function isClientActive(int $client): bool | |
{ | |
$clientRecord = $db->find($client); | |
return $clientRecord->isActive(); | |
} | |
// Reference: https://github.com/jupeter/clean-code-php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exemplo fantástico! Senti um "blown" na minha cabeça, com certeza terei uma otima diversão essa semana refatorando meu código. :) 👍