Skip to content

Commit cd84aa9

Browse files
committed
Now enforcing that function open brace is the last content on the line
1 parent 731a887 commit cd84aa9

File tree

6 files changed

+206
-4
lines changed

6 files changed

+206
-4
lines changed

CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6363

6464
$openingBrace = $tokens[$stackPtr]['scope_opener'];
6565

66+
$next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true);
67+
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
68+
$error = 'Opening brace must be the last content on the line';
69+
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
70+
if ($fix === true) {
71+
$phpcsFile->fixer->addNewline($openingBrace);
72+
}
73+
}
74+
6675
// The end of the function occurs at the end of the argument list. Its
6776
// like this because some people like to break long function declarations
6877
// over multiple lines.
@@ -112,15 +121,15 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
112121
// or an if with an else before it, then we need to start the scope
113122
// checking from there, rather than the current token.
114123
$lineStart = $stackPtr;
115-
while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) {
124+
while (($lineStart = $phpcsFile->findPrevious(T_WHITESPACE, ($lineStart - 1), null, false)) !== false) {
116125
if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
117126
break;
118127
}
119128
}
120129

121-
// We found a new line, now go forward and find the first non-whitespace
122-
// token.
123-
$lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true);
130+
// We found a new line, now go forward and find the first
131+
// non-whitespace token.
132+
$lineStart = $phpcsFile->findNext(T_WHITESPACE, $lineStart, null, true);
124133

125134
// The opening brace is on the correct line, now it needs to be
126135
// checked to be correctly indented.

CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6363

6464
$openingBrace = $tokens[$stackPtr]['scope_opener'];
6565

66+
$next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true);
67+
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
68+
$error = 'Opening brace must be the last content on the line';
69+
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
70+
if ($fix === true) {
71+
$phpcsFile->fixer->addNewline($openingBrace);
72+
}
73+
}
74+
6675
// The end of the function occurs at the end of the argument list. Its
6776
// like this because some people like to break long function declarations
6877
// over multiple lines.

CodeSniffer/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,9 @@ namespace {
157157
use function Name\Space\f;
158158
f();
159159
}
160+
161+
$var = function() {return true;};
162+
$var = function() {return true;
163+
};
164+
function blah(){return true;
165+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
public function someFunctionWithAVeryLongName($firstParameter='something',
3+
$secondParameter='booooo', $third=null, $fourthParameter=false,
4+
$fifthParameter=123.12, $sixthParam=true
5+
) {
6+
}
7+
8+
function someFunctionWithAVeryLongName2($firstParameter='something',
9+
$secondParameter='booooo', $third=null, $fourthParameter=false,
10+
$fifthParameter=123.12, $sixthParam=true
11+
) {
12+
}
13+
14+
function blah()
15+
{
16+
}
17+
18+
function blah()
19+
{
20+
}
21+
22+
class MyClass
23+
{
24+
25+
public function someFunctionWithAVeryLongName($firstParameter='something',
26+
$secondParameter='booooo', $third=null, $fourthParameter=false,
27+
$fifthParameter=123.12, $sixthParam=true
28+
) /** w00t */ {
29+
}
30+
31+
public function someFunctionWithAVeryLongName2(
32+
$firstParameter='something', $secondParameter='booooo', $third=null
33+
) {
34+
}
35+
36+
protected abstract function processTokenWithinScope(
37+
PHP_CodeSniffer_File $phpcsFile,
38+
$stackPtr,
39+
$currScope
40+
);
41+
42+
protected abstract function processToken(
43+
PHP_CodeSniffer_File $phpcsFile,
44+
$stackPtr,
45+
$currScope
46+
);
47+
48+
}
49+
50+
function getInstalledStandards(
51+
$includeGeneric=false,
52+
$standardsDir=''
53+
) {
54+
}
55+
56+
function &testFunction($arg1,
57+
$arg2,
58+
) {
59+
}
60+
61+
function testFunction($arg1,
62+
$arg2
63+
) {
64+
}
65+
66+
function validateUrl(
67+
$url,
68+
$requireScheme=TRUE,
69+
array $allowedSchemes=array(
70+
'http',
71+
'https',
72+
),
73+
array $notAllowedSchemes=array('ftp', 'sftp')
74+
) {
75+
}
76+
77+
function validateUrlShort(
78+
$url,
79+
$requireScheme=TRUE,
80+
array $allowedSchemes=[
81+
'http',
82+
'https',
83+
],
84+
array $notAllowedSchemes=['ftp', 'sftp']
85+
) {
86+
}
87+
88+
$noArgs_longVars = function () use (
89+
$longVar1,
90+
$longerVar2,
91+
$muchLongerVar3
92+
) {
93+
// body
94+
};
95+
96+
$longArgs_longVars = function (
97+
$longArgument,
98+
$longerArgument,
99+
$muchLongerArgument
100+
) use (
101+
$longVar1,
102+
$longerVar2,
103+
$muchLongerVar3
104+
) {
105+
// body
106+
};
107+
108+
$longArgs_longVars = function (
109+
$longArgument,
110+
$longerArgument,
111+
$muchLongerArgument
112+
) use (
113+
$longVar1,
114+
$longerVar2,
115+
$muchLongerVar3
116+
) {
117+
// body
118+
};
119+
120+
$longArgs_longVars = function (
121+
$longArgument,
122+
$muchLongerArgument
123+
) use (
124+
$muchLongerVar3
125+
) {
126+
// body
127+
};
128+
129+
function test()
130+
{
131+
$longArgs_longVars = function (
132+
$longArgument,
133+
$longerArgument,
134+
$muchLongerArgument
135+
) use (
136+
$longVar1,
137+
$longerVar2,
138+
$muchLongerVar3
139+
) {
140+
// body
141+
};
142+
}
143+
144+
function myFunction()
145+
{
146+
}
147+
148+
function myFunction()
149+
{
150+
}
151+
152+
153+
use function foo\bar;
154+
155+
use
156+
function bar\baz;
157+
158+
namespace {
159+
use function Name\Space\f;
160+
f();
161+
}
162+
163+
$var = function() {
164+
return true;};
165+
$var = function() {
166+
return true;
167+
};
168+
function blah()
169+
{
170+
return true;
171+
}

CodeSniffer/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function getErrorList()
6161
121 => 1,
6262
140 => 1,
6363
145 => 1,
64+
161 => 1,
65+
162 => 1,
66+
164 => 2,
6467
);
6568

6669
}//end getErrorList()

package.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
3232
-- Possible alternative for code using PHP_CodeSniffer_File::findPrevious() with the local flag set
3333
- Added PHP_CodeSniffer_File::findEndOfStatement() to find the last non-whitespace token in a statement
3434
-- Possible alternative for code using PHP_CodeSniffer_File::findNext() with the local flag set
35+
- Generic opening function brace sniff now ensure the opening brace is the last content on the line
36+
-- Affects OpeningFunctionBraceBsdAllmanSniff and OpeningFunctionBraceKernighanRitchieSniff
37+
-- Also enforced in PEAR FunctionDeclarationSniff and Squiz MultiLineFunctionDeclarationSniff
3538
- Generic DisallowTabIndentSniff now replaces tabs everywhere it finds them, except in strings and here/now docs
3639
- Generic EmptyStatementSniff error codes now contain the type of empty statement detected (request #314)
3740
-- All messages generated by this sniff are now errors (empty CATCH was previously a warning)
@@ -1069,6 +1072,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
10691072
<tasks:replace from="@package_version@" to="version" type="package-info" />
10701073
</file>
10711074
<file baseinstalldir="PHP" name="FunctionDeclarationUnitTest.inc" role="test" />
1075+
<file baseinstalldir="PHP" name="FunctionDeclarationUnitTest.inc.fixed" role="test" />
10721076
<file baseinstalldir="PHP" name="FunctionDeclarationUnitTest.php" role="test">
10731077
<tasks:replace from="@package_version@" to="version" type="package-info" />
10741078
</file>

0 commit comments

Comments
 (0)