[PSR-12] Spacing for INSTEADOF and AS in trait trait use blocks

55 views
Skip to first unread message

Greg Sherwood

unread,
Apr 22, 2018, 7:59:39 PM4/22/18
to PHP Framework Interoperability Group
Hi,

I'm working on the PSR-12 standard for PHP_CodeSniffer. I have a question about this section at the bottom of 4.2:

When using the insteadof and as operators they must be used as follows taking note of indentation, spacing and new lines. 

<?php

class Talker
{
    use A, B, C {
        B::smallTalk insteadof A;
        A::bigTalk insteadof C;
        C::mediumTalk as FooBar;
    }
}

In this code example, insteadof and as have exactly one space on either side. The text above the example says to note spacing in general. But PSR-12 also contains this:

6. Operators
All binary and ternary (but not unary) operators MUST be preceded and followed by at least one space. This includes all arithmetic, comparison, assignment, bitwise, logical (excluding ! which is unary), string concatenation, type operators, trait operators (insteadof and as), and the single pipe operator (e.g. ExceptionType1 | ExceptionType2 $e).

Here, instead of and as must be surrounded by at least 1 space - not exactly 1 space.

So when I'm writing the checks for use blocks, should I be enforcing a single space around instead of and as, or should I enforce at least one space? Is this valid code?

<?php
class Talker
{
    use A, B, C {
        B::smallTalk    insteadof    A;
        A::bigTalk   insteadof C;
        C::mediumTalk as    FooBar;
    }
}

Thanks

Alexander Makarov

unread,
Apr 23, 2018, 4:04:54 PM4/23/18
to PHP Framework Interoperability Group
At least one may make sense for aligning:

Greg Sherwood

unread,
Apr 23, 2018, 6:31:07 PM4/23/18
to PHP Framework Interoperability Group
Yeah, it could. But when PSR-12 says to "take note of spacing" what is it referring to then? Does it just mean the indent? Does it mean spacing around the comma, or around the braces?

The examples with "take note" style comments are used throughout PSR-2 and again in PSR-12, but it makes it quite hard to figure out how to enforce the rules because you have to interpret so much from a piece of sample code and you never know if the authors meant for you to read so much into it. From just the one PSR-12 sample I posted originally:

class Talker
{
    use A, B, C {
        B::smallTalk insteadof A;
        A::bigTalk insteadof C;
        C::mediumTalk as FooBar;
    }
}

I see the following rules:
- exactly one space after use
- no spaces before commas
- exactly one space after commas
- all class names on the same line
- opening brace on same line as class names
- exactly one space before opening brace
- insteadof/as statements indented 4 spaces
- no blank lines between statements
- exactly 1 space before insteadof/as
- exactly 1 space after insteadof/as
- no spaces around double colons
- no space before semicolons
- closing brace on line by itself
- closing brace aligned with use keyword

It would be really really helpful if those rules were listed along with each sample. So rather than "indentation, spacing and new lines", the standard told you exactly what to take note of.

Greg
Reply all
Reply to author
Forward
0 new messages