Skip to content

Commit ae89c53

Browse files
committed
Only check public accessors
But configurable
1 parent 6ba1e36 commit ae89c53

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Inpsyde/Sniffs/CodeQuality/NoAccessorsSniff.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Inpsyde\PhpcsHelpers;
1919
use PHP_CodeSniffer\Files\File;
2020
use PHP_CodeSniffer\Sniffs\Sniff;
21+
use PHP_CodeSniffer\Util\Tokens;
2122

2223
/**
2324
* @package php-coding-standards
@@ -34,6 +35,8 @@ final class NoAccessorsSniff implements Sniff
3435

3536
public $skipForFunctions = true;
3637

38+
public $skipForNonPublic = true;
39+
3740
/**
3841
* @return int[]
3942
*/
@@ -54,11 +57,28 @@ public function process(File $file, $position)
5457
}
5558

5659
$functionName = $file->getDeclarationName($position);
57-
5860
if (!$functionName || in_array($functionName, self::ALLOWED_NAMES, true)) {
5961
return;
6062
}
6163

64+
if ($this->skipForNonPublic && PhpcsHelpers::functionIsMethod($file, $position)) {
65+
$modifierPointerPosition = $file->findPrevious(
66+
[T_WHITESPACE, T_ABSTRACT],
67+
$position - 1,
68+
null,
69+
true,
70+
null,
71+
true
72+
);
73+
$modifierPointer = $file->getTokens()[$modifierPointerPosition] ?? null;
74+
if (!in_array($modifierPointer['code'], Tokens::$scopeModifiers, true)) {
75+
$modifierPointer = null;
76+
}
77+
if ($modifierPointer && $modifierPointer['code'] !== T_PUBLIC) {
78+
return;
79+
}
80+
}
81+
6282
preg_match('/^(set|get)[_A-Z0-9]+/', $functionName, $matches);
6383
if (!$matches) {
6484
return;

tests/fixtures/no-accessors.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ function withThing() {
4040

4141
}
4242

43-
// @phpcsWarningCodeOnNextLine NoSetter
44-
function setTheThing($foo, $bar) {
43+
private function setTheThing($foo, $bar) {
4544

4645
}
4746

0 commit comments

Comments
 (0)