[PSR-12] How to deal with comments inside trait use blocks

75 views
Skip to first unread message

Greg Sherwood

unread,
Apr 23, 2018, 1:06:56 AM4/23/18
to PHP Framework Interoperability Group
Hi,

I'm working on the PSR-12 standard for PHP_CodeSniffer. I have a question about comments in general.

PSR-2 explicitly excluded comments so I ended up writing a lot of checks that just treat comments like whitespace. PSR-12 doesn't look like it does this, but it hardly mentions comments, so I'm not sure how to deal with them.

For example, I'm working on checks around use statements for traits. I can see that use statements need to be one per line, and the example code indicates that there should not be blank lines between these statements:

class ClassName
{
    use FirstTrait;
    use SecondTrait;
    use ThirdTrait;
}

What I don't know is:

1. Can you put blank lines between use statements? For example:

class ClassName
{
    use FirstTrait; 

    use SecondTrait;  
 
 
    use ThirdTrait;
}

2. Can you put comments in between use statements? What about before the first use statement? If so, how should it be done (with or without blank lines? how many blank lines?):

class ClassName
{
    // We need this for reasons.
    use FirstTrait;
    // We need these two more for reasons.
    use SecondTrait;
    use ThirdTrait;
}

or

class ClassName
{
    // We need this for reasons.
    use FirstTrait; 

    // We need these two more for reasons.
    use SecondTrait;
    use ThirdTrait;
}

Or some other way?

PSR-12 does say The use keyword used inside the classes to implement traits MUST be declared on the next line after the opening brace" so a comment before the use statement would contradict this, but I'm not sure if a comment needs to be excluded from this check.

Thanks

Rodrigo Aguilera

unread,
Dec 18, 2018, 12:28:06 PM12/18/18
to PHP Framework Interoperability Group
As we discussed here
we should enforce some standards on comments at least to be inline with the surrounding code.
Reply all
Reply to author
Forward
0 new messages