Amanzhol Zhaparov <balloon...@gmail.com>: Mar 27 02:49AM -0700
Hello PHP-FIG members,
I would like to start a discussion regarding a potential refinement or an
allowed alternative in the PER Coding Style (successor to PSR-12),
specifically concerning section *5.1 (if, elseif, else)*.
Currently, the standard strictly requires else and elseif to be on the same
line as the closing brace of the previous block:
PHP
if ($expr1) {
// body
} else {
// body
}
*The Problem: Code Folding & Scannability* In modern development
environments (JetBrains PhpStorm, VS Code, Sublime Text), the "Code
Folding" feature is essential for navigating large files. When a developer
collapses an if block following the PSR-12 standard, the else keyword often
gets hidden or visually merged into a single line like: if ($expr1) { ... }
else {
This makes it difficult to quickly scan the code to see if an alternative
logic branch exists without unfolding every block.
*The Proposal:* I propose to allow (or recommend as an alternative) placing
else and elseif on a new line, similar to the Allman style but keeping the
opening brace on the same line as the keyword:
PHP
if ($expr1) {
// if body
}
elseif ($expr2) {
// elseif body
}
else {
// else body
}
*Benefits:*
1.
*Improved Visual Hierarchy:* Each logical branch starts on its own line,
making the structure clear at a glance.
2.
*Better IDE Integration:* When the if block is folded, the else or
elseif remains clearly visible on the next line, preserving the "map" of
the logic.
3.
*Consistency:* It aligns with how many developers naturally separate
distinct logical steps.
I understand that PSR-12 aimed for vertical compactness, but with
high-resolution monitors and the heavy use of IDE folding features, the
"saved line" often comes at the cost of readability.
I would love to hear the community's thoughts on whether the PER Coding
Style could be updated to allow this formatting as a valid alternative.
Best regards, Amanzhol Zhaparov
|
|
|
Woody Gilk <woody...@gmail.com>: Mar 27 06:12AM -0500
On Fri, Mar 27, 2026 at 6:05 AM Amanzhol Zhaparov <balloon...@gmail.com>
wrote:
> "saved line" often comes at the cost of readability.
> I would love to hear the community's thoughts on whether the PER Coding
> Style could be updated to allow this formatting as a valid alternative.
I disagree and prefer the current style.
--
Woody Gilk (he/him)
https://www.shadowhand.com
|
|
|
Matteo Beccati <mat...@beccati.com>: Mar 27 12:15PM +0100
Hi,
Il 27/03/2026 12:12, Woody Gilk ha scritto:
>> Coding Style could be updated to allow this formatting as a valid
>> alternative.
> I disagree and prefer the current style.
100% with Woody here.
Cheers
--
Matteo
|
|
|
Mathieu Rochette <mat...@rochette.cc>: Mar 27 11:19AM
Hi!
it's not obvious what is the issue and how much better it would be if this was accepted, probably worth sharing before/after screenshots of the diff it makes in the different IDEs
regards,
Mathieu Rochette
|
|
|
Matteo Beccati <mat...@beccati.com>: Mar 27 12:49PM +0100
Hi,
Il 27/03/2026 12:19, Mathieu Rochette ha scritto:
> it's not obvious what is the issue and how much better it would be if
> this was accepted, probably worth sharing before/after screenshots of
> the diff it makes in the different IDEs
TBH I rarely use the feature and I hadn't noticed PhpStorm folds the
code in a weird way with if/elseif/else.
I do now, and I think it's JetBrains that should fix code folding,
especially given they otherwise seem to suppor PER Coding Style.
Cheers
--
Matteo
|
|
|
Woody Gilk <woody...@gmail.com>: Mar 27 07:41AM -0500
> code in a weird way with if/elseif/else.
> I do now, and I think it's JetBrains that should fix code folding,
> especially given they otherwise seem to suppor PER Coding Style.
Agreed. This should be reported to JetBrains as a bug, as opposed to a
change that affects everyone.
--
Woody Gilk (he/him)
https://www.shadowhand.com
|