PSR-2 Errata - Multi-line arguments

778 views
Skip to first unread message

Phil Sturgeon

unread,
Sep 10, 2013, 10:23:49 AM9/10/13
to php...@googlegroups.com
It looks fairly certain that single-line callbacks are not PSR-2, but single-line arrays are. Thats ok, seems to be in the letter of the standard and the intent is the same - thats what im trying to discern here. No change required.


Example A)
somefunction($foo, $bar, [
  // ...
]);
Example B)
somefunction($foo, $bar, [
  // ...
], $ban);
Please reply with one of the following answers.

1. A and B are both invalid PSR-2.
2. A is/should be valid PSR-2.
3. A and B are/should be valid PSR-2

I am adding the complication here of "arguments after the callback" after Paul M Jones raised his concerns on GitHub.

Just answer 1, 2 or 3 and I'll get the pull request updated accordingly.

Paul M. Jones

unread,
Sep 10, 2013, 10:46:57 AM9/10/13
to php...@googlegroups.com

On Sep 10, 2013, at 9:23 AM, Phil Sturgeon <em...@philsturgeon.co.uk> wrote:

> Example A)
> somefunction($foo, $bar, [
> // ...
> ]);
>
> Example B)
> somefunction($foo, $bar, [
> // ...
> ], $ban);
>
> Please reply with one of the following answers.
>
> 1. A and B are both invalid PSR-2.
> 2. A is/should be valid PSR-2.
> 3. A and B are/should be valid PSR-2
>
> I am adding the complication here of "arguments after the callback" after Paul M Jones raised his concerns on GitHub.
>
> Just answer 1, 2 or 3 and I'll get the pull request updated accordingly.

Per notes elsewhere, I answer with (2): that is, A is valid, but B is not.


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


Donald Gilbert

unread,
Sep 10, 2013, 4:09:38 PM9/10/13
to php...@googlegroups.com
3

justin

unread,
Sep 10, 2013, 4:34:48 PM9/10/13
to php...@googlegroups.com
I'd be "2" *but* array_map, array_filter and friends have their arguments in the wrong order:

Example C)

array_map(function($foo) {
  // ...
}, $bar);

… so that complicates things :(

--j


--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/8280f3e5-da43-4289-8e09-56c5b8f742c9%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
http://justinhileman.com

Jeremy Lindblom

unread,
Sep 10, 2013, 4:36:47 PM9/10/13
to php...@googlegroups.com
Justin, I was thinking the same thing.

Phil, this is a trick question, isn't it? :-b

--
Jeremy Lindblom
PHP Software Engineer at Amazon Web Services
Co-author of the AWS SDK for PHP
Co-organizer of the Seattle PHP User Group
Keys/Vocals for Gigawatt Band


justin

unread,
Sep 10, 2013, 4:48:45 PM9/10/13
to php...@googlegroups.com

On Tue, Sep 10, 2013 at 1:44 PM, Paul M. Jones <pmjo...@gmail.com> wrote:
Yay for consistent order of arguments in PHP! /me rolls eyes

I opine that this calls for the use of an explaining variable; e.g.:

    $map = function ($foo) {
        // ...
    };
    array_map($map, $bar);



So not only are you keeping me from inlining simple functions:

    $ids = array_map(function($i) { return $i->id; }, $data);

… but you're forcing me to move them into another variable and complicate things further?

    $map = function($) {
        return $i->id;
    };
    $ids = array_map($map, $data);

Come on, man.

--j

Paul M. Jones

unread,
Sep 10, 2013, 4:57:13 PM9/10/13
to php...@googlegroups.com
Nobody's forcing anyone to do anything. I am presenting an opinion. If this is the heaviest burden in your coding style life, then I salute you.

I personally like single-line function definitions in limited cases, but it seems that to get them, we need to amend PSR-2, and I don't think anyone here is up for that.

Paul M. Jones

unread,
Sep 10, 2013, 4:44:19 PM9/10/13
to php...@googlegroups.com

On Sep 10, 2013, at 3:34 PM, justin <jus...@justinhileman.info> wrote:

> I'd be "2" *but* array_map, array_filter and friends have their arguments in the wrong order:
>
> Example C)
>
> array_map(function($foo) {
> // ...
> }, $bar);
>
>
> … so that complicates things :(

Yay for consistent order of arguments in PHP! /me rolls eyes

I opine that this calls for the use of an explaining variable; e.g.:

$map = function ($foo) {
// ...
};
array_map($map, $bar);


Karsten Dambekalns

unread,
Sep 11, 2013, 3:31:35 AM9/11/13
to php...@googlegroups.com
Hi.

A question on what would be valid already. This?!

someFunction(
  $foo,
  $bar,
  [
  // … can even be a closure with more indentation
  ],
  $ban
);

So why the need for another option? Even if we (TYPO3 Flow) are not following PSR-2, I'd vote 1) because there is already a valid way to express this.

Regards,
Karsten

On 10. September2013 at 16:23:54 , Phil Sturgeon (em...@philsturgeon.co.uk) wrote:
>Please reply with one of the following answers.
>
>1. A and B are both *invalid* PSR-2.
>2. A is/should be valid PSR-2.
>3. A and B are/should be *valid* PSR-2


Paul M. Jones

unread,
Sep 11, 2013, 8:21:45 AM9/11/13
to php...@googlegroups.com

On Sep 11, 2013, at 2:31 AM, Karsten Dambekalns <kar...@dambekalns.de> wrote:

> Hi.
>
> A question on what would be valid already. This?!
>
> someFunction(
> $foo,
> $bar,
> [
> // … can even be a closure with more indentation
> ],
> $ban
> );

I would say so, yes.

Phil Sturgeon

unread,
Sep 11, 2013, 10:42:14 AM9/11/13
to php...@googlegroups.com
PLEASE ONLY REPLY WITH A, B OR C.

I'll be available on IRC for most of the day if you would like to discuss. Otherwise, make a new thread or something.

Moisa Teodor

unread,
Sep 11, 2013, 11:15:36 AM9/11/13
to php...@googlegroups.com
3


On Wed, Sep 11, 2013 at 5:42 PM, Phil Sturgeon <em...@philsturgeon.co.uk> wrote:
PLEASE ONLY REPLY WITH A, B OR C.

I'll be available on IRC for most of the day if you would like to discuss. Otherwise, make a new thread or something.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Doru Moisa
web: http;//moisadoru.wordpress.com
tel: +40 720 861 922
Bucharest, Romania

Phil Sturgeon

unread,
Sep 13, 2013, 3:19:26 PM9/13/13
to php...@googlegroups.com
Ok I'm going to ditch my attempt at having this be a poll, and try to answer some of the questions:



On Tuesday, 10 September 2013 16:36:47 UTC-4, Jeremy Lindblom wrote:
Justin, I was thinking the same thing.

Phil, this is a trick question, isn't it? :-b

--
Jeremy Lindblom
PHP Software Engineer at Amazon Web Services
Co-author of the AWS SDK for PHP
Co-organizer of the Seattle PHP User Group
Keys/Vocals for Gigawatt Band


On Tue, Sep 10, 2013 at 1:34 PM, justin <jus...@justinhileman.info> wrote:
I'd be "2" *but* array_map, array_filter and friends have their arguments in the wrong order:

Example C)
array_map(function($foo) {
  // ...
}, $bar);

… so that complicates things :(

It is not a trick question at all sadly. Currently this example is invalid PSR-2 (according to CodeSniffer):
array_map(function($foo) {
  // ...
}, $bar);
Thats invalid. But we all do it. The current PR is saying that multi-line arguments in general should be allowed, but Paul is suggesting only if the multi-line argument is the LAST argument.

In this example as there is an argument after the multi-line argument, he'd like to say that is invalid, and to make it valid you have to shove it in a variable. 

I am trying to find out if we A) do it the way the PR says - which means this example becomes valid explicitly or B) We do the Paul thing and I update my PR saying that multi-line arguments are fine as long as they go at the end. 

Does that make sense? I feel this has become complicated and I don't want that to be the case.


On Wednesday, 11 September 2013 03:31:35 UTC-4, Karsten Dambekalns wrote:
Hi.

A question on what would be valid already. This?!

someFunction(
  $foo,
  $bar,
  [
  // … can even be a closure with more indentation
  ],
  $ban
);

So why the need for another option? Even if we (TYPO3 Flow) are not following PSR-2, I'd vote 1) because there is already a valid way to express this.

Yes that is already valid. Did you read my write-up on this

You're saying you like code like this?

Every project in the FIG uses multi-line arguments in function calls the way that PSR-2 CodeSniffer currently forbids. That means that there is not a single project amongst us following PSR-2 correctly, or, PSR-2 CodeSniffer is wrong.

CalEvans

unread,
Sep 13, 2013, 3:27:30 PM9/13/13
to php...@googlegroups.com
3

=C=

Taylor Otwell

unread,
Sep 13, 2013, 3:32:16 PM9/13/13
to php...@googlegroups.com
3

On Tuesday, September 10, 2013 9:23:49 AM UTC-5, Phil Sturgeon wrote:

Phil Sturgeon

unread,
Sep 13, 2013, 3:33:23 PM9/13/13
to php...@googlegroups.com


On Wednesday, 11 September 2013 10:42:14 UTC-4, Phil Sturgeon wrote:
PLEASE ONLY REPLY WITH A, B OR C.

Grumpy Phil can't tell the difference between numbers and letters. 1, 2 or 3 not A, B, C.

I screwed up this whole thread with my confusing poll. Sorry. 

John Mertic

unread,
Sep 13, 2013, 3:34:19 PM9/13/13
to php...@googlegroups.com
3
> --
> You received this message because you are subscribed to the Google Groups
> "PHP Framework Interoperability Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to php-fig+u...@googlegroups.com.
> To post to this group, send email to php...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/php-fig/5a550bd3-9c9c-4121-8367-04c7a353a4f5%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>


--
John Mertic
jme...@gmail.com | http://jmertic.wordpress.com

Paul M. Jones

unread,
Sep 13, 2013, 3:36:26 PM9/13/13
to php...@googlegroups.com

On Sep 13, 2013, at 2:19 PM, Phil Sturgeon <em...@philsturgeon.co.uk> wrote:

> Ok I'm going to ditch my attempt at having this be a poll, and try to answer some of the questions:

...

> Currently this example is invalid PSR-2 (according to CodeSniffer):
>
> array_map(function($foo) {
> // ...
> }, $bar);
>
> Thats invalid. But we all do it.

Well, not *all* of us. ;-)


> The current PR is saying that multi-line arguments in general should be allowed, but Paul is suggesting only if the multi-line argument is the LAST argument.

Mostly; the reason my opinion leans that way is because $bar is on a different line from the first argument.


> In this example as there is an argument after the multi-line argument, he'd like to say that is invalid, and to make it valid you have to shove it in a variable.

Not necessarily; a valid version, to me, could also be this:

array_map(
function($foo) {
// ...
},
$bar
);

No explaining variable, everything on its own line, yada yada yada.

Drak

unread,
Sep 13, 2013, 3:38:23 PM9/13/13
to php...@googlegroups.com
On 13 September 2013 20:36, Paul M. Jones <pmjo...@gmail.com> wrote:
Not necessarily; a valid version, to me, could also be this:

    array_map(
        function($foo) {
            // ...
        },
        $bar
    );

This is much better than having a weird exception of $bar acceptable only as a the last arg. 

Phil Sturgeon

unread,
Sep 13, 2013, 4:51:21 PM9/13/13
to php...@googlegroups.com
So you're "2" Drak? 

Drak

unread,
Sep 13, 2013, 5:10:03 PM9/13/13
to php...@googlegroups.com
Yes
 

Michael C

unread,
Sep 13, 2013, 5:16:06 PM9/13/13
to php...@googlegroups.com

2

 

Thanks,

Michael C

 

--

You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.

Beau Simensen

unread,
Sep 13, 2013, 5:52:48 PM9/13/13
to php...@googlegroups.com
To me it isn't a weird exception of having a trailing $variable as the last arg. I consider an array [] or a function () { } to be one argument. If they happen to span multiple lines themselves, that does not change the fact that they are one argument and for all intents and purposes are "inline."

Said another way:

... an array or anonymous function argument may span multiple lines.

vs.

... an array or anonymous function argument may span multiple lines only if it is the last argument in the argument list.


I voted for 3 earlier today on IRC.

Drak

unread,
Sep 14, 2013, 2:50:06 AM9/14/13
to php...@googlegroups.com
On 13 September 2013 22:52, Beau Simensen <sime...@gmail.com> wrote:
On 13 September 2013 20:36, Paul M. Jones <pmjo...@gmail.com> wrote:
Not necessarily; a valid version, to me, could also be this:

    array_map(
        function($foo) {
            // ...
        },
        $bar
    );

This is much better than having a weird exception of $bar acceptable only as a the last arg. 

To me it isn't a weird exception of having a trailing $variable as the last arg. I consider an array [] or a function () { } to be one argument. If they happen to span multiple lines themselves, that does not change the fact that they are one argument and for all intents and purposes are "inline."

Said another way:

... an array or anonymous function argument may span multiple lines.

Yes, that's my feeling. 

Phil Sturgeon

unread,
Sep 14, 2013, 10:18:06 PM9/14/13
to php...@googlegroups.com
I'm a little confused Drak, you said "2" initially. Beau is saying "3", and you're agreeing with him. So... you're contradicting yourself a little there :)

We have a lot of people going with 3 anyhow, so I'm just going to go ahead and put the pull request in for a vote. If anyone has any suggestions on the wording please let me know before Sunday afternoon EST.

Drak

unread,
Sep 15, 2013, 3:45:05 AM9/15/13
to php...@googlegroups.com
Sorry Phil, I quoted the wrong post, I mean to quote PMJ's

    array_map(
        function($foo) {
            // ...
        },
        $bar
    );

I was saying that looks fine. Option 3 is not OK for me.


--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.

Phil Sturgeon

unread,
Sep 16, 2013, 10:16:22 AM9/16/13
to php...@googlegroups.com
So the vote has begun:


If you answered with "1" here you want to -1 that vote, but if you answered "2" or "3" here then you want to +1 it. 

2 and 3 are only slightly different, and +1 on this errata will allow you to do it either way, you just use personal preference to dictate wether you should add trailing arguments after the multi-line argument. If you don't want to then don't, but PSR-2 wont shout at you and neither will CodeSniffer.
Reply all
Reply to author
Forward
0 new messages