PSR-2: default value assignment in method arguments

163 views
Skip to first unread message

mplx

unread,
Jan 24, 2013, 6:33:06 PM1/24/13
to php-f...@googlegroups.com
Dear all,

first of all, thank you very much for creation of the PSR guides.

As I'm adopting legacy code to PSR guidelines and had an discussion about a specific problem with a fellow programmer I'd like to request a quote from you regarding my interpretation of PSR-2, more precisly regarding the assignment of default values in method arguments.

While it is good practice (and it's also used in the sample code in chapters 4.3 and 4.4[1]) to have one space character between the argument and the default value it seems to me that the spacing itself is neither a requirement (MUST) or a recommendation (SHOULD) in the PSR-2 definition. 

While the code sample is prefixed with "[..]Note the placement of parentheses, commas, spaces, and braces...[]" I don't consider this as a definition in terms of the guideline as all other spacing requirements are defined explicitly - there's no rule like "There MUST|SHOULD be one space character before and after the operator in the definition of default values". 

Also chapter 7 states that practices regarding operators and assignments are intentionally omitted, so wouldn't it be inconsistent to omitt variable assignments but not assignments of default values?

So, after a lot of explanations, would you consider following code lines valid styling in terms of PSR-2 guidelines?

public function fooA($a, $b = '')
{
}

public function fooB($a, $b='')
{
}

public function fooC($a, $b=    '')
{
}

The PSR2 ruleset included in phpcs considers the definition of the default values in sample functions fooB and fooC as errors (see also PEAR bug #19798[3] and issue on github[2]), the PSR ruleset from Klaus Silveira[4] as valid code styling.

Best regards,
mplx



Reply all
Reply to author
Forward
0 new messages