Skip to content

Commit de4f1fd

Browse files
committed
ClassConstantVisibilitySniff: not fixable by default
1 parent 67503ce commit de4f1fd

File tree

5 files changed

+101
-14
lines changed

5 files changed

+101
-14
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ Sniff provides the following settings:
209209

210210
In PHP 7.1 it's possible to declare [visibility of class constants](https://wiki.php.net/rfc/class_const_visibility). In a similar vein to optional declaration of visibility for properties and methods which is actually required in sane coding standards, this sniff also requires declaring visibility for all class constants.
211211

212+
Sniff provides the following settings:
213+
214+
* `fixable`: the sniff is not fixable by default because we think it's better to decide about each constant one by one however you can enable fixability with this option
215+
212216
```php
213217
const FOO = 1; // visibility missing!
214218
public const BAR = 2; // correct

SlevomatCodingStandard/Sniffs/Classes/ClassConstantVisibilitySniff.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class ClassConstantVisibilitySniff implements \PHP_CodeSniffer\Sniffs\Sniff
1616
*/
1717
public $enabled = true;
1818

19+
/**
20+
* @var bool
21+
*/
22+
public $fixable = false;
23+
1924
/**
2025
* @return mixed[]
2126
*/
@@ -50,19 +55,21 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $constantPointer
5055

5156
$visibilityPointer = TokenHelper::findPreviousEffective($phpcsFile, $constantPointer - 1);
5257
if (!in_array($tokens[$visibilityPointer]['code'], [T_PUBLIC, T_PROTECTED, T_PRIVATE], true)) {
53-
$fix = $phpcsFile->addFixableError(
54-
sprintf(
55-
'Constant %s::%s visibility missing.',
56-
ClassHelper::getFullyQualifiedName($phpcsFile, $classPointer),
57-
$tokens[TokenHelper::findNextEffective($phpcsFile, $constantPointer + 1)]['content']
58-
),
59-
$constantPointer,
60-
self::CODE_MISSING_CONSTANT_VISIBILITY
58+
$message = sprintf(
59+
'Constant %s::%s visibility missing.',
60+
ClassHelper::getFullyQualifiedName($phpcsFile, $classPointer),
61+
$tokens[TokenHelper::findNextEffective($phpcsFile, $constantPointer + 1)]['content']
6162
);
62-
if ($fix) {
63-
$phpcsFile->fixer->beginChangeset();
64-
$phpcsFile->fixer->addContentBefore($constantPointer, 'public ');
65-
$phpcsFile->fixer->endChangeset();
63+
64+
if ($this->fixable) {
65+
$fix = $phpcsFile->addFixableError($message, $constantPointer, self::CODE_MISSING_CONSTANT_VISIBILITY);
66+
if ($fix) {
67+
$phpcsFile->fixer->beginChangeset();
68+
$phpcsFile->fixer->addContentBefore($constantPointer, 'public ');
69+
$phpcsFile->fixer->endChangeset();
70+
}
71+
} else {
72+
$phpcsFile->addError($message, $constantPointer, self::CODE_MISSING_CONSTANT_VISIBILITY);
6673
}
6774
}
6875
}

tests/Sniffs/Classes/ClassConstantVisibilitySniffTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,21 @@ public function testDisabledSniff(): void
4949
$this->assertNoSniffErrorInFile($report);
5050
}
5151

52-
public function testFixable(): void
52+
public function testFixableEnabled(): void
5353
{
5454
$report = $this->checkFile(
5555
__DIR__ . '/data/fixableMissingClassConstantVisibility.php',
56-
[],
56+
['fixable' => true],
57+
[ClassConstantVisibilitySniff::CODE_MISSING_CONSTANT_VISIBILITY]
58+
);
59+
$this->assertAllFixedInFile($report);
60+
}
61+
62+
public function testFixableDisabled(): void
63+
{
64+
$report = $this->checkFile(
65+
__DIR__ . '/data/fixableMissingClassConstantVisibilityFixableDisabled.php',
66+
['fixable' => false],
5767
[ClassConstantVisibilitySniff::CODE_MISSING_CONSTANT_VISIBILITY]
5868
);
5969
$this->assertAllFixedInFile($report);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
const NON_CLASS = 1;
4+
5+
interface A
6+
{
7+
8+
const HELLO = 2;
9+
10+
}
11+
12+
class B
13+
{
14+
15+
const WORLD = 3;
16+
17+
}
18+
19+
class C
20+
{
21+
22+
const A = 1;
23+
const B = 2;
24+
const C = 3;
25+
26+
}
27+
28+
class D
29+
{
30+
31+
const A = 1, B = 2, C = 3;
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
const NON_CLASS = 1;
4+
5+
interface A
6+
{
7+
8+
const HELLO = 2;
9+
10+
}
11+
12+
class B
13+
{
14+
15+
const WORLD = 3;
16+
17+
}
18+
19+
class C
20+
{
21+
22+
const A = 1;
23+
const B = 2;
24+
const C = 3;
25+
26+
}
27+
28+
class D
29+
{
30+
31+
const A = 1, B = 2, C = 3;
32+
33+
}

0 commit comments

Comments
 (0)