Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RodrigoPauletti/71353097520f5c4f6475d0be300511b3 to your computer and use it in GitHub Desktop.
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.
<?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