I finally updated phpcs last night and have run PSR2 validation against some of my projects. Things looked good at first until I ran into PSR-2 validation errors with the following:return $this->em->transactional(function () use ($session, $func) {return call_user_func_array($func, $session);});
I also discovered that the following somewhat standard Silex style code does not pass PSR-2 validation:$app->get('/hello/{name}', function ($name) use ($app) {return 'Hello '.$app->escape($name);});
Based on the existing PSR-2 documentation, should these both validate as valid PSR-2?
I was able to clear up the validation warning by making my code look like this. I find this altogether unacceptable so I'm hoping this is a bug in phpcs and not "as intended" PSR-2. :)$app->get('/hello/{name}',function ($name) use ($app) {return 'Hello '.$app->escape($name);});return $this->entityManager->transactional(function () use ($session, $func) {return call_user_func_array($func, $session);});
I am the PHP_CodeSniffer maintainer. I'm lurking around these lists, so you can post here with questions about the PSR standards in PHPCS, or you can post a bug on the project bug tracker if you prefer. The link is at the bottom of this page: https://github.com/squizlabs/PHP_CodeSniffer
In particular, this part: "Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line."
But hopefully that makes it clearer as to why PHPCS is reporting those errors.
Thanks for adding the actual errors to the context of my post. I appreciate it. I don't have an easy way to get the exact messages (due to how I'm using phpcs with my editor) so that was nice to see added.
In particular, this part: "Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line."So in other words since by definition an anonymous function is probably going to be multiple lines it automatically requires that any call made with an anonymous function has to be done the "one argument per line" way? Ugh. I hope this isn't true. :) If it is, so be it, but if there is ambiguity in how this can be interpreted (I've heard that at least phpstorm is doing PSR-2 code formatting and doesn't complain about the more compressed format) that could be problematic.
Can one or more fig members chime in on the spirit of the PSR-2 standard in this regard?
Greg, I'm relatively new to phpcs. I'm all about trying to follow PSR2 but this would be a deal breaker for me. I'd like to try and tweak the PSR2 bits such that in the case of passing anonymous functions in the ways I'm trying to use them it give me any problems. Where would I look to start trying to patch this for my local install? Do you think it would be easy to do so or am I in for a world of hurt? :)
Thanks for the tips, Greg. I found that the following was sufficient to stop seeing stuff pop up for my specific use cases:<?xml version="1.0"?><ruleset name="PSR2Lite"><description>A custom PSR2 standard.</description><rule ref="PSR2"><exclude name="PEAR.Functions.FunctionCallSignature.MultipleArguments"/></rule></ruleset>
It isn't clear to me what impact this is going to have on the standards checking, though. If I wanted to see exactly what disabling "PEAR.Functions.FunctionCallSignature.MultipleArguments" does, where would I look? Is it only in the code somewhere? Or is it documented on a website as well? I'm curious to know what other types of sins I may end up committing that I won't be aware of by disabling this. :)
From the phpcs error naming, they seem to be using "multiple lines"
detection which would definitely fail your example.
Does the same
issue happen if you include a concatenated string as the single
argument to fit code width over multiple lines? Probably does which, I
assume, is also wrong.
Might be something to consider in the future
for PSR-2 whenever we get around to having a workflow for revisions to
clear up such potential misunderstandings.
On 5 March 2013 21:29, Greg Sherwood <gshe...@gmail.com> wrote:
> First, congratulations on becoming a PHP-FIG voting member. That vote didn't
> take long ;)
Thanks :P
I've noted on the main mailing list that this needs comment from other
members.
For me at least, the OP's original example is completely legal under
PSR-2. That may be against the intended spirit of the PSR but I'm not
psychic and I don't expect those adopting the PSR to be either. The
only current reason to adopt the multiline interpretation is to avoid
CodeSniffer's or similar errors.
I think you meant this URL:
--
RJT
I believe the idea was simply to ensure that the file is "properly" terminated. Who really care how many blank lines there are at the end of the file as long as the last character is a LF?
On Friday, 10 May 2013 11:12:50 UTC+1, Richard Turner wrote:I believe the idea was simply to ensure that the file is "properly" terminated. Who really care how many blank lines there are at the end of the file as long as the last character is a LF?Then perhaps it needs to say that `every line MUST be terminated with a newline (LF) character`. The last line of the file is no exception, so it does not need to be mentioned at all.
<snip>
On the whole, it is all good stuff, but where there is some ambiguity, the intention probably needs to be spelled out.