Spaces and PSR-1 , PSR-2

749 views
Skip to first unread message

Hari K T

unread,
Jul 8, 2012, 11:57:29 AM7/8/12
to php...@googlegroups.com
Hey guys, 

I love to clarify . Can we place spaces like this for PSR-1 , PSR-2 or only one space ?

    public function __construct(
        Context       $context,
        Stdio           $stdio,
        Getopt        $getopt
    )

as like 

        $this->context = $context;
        $this->stdio   = $stdio;
        $this->getopt  = $getopt;

I was trying with https://github.com/squizlabs/PHP_CodeSniffer/ which was reporting error for it . Wondering is it not possible ?

Thanks

Hari K T
M: +91-9388758821 | W: http://harikt.com/blog

Paul M. Jones

unread,
Jul 8, 2012, 12:43:39 PM7/8/12
to Hari K T, php...@googlegroups.com

On Jul 8, 2012, at 10:57 AM, Hari K T wrote:

> Hey guys,
>
> I love to clarify . Can we place spaces like this for PSR-1 , PSR-2 or only one space ?
>
> public function __construct(
> Context $context,
> Stdio $stdio,
> Getopt $getopt
> )
>
> as like
>
> $this->context = $context;
> $this->stdio = $stdio;
> $this->getopt = $getopt;
>
> I was trying with https://github.com/squizlabs/PHP_CodeSniffer/ which was reporting error for it . Wondering is it not possible ?

PSR-1 and 2 are silent regarding inter-line alignment. So, you can do it if you like, or not if you don't like, as far as those PSRs are concerned.

My feeling is that a codesniffer for PSR-1/2 (per se) should ignore inter-line alignment spacing entirely.


-- pmj

Greg Sherwood

unread,
Jul 8, 2012, 7:25:18 PM7/8/12
to php...@googlegroups.com, Hari K T
On Monday, 9 July 2012 02:43:39 UTC+10, pmjones wrote:

PSR-1 and 2 are silent regarding inter-line alignment.  So, you can do it if you like, or not if you don't like, as far as those PSRs are concerned.

My feeling is that a codesniffer for PSR-1/2 (per se) should ignore inter-line alignment spacing entirely.

In the absence of defined standards around things like type hint spacing, I have to use the sample code provided in the standard. PSR-2 is highly detailed in some areas and then completely absent in others, but it does have good example code and does repeatedly say to note the spacing in the examples provided. Note that the same is true of all coding standards. It's very hard to work out everything a developer will try and do before you start trying to automate the checking process over all sort of code.

The PSR-2 example for a muli-line function declaration includes 3 arguments, 1 of which has a type hint, but that's all we've got. If you are trying to get your code to conform to the coding style that PSR-2 outlines, I can't see how aligning function arguments with additional spacing does that. Even though the rule is not explicitly stated, the purpose of a coding standard is to try and get all code to look like it was written by the same team (or even the same person), so the examples serve as a really good way of figuring out the intention of the authors when they didn't fully document their intent with MUSTS and SHOULDs.

The biggest problem with standards is always ambiguity (well, after getting people to agree to them in the first place). If the standard is not clear, I think it needs to be made clear with a minor edit. So if the outcome of this conversation is that you can do whatever spacing you want around type hints (only in multi-line declarations?) then I think PSR-2 should include a section about optional alignment, like the PEAR standard does for equals sign alignment. But if the decision is that code should follow the implied standards in the PSR-2 sample code, then maybe a message to that effect should be added and automated checkers and fixers can work with that.

Paul M. Jones

unread,
Jul 8, 2012, 9:12:55 PM7/8/12
to php...@googlegroups.com, Hari K T

On Jul 8, 2012, at 6:25 PM, Greg Sherwood wrote:

> The biggest problem with standards is always ambiguity (well, after getting people to agree to them in the first place). If the standard is not clear, I think it needs to be made clear with a minor edit. So if the outcome of this conversation is that you can do whatever spacing you want around type hints (only in multi-line declarations?) then I think PSR-2 should include a section about optional alignment, like the PEAR standard does for equals sign alignment. But if the decision is that code should follow the implied standards in the PSR-2 sample code, then maybe a message to that effect should be added and automated checkers and fixers can work with that.

Yeah, it's a tough position to be in. The way-back original long-form PSR-1 covered inter-line alignment, globals, ternaries, assignment, and lots of other things. Those ended up getting removed for various reasons; their epitaph is at the conclusion of PSR-2:

> There are many elements of style and practice intentionally omitted by this guide. These include but are not limited to:
>
> • Declaration of global variables and global constants
>
> • Declaration of functions
>
> • Operators and assignment
>
> • Inter-line alignment
>
> • Comments and documentation blocks
>
> • Class name prefixes and suffixes
>
> • Best practices
>
> Future recommendations MAY revise and extend this guide to address those or other elements of style and practice.

In my opinion, Adding these things on should probably not be an edit to PSR-2, but the subject of a separate recommendations extending from PSR-2.


-- pmj

Greg Sherwood

unread,
Jul 8, 2012, 11:33:26 PM7/8/12
to php...@googlegroups.com, Hari K T
On Monday, 9 July 2012 11:12:55 UTC+10, pmjones wrote:

In my opinion, Adding these things on should probably not be an edit to PSR-2, but the subject of a separate recommendations extending from PSR-2.


If it was added on and became (for example) PSR-3, then it would be equally as obvious. If you know PSR-3 includes all of PSR-2 and specifically defines multi-line function declaration spacing, it is easy to see PSR-2 as a relaxed version of PSR-3 and there is certainly no confusion for automated tools or developers.

So nice idea. Although the recommended standards would start to pile up.

I'm late to the group, but I wonder if you've considered having a single standard with various levels, as is the case with WCAG2 A, AA, AAA. The standard itself contains everything, but every element is considered to belong to one of the levels. So you can always conform to WCAG2, but the level at which you conform is up to you. A is considered the bare minimum for web accessibility while AAA is considered much harder to conform to. AA is a nice balance and used a lot by government departments here in AU.

The reason I bring that up is that PSR-1 and 2 feel a lot like levels A and AA (0 feels like something else entirely) while the stuff you ended up throwing away feels like AAA.

It would be a wonderful world if everyone used the same standard when writing PHP code (JS would be magically as well). Even if all the projects just implemented it at different levels, you'd still know you were making your project compatible with others, even if the other projects were all going for hard-core AAA compliance and you were just sitting at A.
Reply all
Reply to author
Forward
0 new messages