Skip to content

Commit 8a89dfa

Browse files
committed
2 parents 7c01187 + b627255 commit 8a89dfa

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
class FunctionCommentSniff implements Sniff
1717
{
1818

19+
/**
20+
* Disable the check for functions with a lower visibility than the value given.
21+
* Allowed values are public, protected and private.
22+
*
23+
* @var string
24+
*/
25+
public $minimumVisibility = 'private';
26+
1927

2028
/**
2129
* Returns an array of tokens this test wants to listen for.
@@ -41,16 +49,29 @@ public function register()
4149
public function process(File $phpcsFile, $stackPtr)
4250
{
4351
$tokens = $phpcsFile->getTokens();
44-
$find = Tokens::$methodPrefixes;
45-
$find[] = T_WHITESPACE;
4652

47-
$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
53+
$ignore = Tokens::$emptyTokens;
54+
$ignore[] = T_STATIC;
55+
56+
$scopeModifier = $phpcsFile->getMethodProperties($stackPtr)['scope'];
57+
if ($scopeModifier === 'protected'
58+
&& $this->minimumVisibility === 'public'
59+
|| $scopeModifier === 'private'
60+
&& ($this->minimumVisibility === 'public' || $this->minimumVisibility === 'protected')
61+
) {
62+
return;
63+
}
64+
65+
$ignore = Tokens::$methodPrefixes;
66+
$ignore[] = T_WHITESPACE;
67+
68+
$commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
4869
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
4970
// Inline comments might just be closing comments for
5071
// control structures or functions instead of function comments
5172
// using the wrong comment type. If there is other code on the line,
5273
// assume they relate to that code.
53-
$prev = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true);
74+
$prev = $phpcsFile->findPrevious($ignore, ($commentEnd - 1), null, true);
5475
if ($prev !== false && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) {
5576
$commentEnd = $prev;
5677
}

src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,20 @@ public function setTranslator($a, &$b): void
381381
{
382382
$this->translator = $translator;
383383
}
384+
385+
// phpcs:set PEAR.Commenting.FunctionComment minimumVisibility protected
386+
private function setTranslator2($a, &$b): void
387+
{
388+
$this->translator = $translator;
389+
}
390+
391+
// phpcs:set PEAR.Commenting.FunctionComment minimumVisibility public
392+
protected function setTranslator3($a, &$b): void
393+
{
394+
$this->translator = $translator;
395+
}
396+
397+
private function setTranslator4($a, &$b): void
398+
{
399+
$this->translator = $translator;
400+
}

src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,20 @@ public function setTranslator($a, &$b): void
381381
{
382382
$this->translator = $translator;
383383
}
384+
385+
// phpcs:set PEAR.Commenting.FunctionComment minimumVisibility protected
386+
private function setTranslator2($a, &$b): void
387+
{
388+
$this->translator = $translator;
389+
}
390+
391+
// phpcs:set PEAR.Commenting.FunctionComment minimumVisibility public
392+
protected function setTranslator3($a, &$b): void
393+
{
394+
$this->translator = $translator;
395+
}
396+
397+
private function setTranslator4($a, &$b): void
398+
{
399+
$this->translator = $translator;
400+
}

0 commit comments

Comments
 (0)