Skip to content

Commit b97faa1

Browse files
committed
Added support for the PSR2 multi-line arguments errata
The vote has not closed yet, but it will pass. The errata PR is here: php-fig/fig-standards#178
1 parent c01aee2 commit b97faa1

File tree

5 files changed

+185
-9
lines changed

5 files changed

+185
-9
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* PSR2_Sniffs_Methods_FunctionCallSignatureSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* PSR2_Sniffs_Methods_FunctionCallSignatureSniff.
17+
*
18+
* @category PHP
19+
* @package PHP_CodeSniffer
20+
* @author Greg Sherwood <[email protected]>
21+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
22+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
23+
* @version Release: @package_version@
24+
* @link http://pear.php.net/package/PHP_CodeSniffer
25+
*/
26+
class PSR2_Sniffs_Methods_FunctionCallSignatureSniff extends PEAR_Sniffs_Functions_FunctionCallSignatureSniff
27+
{
28+
29+
/**
30+
* If TRUE, multiple arguments can be defined per line in a multi-line call.
31+
*
32+
* @var bool
33+
*/
34+
public $allowMultipleArguments = false;
35+
36+
37+
/**
38+
* Processes single-line calls.
39+
*
40+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
41+
* @param int $stackPtr The position of the current token
42+
* in the stack passed in $tokens.
43+
* @param int $openBracket The position of the opening bracket
44+
* in the stack passed in $tokens.
45+
* @param array $tokens The stack of tokens that make up
46+
* the file.
47+
*
48+
* @return void
49+
*/
50+
public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
51+
{
52+
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
53+
$compareLine = $tokens[$openBracket]['line'];
54+
55+
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
56+
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
57+
$i = $tokens[$i]['parenthesis_closer'];
58+
$compareLine = $tokens[$i]['line'];
59+
continue;
60+
} else if ($tokens[$i]['code'] === T_CLOSURE) {
61+
$i = $tokens[$i]['scope_closer'];
62+
$compareLine = $tokens[$i]['line'];
63+
continue;
64+
} else if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
65+
$i = $tokens[$i]['bracket_closer'];
66+
$compareLine = $tokens[$i]['line'];
67+
continue;
68+
}
69+
70+
if ($tokens[$i]['line'] !== $compareLine) {
71+
return true;
72+
}
73+
}//end for
74+
75+
return false;
76+
77+
}//end isMultiLineCall()
78+
79+
80+
}//end class
81+
?>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
somefunction($foo, $bar, [
3+
// ...
4+
], $baz);
5+
6+
$app->get('/hello/{name}', function ($name) use ($app) {
7+
return 'Hello '.$app->escape($name);
8+
}, array(
9+
'1',
10+
'2',
11+
'3',
12+
));
13+
14+
somefunction($foo, $bar, [
15+
// ...
16+
],
17+
$baz);
18+
19+
somefunction(
20+
$foo,
21+
$bar,
22+
[
23+
// ...
24+
],
25+
$baz
26+
);
27+
28+
29+
?>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Unit test class for the FunctionCallSignature sniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @author Marc McIntyre <[email protected]>
11+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
12+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13+
* @link http://pear.php.net/package/PHP_CodeSniffer
14+
*/
15+
16+
/**
17+
* Unit test class for the FunctionCallSignature sniff.
18+
*
19+
* A sniff unit test checks a .inc file for expected violations of a single
20+
* coding standard. Expected errors and warnings are stored in this class.
21+
*
22+
* @category PHP
23+
* @package PHP_CodeSniffer
24+
* @author Greg Sherwood <[email protected]>
25+
* @author Marc McIntyre <[email protected]>
26+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
27+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
28+
* @version Release: @package_version@
29+
* @link http://pear.php.net/package/PHP_CodeSniffer
30+
*/
31+
class PSR2_Tests_Methods_FunctionCallSignatureUnitTest extends AbstractSniffUnitTest
32+
{
33+
34+
35+
/**
36+
* Returns the lines where errors should occur.
37+
*
38+
* The key of the array should represent the line number and the value
39+
* should represent the number of errors that should occur on that line.
40+
*
41+
* @return array(int => int)
42+
*/
43+
public function getErrorList()
44+
{
45+
return array(
46+
14 => 3,
47+
17 => 2,
48+
);
49+
50+
}//end getErrorList()
51+
52+
53+
/**
54+
* Returns the lines where warnings should occur.
55+
*
56+
* The key of the array should represent the line number and the value
57+
* should represent the number of warnings that should occur on that line.
58+
*
59+
* @return array(int => int)
60+
*/
61+
public function getWarningList()
62+
{
63+
return array();
64+
65+
}//end getWarningList()
66+
67+
68+
}//end class
69+
70+
?>

CodeSniffer/Standards/PSR2/ruleset.xml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<rule ref="Squiz.Functions.FunctionDeclaration"/>
108108
<rule ref="Squiz.Functions.LowercaseFunctionKeywords"/>
109109

110-
<!-- 4.3 Method Arguments -->
110+
<!-- 4.4 Method Arguments -->
111111

112112
<!-- In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. -->
113113
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
@@ -125,23 +125,18 @@
125125
<!-- 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. When the argument list is split across multiple lines, the closing parenthesis and opening brace MUST be placed together on their own line with one space between them. -->
126126
<rule ref="Squiz.Functions.MultiLineFunctionDeclaration"/>
127127

128-
<!-- 4.4. abstract, final, and static -->
128+
<!-- 4.5 abstract, final, and static -->
129129

130130
<!-- When present, the abstract and final declarations MUST precede the visibility declaration.
131131
When present, the static declaration MUST come after the visibility declaration. -->
132132
<!-- checked in Methods/MethodDeclarationSniff -->
133133

134-
<!-- 4.5 Method and Function Calls -->
134+
<!-- 4.6 Method and Function Calls -->
135135

136136
<!-- When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis, there MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis. In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.
137137
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. -->
138138
<rule ref="Generic.Functions.FunctionCallArgumentSpacing"/>
139-
<rule ref="PEAR.Functions.FunctionCallSignature">
140-
<properties>
141-
<property name="allowMultipleArguments" value="false"/>
142-
</properties>
143-
</rule>
144-
<rule ref="PEAR.Functions.FunctionCallSignature.SpaceAfterCloseBracket">
139+
<rule ref="PSR2.Methods.FunctionCallSignature.SpaceAfterCloseBracket">
145140
<severity>0</severity>
146141
</rule>
147142

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5858
- Squiz LogicalOperatorSpacingSniff now ignores whitespace at the end of a line
5959
- Squiz.Scope.MethodScope.Missing error message now mentions 'visibility' instead of 'scope modifier'
6060
-- Thanks to Renat Akhmedyanov for the patch
61+
- Added support for the PSR2 multi-line arguments errata
6162
- The PSR2 standard no longer throws errors for additional spacing after a type hint
6263
- PSR UseDeclarationSniff no longer throws errors for USE statements inside TRAITs
6364
- Fixed bug #20026 : Check for multi-line arrays that should be single-line is slightly wrong

0 commit comments

Comments
 (0)