Why this matters
Single-responsibility functions are easier to read, test, and reuse. Smaller functions tend to have fewer bugs and simplify isolating issues, while making future enhancements cleaner.
Write functions and methods so each does one well-defined task. If a function handles distinct steps (validation, processing, saving, notifying), refactor into smaller focused functions.
Single-responsibility functions are easier to read, test, and reuse. Smaller functions tend to have fewer bugs and simplify isolating issues, while making future enhancements cleaner.
Side-by-side examples engineers can pattern-match during review.
<?php
function processData(\$data) {
// validate
if (!validate(\$data)) return false;
// process
\$result = calculate(\$data);
// save
saveToDb(\$result);
// notify
sendNotification(\$result);
return true;
}
?><?php
function validateData(\$data): bool {
// validation logic
}
function calculateResult(\$data) {
// processing logic
}
function saveResult(\$res) {
// persistence logic
}
function notifyUser(\$res) {
// notification logic
}
if (validateData(\$input)) {
\$res = calculateResult(\$input);
saveResult(\$res);
notifyUser(\$res);
}
?><?php
function processData(\$data) {
// validate
if (!validate(\$data)) return false;
// process
\$result = calculate(\$data);
// save
saveToDb(\$result);
// notify
sendNotification(\$result);
return true;
}
?><?php
function validateData(\$data): bool {
// validation logic
}
function calculateResult(\$data) {
// processing logic
}
function saveResult(\$res) {
// persistence logic
}
function notifyUser(\$res) {
// notification logic
}
if (validateData(\$input)) {
\$res = calculateResult(\$input);
saveResult(\$res);
notifyUser(\$res);
}
?>From the same buckets as this rule.