Skip to content

Commit 81e880f

Browse files
author
Vincent Langlet
committed
✨ Process whitespace for function with no docblock
1 parent 75a8868 commit 81e880f

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

Symfony3Custom/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,15 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
9999
// These checks need function comment
100100
$this->processParams($phpcsFile, $stackPtr, $commentStart);
101101
$this->processThrows($phpcsFile, $stackPtr, $commentStart);
102-
} elseif (count($realParams) > 0) {
103-
foreach ($realParams as $neededParam) {
104-
$error = 'Doc comment for parameter "%s" missing';
105-
$data = array($neededParam['name']);
106-
$phpcsFile->addError($error, $stackPtr, 'MissingParamTag', $data);
102+
} else {
103+
$this->processWhitespace($phpcsFile, $stackPtr, $stackPtr);
104+
105+
if (count($realParams) > 0) {
106+
foreach ($realParams as $neededParam) {
107+
$error = 'Doc comment for parameter "%s" missing';
108+
$data = array($neededParam['name']);
109+
$phpcsFile->addError($error, $stackPtr, 'MissingParamTag', $data);
110+
}
107111
}
108112
}
109113
}
@@ -193,9 +197,17 @@ protected function processWhitespace(
193197
$found = 0;
194198
}
195199

196-
$error = 'Expected 1 blank line before docblock; %s found';
200+
if ($stackPtr === $commentStart) {
201+
// There is no docblock
202+
$error = 'Expected 1 blank line before function; %s found';
203+
$rule = 'SpacingBeforeFunction';
204+
} else {
205+
$error = 'Expected 1 blank line before docblock; %s found';
206+
$rule = 'SpacingBeforeDocblock';
207+
}
208+
197209
$data = array($found);
198-
$fix = $phpcsFile->addFixableError($error, $commentStart, 'SpacingBeforeDocblock', $data);
210+
$fix = $phpcsFile->addFixableError($error, $commentStart, $rule, $data);
199211

200212
if ($fix === true) {
201213
if ($found > 1) {

Symfony3Custom/Tests/Commenting/FunctionCommentUnitTest.inc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ function testToNotIgnore2($test)
8686
return 42;
8787
}
8888

89-
function testToIgnore()
89+
function testToIgnore1()
9090
{
91-
$test = 2;
91+
$test = 42;
92+
}
93+
function testToIgnore2()
94+
{
95+
$test = 42;
9296
}

Symfony3Custom/Tests/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ function testToNotIgnore2($test)
8686
return 42;
8787
}
8888

89-
function testToIgnore()
89+
function testToIgnore1()
9090
{
91-
$test = 2;
91+
$test = 42;
92+
}
93+
94+
function testToIgnore2()
95+
{
96+
$test = 42;
9297
}

Symfony3Custom/Tests/Commenting/FunctionCommentUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function getErrorList()
2727
57 => 1,
2828
76 => 2,
2929
83 => 1,
30+
93 => 1,
3031
);
3132
}
3233

0 commit comments

Comments
 (0)