NestedPositiveIfStatements
info on by default quick-fixGroup: Code style · PhpStorm name: NestedPositiveIfStatementsInspection
An if that is the only statement inside another if (without alternative branches) can be merged into its parent with &&; an if that is the only statement inside an else block can become else if. Both remove a nesting level.
Example
php
<?php
function merge_cases($u, $v, $w) {
if ($u) {
if ($v) {
go();
}
}
if ($u && $v) {
// explain
if ($w > 2) {
run();
}
}
if ($u ?? $v) {
if ($w) { run(); }
}
if ($u) {
if ($v) { run(); } else { stop(); }
} else { stop(); }
if ($u) {
if ($v) {}
} else {
stop();
}
if ($u || $v) {
if ($w) {}
}
if ($u) {
if ($v) {}
else { stop(); }
}
}
function else_cases($u, $v) {
if ($u) {
run();
} else {
// fallback
if ($v) {
stop();
} else {
halt();
}
}
}php
<?php
function merge_cases($u, $v, $w) {
if ($u && $v) {
go();
}
if ($u && $v && $w > 2) {
// explain
run();
}
if (($u ?? $v) && $w) { run(); }
if ($u && $v) { run(); } else { stop(); }
if ($u) {
if ($v) {}
} else {
stop();
}
if ($u || $v) {
if ($w) {}
}
if ($u) {
if ($v) {}
else { stop(); }
}
}
function else_cases($u, $v) {
if ($u) {
run();
} else if ($v) {
// fallback
stop();
} else {
halt();
}
}Reported:
- line 4: Merge this if statement into its parent construct.
- line 11: Merge this if statement into its parent construct.
- line 17: Merge this if statement into its parent construct.
- line 21: Merge this if statement into its parent construct.
- line 45: Merge this if statement into its parent construct.
Configure
In custos.json:
json
{
"rules": {
"NestedPositiveIfStatements": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore NestedPositiveIfStatements
/**
* @noinspection NestedPositiveIfStatementsInspection
*/