PSR-2 Comments in Control Structures

648 views
Skip to first unread message

Phil Sturgeon

unread,
Sep 30, 2013, 10:12:58 PM9/30/13
to php-f...@googlegroups.com
How do people feel about the way CodeSniffer PSR-2 currently handles comments in control structures?


Greg is asking if they should be ignored completely so comments can go anywhere, or comments on the line directly preceding the } else { (or whatever) are treated as exceptions to indentation rule? 

Pádraic Brady

unread,
Oct 1, 2013, 5:18:34 AM10/1/13
to php-f...@googlegroups.com
I'm inclined to ignore comments completely. Comment indentation will tend to diverge quite a bit in my experience. You could put them before the control statement, after it and indented as for the inner code, on the same line as a declaration, etc. Even indented behind the next statement if horizontal space is at a premium for that section.

Paddy


--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group - Coding Style" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig-cs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

--
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
Zend Framework Community Review Team
Zend Framework PHP-FIG Representative



--

--
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
Zend Framework Community Review Team
Zend Framework PHP-FIG Representative

Michael C

unread,
Oct 1, 2013, 11:59:12 AM10/1/13
to php-f...@googlegroups.com

+1. I’d say comments can go wherever.

 

--

Thanks,

Michael C

 

Off-topic: Whoever has control over the CS ML could they change the reply-to setting so replying to an email goes to the ML, not the person.

Paul Dragoonis

unread,
Oct 15, 2013, 6:18:33 AM10/15/13
to php-f...@googlegroups.com
Ignore comments guys, it removes documentation creativity. Lets not take the piss with this PSR.

Cheers,
Paul

Paul M. Jones

unread,
Oct 15, 2013, 9:30:32 AM10/15/13
to Paul Dragoonis, php-f...@googlegroups.com

On Oct 15, 2013, at 5:18 AM, Paul Dragoonis <drag...@gmail.com> wrote:

> Ignore comments guys, it removes documentation creativity. Lets not take the piss with this PSR.

PSR-2 explicitly does not address comments. They are entirely outside the scope of PSR-2. CodeSniffer can do what it wants, but what it cannot say is that comments are either compliant or non-compliant with PSR-2, since PSR-2 does not specify either way.

My *personal* opinion is that this style ...

// Do something if yes
if ($foo === true) {
echo "Yep"

// Do something if nope
} elseif ($foo !== true) {
echo "Nope";

// Fuck knows
} else {
die("Universe Implosion");
}

... is a bit off. I would argue for a modified version, where the comments actually go in the blocks they are commenting, with an overall comment on the block as a whole:

// yep, nope, or fuck knows
if ($foo === true) {
// Do something if yes
echo "Yep"
} elseif ($foo !== true) {
// Do something if nope
echo "Nope";
} else {
// Fuck knows
die("Universe Implosion");
}


--
Paul M. Jones
pmjo...@gmail.com
http://paul-m-jones.com


Mike van Riel

unread,
Oct 16, 2013, 3:31:56 PM10/16/13
to php-f...@googlegroups.com
I agree with Paul Jones, PSR-2 does not prescribe how comments should be indented and if you interpret the indentation rules then I would argue that the current handling is correct; the indentation for a comment should, in my opinion, be equal to the code block where it is at.
Reply all
Reply to author
Forward
0 new messages