Forked from joaorobertopb/srp-solid-functions-example.php
Created
June 2, 2020 16:44
-
-
Save RodrigoPauletti/71353097520f5c4f6475d0be300511b3 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 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