-
-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore trailing if in scope for early exit #574
Comments
Same issue here. For those cases, the early return makes the code less readable and more verbose: $errors = [];
foreach ($values as $value) {
$error = $value->validate();
if ($error === null) {
continue;
}
$errors[] = $error;
} In that sense, I think an option |
Simple ignore would lead to mixture of EarlyExits and TrailingIfs across codebase. In my opinion you should either require EarlyExit or require TrailingIf.
|
Suggestion: what about ignoring trailing function foo() {
$var = function_call();
if ($var === true) {
call_other_function();
}
}
// Versus
function foo() {
$var = function_call();
if ($var !== true) {
return;
}
call_other_function();
} |
New option implemented in 055af01 |
To be honest i like this new option, but don't like it allows no early exit if only one line - it should be more like one instruction, let's take a this example: protected function addResultBasedOnCondition($budgetEnded, $timeEnded, $timeAlmostOver, $campaignUnderPerforming): void
{
if ($budgetEnded) {
$timeAlertNotificationsCollection->addResult(
new TimeAlertNotificationDto($campaignName, $brandName, Notification::TYPE_TIME_ALERT_BUDGET_ENDED)
);
}
if ($timeEnded) {
$timeAlertNotificationsCollection->addResult(
new TimeAlertNotificationDto($campaignName, $brandName, Notification::TYPE_TIME_ALERT_TIME_ENDED)
);
}
if ($timeAlmostOver) {
$timeAlertNotificationsCollection->addResult(
new TimeAlertNotificationDto($campaignName, $brandName, Notification::TYPE_TIME_ALERT_TIME_ALMOST_OVER)
);
}
if ($campaignUnderPerforming) {
$timeAlertNotificationsCollection->addResult(
new TimeAlertNotificationDto($campaignName, $brandName, Notification::TYPE_TIME_ALERT_CAMPAIGN_IS_UNDERPERFORMING)
);
}
} It will require early exit on protected function addResultBasedOnCondition($budgetEnded, $timeEnded, $timeAlmostOver, $campaignUnderPerforming): void
{
if ($budgetEnded) {
$timeAlertNotificationsCollection->addResult(
new TimeAlertNotificationDto($campaignName, $brandName, Notification::TYPE_TIME_ALERT_BUDGET_ENDED)
);
}
if ($timeEnded) {
$timeAlertNotificationsCollection->addResult(
new TimeAlertNotificationDto($campaignName, $brandName, Notification::TYPE_TIME_ALERT_TIME_ENDED)
);
}
if ($timeAlmostOver) {
$timeAlertNotificationsCollection->addResult(
new TimeAlertNotificationDto($campaignName, $brandName, Notification::TYPE_TIME_ALERT_TIME_ALMOST_OVER)
);
}
if ($campaignUnderPerforming) {
$timeAlertNotificationsCollection->addResult(new TimeAlertNotificationDto($campaignName, $brandName, Notification::TYPE_TIME_ALERT_CAMPAIGN_IS_UNDERPERFORMING));
}
} Then there is no error reported. Can this somehow be improved? @kukulich |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
The EarlyExit sniff has the setting
ignoreStandaloneIfInScope
. What about ignoringif
s that are the trailing part of the scope, and not onlyif
s that are the only part of the scope? Then also such cases could be ignored:In addition to the examples shown in #371 and #409.
Thank you for your consideration and great work!
The text was updated successfully, but these errors were encountered: