Skip to content

Commit 5204706

Browse files
committed
SlevomatCodingStandard.Functions.DisallowEmptyFunction: Private or protected constructor should not be reported
1 parent 4ffc43a commit 5204706

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

SlevomatCodingStandard/Sniffs/Functions/DisallowEmptyFunctionSniff.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
use PHP_CodeSniffer\Util\Tokens;
88
use SlevomatCodingStandard\Helpers\FunctionHelper;
99
use SlevomatCodingStandard\Helpers\TokenHelper;
10+
use function in_array;
11+
use const T_CLOSE_CURLY_BRACKET;
1012
use const T_FUNCTION;
13+
use const T_OPEN_CURLY_BRACKET;
14+
use const T_PRIVATE;
15+
use const T_PROTECTED;
16+
use const T_SEMICOLON;
1117
use const T_WHITESPACE;
1218

1319
class DisallowEmptyFunctionSniff implements Sniff
@@ -36,6 +42,16 @@ public function process(File $phpcsFile, $functionPointer): void
3642
}
3743

3844
if (FunctionHelper::getName($phpcsFile, $functionPointer) === '__construct') {
45+
$previousPointer = TokenHelper::findPrevious(
46+
$phpcsFile,
47+
[T_PRIVATE, T_SEMICOLON, T_CLOSE_CURLY_BRACKET, T_OPEN_CURLY_BRACKET],
48+
$functionPointer - 1,
49+
);
50+
51+
if ($previousPointer !== null && in_array($tokens[$previousPointer]['code'], [T_PRIVATE, T_PROTECTED], true)) {
52+
return;
53+
}
54+
3955
$propertyPromotion = TokenHelper::findNext(
4056
$phpcsFile,
4157
Tokens::$scopeModifiers,

tests/Sniffs/Functions/data/disallowEmptyFunctionNoErrors.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,12 @@ public function __construct(private $a, public $b)
5454
}
5555

5656
}
57+
58+
class Foo
59+
{
60+
61+
private function __construct()
62+
{
63+
}
64+
65+
}

0 commit comments

Comments
 (0)