Skip to content

Commit 5d15a5c

Browse files
author
Vincent Langlet
committed
🐛 Still check phpdoc for test functions if there is already one
1 parent 6eb2c97 commit 5d15a5c

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

Symfony3Custom/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,34 @@ class Symfony3Custom_Sniffs_Commenting_FunctionCommentSniff extends PEAR_Sniffs_
2828
*/
2929
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
3030
{
31-
if (false === $commentEnd = $phpcsFile->findPrevious(
32-
array(
33-
T_COMMENT,
34-
T_DOC_COMMENT,
35-
T_CLASS,
36-
T_FUNCTION,
37-
T_OPEN_TAG,
38-
),
39-
($stackPtr - 1)
40-
)
41-
) {
42-
return;
43-
}
44-
4531
$tokens = $phpcsFile->getTokens();
46-
$code = $tokens[$commentEnd]['code'];
32+
$find = PHP_CodeSniffer_Tokens::$methodPrefixes;
33+
$find[] = T_WHITESPACE;
34+
35+
$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
36+
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
37+
// Inline comments might just be closing comments for
38+
// control structures or functions instead of function comments
39+
// using the wrong comment type. If there is other code on the line,
40+
// assume they relate to that code.
41+
$prev = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true);
42+
if ($prev !== false && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) {
43+
$commentEnd = $prev;
44+
}
45+
}
4746

48-
$name = $phpcsFile->getDeclarationName($stackPtr);
47+
if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
48+
&& $tokens[$commentEnd]['code'] !== T_COMMENT
49+
) {
50+
$name = $phpcsFile->getDeclarationName($stackPtr);
4951

50-
$commentRequired = strpos($name, 'test') !== 0 && $name !== 'setUp' && $name !== 'tearDown';
52+
$commentRequired = strpos($name, 'test') !== 0
53+
&& $name !== 'setUp'
54+
&& $name !== 'tearDown';
5155

52-
if (($code === T_COMMENT && !$commentRequired)
53-
|| ($code !== T_DOC_COMMENT && !$commentRequired)
54-
) {
55-
return;
56+
if (! $commentRequired) {
57+
return;
58+
}
5659
}
5760

5861
parent::process($phpcsFile, $stackPtr);

Symfony3Custom/Tests/Commenting/FunctionCommentUnitTest.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,16 @@ function functionTestInherit($test)
7272
{
7373
return 42;
7474
}
75+
76+
function testToIgnore($test)
77+
{
78+
return 42;
79+
}
80+
81+
/**
82+
* @param string $test should not be ignore
83+
*/
84+
function testToNotIgnore($test)
85+
{
86+
return 42;
87+
}

Symfony3Custom/Tests/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,16 @@ function functionTestInherit($test)
7272
{
7373
return 42;
7474
}
75+
76+
function testToIgnore($test)
77+
{
78+
return 42;
79+
}
80+
81+
/**
82+
* @param string $test should not be ignore
83+
*/
84+
function testToNotIgnore($test)
85+
{
86+
return 42;
87+
}

Symfony3Custom/Tests/Commenting/FunctionCommentUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function getErrorList()
2525
43 => 1,
2626
48 => 2,
2727
57 => 1,
28+
83 => 1,
2829
);
2930
}
3031

0 commit comments

Comments
 (0)