Skip to content

Instantly share code, notes, and snippets.

@joaorobertopb
Created December 31, 2018 02:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joaorobertopb/95d6aff2b31788a401b7027a4b9a77f3 to your computer and use it in GitHub Desktop.
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.
<?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
@Dannark
Copy link

Dannark commented Aug 13, 2020

Exemplo fantástico! Senti um "blown" na minha cabeça, com certeza terei uma otima diversão essa semana refatorando meu código. :) 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment