Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 3.7.2

Bug #19798 PSR-2: default value assignment in method arguments
Submitted: 2013-01-23 21:39 UTC
From: mplx Assigned: squiz
Status: Closed Package: PHP_CodeSniffer (version 1.4.3)
PHP Version: 5.3.20 OS: Cygwin
Roadmaps: (Not assigned)    
Subscription  


 [2013-01-23 21:39 UTC] mplx (Martin Pircher)
Description: ------------ Assignment operations are omitted intentionally from the PSR-2 ruleset (see PSR-2 chapter 7). Current phpcs PSR-2 ruleset however enforces blanks for assignment of default values in method arguments. See also https://github.com/php-fig/fig-standards/issues/82 Test script: --------------- public foo ($arg='default') { } Expected result: ---------------- phpcs considers this valid PSR-2 coding style Actual result: -------------- Incorrect spacing between argument "$arg" and equals sign; expected 1 but found 0 Incorrect spacing between default value and equals sign for argument "$arg"; expected 1 but found 0

Comments

 [2013-01-23 21:49 UTC] mplx (Martin Pircher)
Testcode: <?php public function foo ($arg = 'default') { }
 [2013-01-23 21:50 UTC] mplx (Martin Pircher)
<?php public function foo ($arg='default') { }
 [2013-01-24 04:32 UTC] squiz (Greg Sherwood)
-Status: Open +Status: Feedback -Assigned To: +Assigned To: squiz
Section 4.3: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2- coding-style-guide.md#43-methods says: "A method declaration looks like the following. Note the placement of parentheses, commas, spaces, and braces:" And is then followed by an example that shows a single space before and after the equals sign for default values. So when I wrote the standard, I ensured that it checked for the correct spacing, comma and bracket placement as defined in that example. This is common in many coding standards because a bit of sample code is always easier to write than a complete list of every rule. Although I still need to code in every rule when I do a standard. Note that section 7 talks about assignments being excluded, which I believe means variable assignments and not assignment of default values, as these are explicitly referred to in the section 4.3 example. If you disagree with me (I am not a PHP FIG voting member) then the best place to ask about the standard is on the google groups page: https://groups.google.com/forum/?fromgroups=#!forum/php-fig-cs Note this is a new group they started to remove the CS discussions from the main group, so it is currently empty. But someone has to be the first to post :)
 [2013-01-25 04:29 UTC] mplx (Martin Pircher)
Greg, thanks for your comment on this issue. I agree with you that it's best practice to include spaces and I would prefer it to have it included in PSR2 for both - default values and variable assignment - to have a consistent styling. As a lot of other issues related to spacing have their own rules I doubt it's valid to construct a rule for this specific issue just through some sample code. If the styling in the sample is part of the guidelines there has to be a rule about it. In the end I just like to have some clarification about how to refactor legacy code to valid PSR2 so I'm happy with any solution. Following your advise I'll post to the Google group. Best regards, mplx :)
 [2013-02-07 03:08 UTC] squiz (Greg Sherwood)
-Status: Feedback +Status: Closed
Looks like you're not getting much help at the Google group. I'm going to close this, but please re-open if you get more information.