Skip to content

Commit 490023f

Browse files
committed
Revert "Enable PHP 7.4+ sniffs always"
This reverts commit 61e439e.
1 parent b58063e commit 490023f

15 files changed

+235
-20
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/RequireNullCoalesceEqualOperatorSniff.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHP_CodeSniffer\Util\Tokens;
88
use SlevomatCodingStandard\Helpers\FixerHelper;
99
use SlevomatCodingStandard\Helpers\IdentificatorHelper;
10+
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
1011
use SlevomatCodingStandard\Helpers\TokenHelper;
1112
use const T_COALESCE;
1213
use const T_EQUAL;
@@ -17,8 +18,7 @@ class RequireNullCoalesceEqualOperatorSniff implements Sniff
1718

1819
public const CODE_REQUIRED_NULL_COALESCE_EQUAL_OPERATOR = 'RequiredNullCoalesceEqualOperator';
1920

20-
/** @deprecated */
21-
public bool $enable = true;
21+
public ?bool $enable = null;
2222

2323
/**
2424
* @return array<int, (int|string)>
@@ -36,6 +36,12 @@ public function register(): array
3636
*/
3737
public function process(File $phpcsFile, $equalPointer): void
3838
{
39+
$this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 70400);
40+
41+
if (!$this->enable) {
42+
return;
43+
}
44+
3945
/** @var int $variableStartPointer */
4046
$variableStartPointer = TokenHelper::findNextEffective($phpcsFile, $equalPointer + 1);
4147
$variableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $variableStartPointer);

SlevomatCodingStandard/Sniffs/Functions/RequireArrowFunctionSniff.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHP_CodeSniffer\Sniffs\Sniff;
77
use SlevomatCodingStandard\Helpers\FixerHelper;
88
use SlevomatCodingStandard\Helpers\ScopeHelper;
9+
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
910
use SlevomatCodingStandard\Helpers\TokenHelper;
1011
use function count;
1112
use const T_BITWISE_AND;
@@ -24,8 +25,7 @@ class RequireArrowFunctionSniff implements Sniff
2425

2526
public bool $allowNested = true;
2627

27-
/** @deprecated */
28-
public bool $enable = true;
28+
public ?bool $enable = null;
2929

3030
/**
3131
* @return array<int, (int|string)>
@@ -43,6 +43,12 @@ public function register(): array
4343
*/
4444
public function process(File $phpcsFile, $closurePointer): void
4545
{
46+
$this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 70400);
47+
48+
if (!$this->enable) {
49+
return;
50+
}
51+
4652
$tokens = $phpcsFile->getTokens();
4753

4854
$returnPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$closurePointer]['scope_opener'] + 1);

SlevomatCodingStandard/Sniffs/Numbers/RequireNumericLiteralSeparatorSniff.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class RequireNumericLiteralSeparatorSniff implements Sniff
1515

1616
public const CODE_REQUIRED_NUMERIC_LITERAL_SEPARATOR = 'RequiredNumericLiteralSeparator';
1717

18-
/** @deprecated */
19-
public bool $enable = true;
18+
public ?bool $enable = null;
2019

2120
public int $minDigitsBeforeDecimalPoint = 4;
2221

@@ -41,9 +40,14 @@ public function register(): array
4140
*/
4241
public function process(File $phpcsFile, $numberPointer): void
4342
{
43+
$this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 70400);
4444
$this->minDigitsBeforeDecimalPoint = SniffSettingsHelper::normalizeInteger($this->minDigitsBeforeDecimalPoint);
4545
$this->minDigitsAfterDecimalPoint = SniffSettingsHelper::normalizeInteger($this->minDigitsAfterDecimalPoint);
4646

47+
if (!$this->enable) {
48+
return;
49+
}
50+
4751
$tokens = $phpcsFile->getTokens();
4852
$number = $tokens[$numberPointer]['content'];
4953

SlevomatCodingStandard/Sniffs/TypeHints/PropertyTypeHintSniff.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class PropertyTypeHintSniff implements Sniff
6868

6969
private const NAME = 'SlevomatCodingStandard.TypeHints.PropertyTypeHint';
7070

71-
/** @deprecated */
72-
public bool $enableNativeTypeHint = true;
71+
public ?bool $enableNativeTypeHint = null;
7372

7473
public ?bool $enableMixedTypeHint = null;
7574

@@ -105,13 +104,18 @@ public function register(): array
105104
*/
106105
public function process(File $phpcsFile, $pointer): void
107106
{
108-
$this->enableMixedTypeHint = SniffSettingsHelper::isEnabledByPhpVersion($this->enableMixedTypeHint, 80000);
109-
$this->enableUnionTypeHint = SniffSettingsHelper::isEnabledByPhpVersion($this->enableUnionTypeHint, 80000);
110-
$this->enableIntersectionTypeHint = SniffSettingsHelper::isEnabledByPhpVersion($this->enableIntersectionTypeHint, 80100);
111-
$this->enableStandaloneNullTrueFalseTypeHints = SniffSettingsHelper::isEnabledByPhpVersion(
112-
$this->enableStandaloneNullTrueFalseTypeHints,
113-
80200,
114-
);
107+
$this->enableMixedTypeHint = $this->enableNativeTypeHint
108+
? SniffSettingsHelper::isEnabledByPhpVersion($this->enableMixedTypeHint, 80000)
109+
: false;
110+
$this->enableUnionTypeHint = $this->enableNativeTypeHint
111+
? SniffSettingsHelper::isEnabledByPhpVersion($this->enableUnionTypeHint, 80000)
112+
: false;
113+
$this->enableIntersectionTypeHint = $this->enableNativeTypeHint
114+
? SniffSettingsHelper::isEnabledByPhpVersion($this->enableIntersectionTypeHint, 80100)
115+
: false;
116+
$this->enableStandaloneNullTrueFalseTypeHints = $this->enableNativeTypeHint
117+
? SniffSettingsHelper::isEnabledByPhpVersion($this->enableStandaloneNullTrueFalseTypeHints, 80200)
118+
: false;
115119

116120
$tokens = $phpcsFile->getTokens();
117121

@@ -201,7 +205,9 @@ private function checkTypeHint(
201205
if (!$isSuppressedAnyTypeHint) {
202206
$phpcsFile->addError(
203207
sprintf(
204-
'Property %s does not have native type hint nor @var annotation for its value.',
208+
$this->enableNativeTypeHint
209+
? 'Property %s does not have native type hint nor @var annotation for its value.'
210+
: 'Property %s does not have @var annotation for its value.',
205211
PropertyHelper::getFullyQualifiedName($phpcsFile, $propertyPointer),
206212
),
207213
$propertyPointer,
@@ -212,6 +218,10 @@ private function checkTypeHint(
212218
return;
213219
}
214220

221+
if (!$this->enableNativeTypeHint) {
222+
return;
223+
}
224+
215225
$typeNode = $propertyAnnotation->getValue()->type;
216226
$originalTypeNode = $typeNode;
217227
if ($typeNode instanceof NullableTypeNode) {

doc/control-structures.md

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

192192
Requires use of null coalesce equal operator when possible.
193193

194+
This sniff provides the following setting:
195+
196+
* `enable`: either to enable or not this sniff. By default, it is enabled for PHP versions 7.4 or higher.
197+
194198
#### SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator 🔧
195199

196200
Requires use of null coalesce operator when possible.

doc/functions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Requires arrow functions.
3434
Sniff provides the following settings:
3535

3636
* `allowNested` (default: `true`)
37+
* `enable`: either to enable or not this sniff. By default, it is enabled for PHP versions 7.4 or higher.
3738

3839
#### SlevomatCodingStandard.Functions.RequireMultiLineCall 🔧
3940

doc/numbers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Requires use of numeric literal separators.
1010

1111
This sniff provides the following setting:
1212

13+
* `enable`: either to enable or not this sniff. By default, it is enabled for PHP versions 7.4 or higher.
1314
* `minDigitsBeforeDecimalPoint`: the minimum digits before decimal point to require separator.
1415
* `minDigitsAfterDecimalPoint`: the minimum digits after decimal point to require separator.
1516
* `ignoreOctalNumbers`: to ignore octal numbers.

doc/type-hints.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ This sniff can cause an error if you're overriding or implementing a parent meth
9595

9696
Sniff provides the following settings:
9797

98+
* `enableNativeTypeHint`: enforces to transform `@var int` into native `int` typehint. It's on by default if you're on PHP 7.4+
9899
* `enableMixedTypeHint`: enforces to transform `@var mixed` into native `mixed` typehint. It's on by default if you're on PHP 8.0+. It can be enabled only when `enableNativeTypeHint` is enabled too.
99100
* `enableUnionTypeHint`: enforces to transform `@var string|int` into native `string|int` typehint. It's on by default if you're on PHP 8.0+. It can be enabled only when `enableNativeTypeHint` is enabled too.
100101
* `enableIntersectionTypeHint`: enforces to transform `@var Foo&Bar` into native `Foo&Bar` typehint. It's on by default if you're on PHP 8.1+. It can be enabled only when `enableNativeTypeHint` is enabled too.

tests/Sniffs/ControlStructures/RequireNullCoalesceEqualOperatorSniffTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ class RequireNullCoalesceEqualOperatorSniffTest extends TestCase
99

1010
public function testNoErrors(): void
1111
{
12-
$report = self::checkFile(__DIR__ . '/data/requireNullCoalesceEqualOperatorNoErrors.php');
12+
$report = self::checkFile(__DIR__ . '/data/requireNullCoalesceEqualOperatorNoErrors.php', [
13+
'enable' => true,
14+
]);
1315
self::assertNoSniffErrorInFile($report);
1416
}
1517

1618
public function testErrors(): void
1719
{
1820
$report = self::checkFile(
1921
__DIR__ . '/data/requireNullCoalesceEqualOperatorErrors.php',
20-
[],
22+
['enable' => true],
2123
[RequireNullCoalesceEqualOperatorSniff::CODE_REQUIRED_NULL_COALESCE_EQUAL_OPERATOR],
2224
);
2325

@@ -30,4 +32,15 @@ public function testErrors(): void
3032
self::assertAllFixedInFile($report);
3133
}
3234

35+
public function testShouldNotReportIfSniffIsDisabled(): void
36+
{
37+
$report = self::checkFile(
38+
__DIR__ . '/data/requireNullCoalesceEqualOperatorErrors.php',
39+
['enable' => false],
40+
[RequireNullCoalesceEqualOperatorSniff::CODE_REQUIRED_NULL_COALESCE_EQUAL_OPERATOR],
41+
);
42+
43+
self::assertNoSniffErrorInFile($report);
44+
}
45+
3346
}

tests/Sniffs/Functions/RequireArrowFunctionSniffTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function testDisallowNestedNoErrors(): void
1111
{
1212
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionDisallowNestedNoErrors.php', [
1313
'allowNested' => false,
14+
'enable' => true,
1415
]);
1516
self::assertNoSniffErrorInFile($report);
1617
}
@@ -19,6 +20,7 @@ public function testDisallowNestedErrors(): void
1920
{
2021
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionDisallowNestedErrors.php', [
2122
'allowNested' => false,
23+
'enable' => true,
2224
]);
2325

2426
self::assertSame(4, $report->getErrorCount());
@@ -35,6 +37,7 @@ public function testAllowNestedNoErrors(): void
3537
{
3638
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionAllowNestedNoErrors.php', [
3739
'allowNested' => true,
40+
'enable' => true,
3841
]);
3942
self::assertNoSniffErrorInFile($report);
4043
}
@@ -43,6 +46,7 @@ public function testAllowNestedErrors(): void
4346
{
4447
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionAllowNestedErrors.php', [
4548
'allowNested' => true,
49+
'enable' => true,
4650
]);
4751

4852
self::assertSame(3, $report->getErrorCount());
@@ -54,4 +58,14 @@ public function testAllowNestedErrors(): void
5458
self::assertAllFixedInFile($report);
5559
}
5660

61+
public function testShouldNotReportIfSniffIsDisabled(): void
62+
{
63+
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionAllowNestedErrors.php', [
64+
'allowNested' => true,
65+
'enable' => false,
66+
]);
67+
68+
self::assertNoSniffErrorInFile($report);
69+
}
70+
5771
}

tests/Sniffs/Numbers/RequireNumericLiteralSeparatorSniffTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ class RequireNumericLiteralSeparatorSniffTest extends TestCase
99

1010
public function testNoErrors(): void
1111
{
12-
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorNoErrors.php');
12+
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorNoErrors.php', [
13+
'enable' => true,
14+
]);
1315
self::assertNoSniffErrorInFile($report);
1416
}
1517

1618
public function testErrors(): void
1719
{
18-
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorErrors.php');
20+
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorErrors.php', [
21+
'enable' => true,
22+
]);
1923

2024
self::assertSame(4, $report->getErrorCount());
2125

@@ -28,6 +32,7 @@ public function testErrors(): void
2832
public function testModifiedSettingsNoErrors(): void
2933
{
3034
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorModifiedSettingsNoErrors.php', [
35+
'enable' => true,
3136
'minDigitsBeforeDecimalPoint' => 7,
3237
'minDigitsAfterDecimalPoint' => 6,
3338
'ignoreOctalNumbers' => false,
@@ -38,6 +43,7 @@ public function testModifiedSettingsNoErrors(): void
3843
public function testModifiedSettingsErrors(): void
3944
{
4045
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorModifiedSettingsErrors.php', [
46+
'enable' => true,
4147
'minDigitsBeforeDecimalPoint' => 7,
4248
'minDigitsAfterDecimalPoint' => 6,
4349
'ignoreOctalNumbers' => false,
@@ -50,4 +56,13 @@ public function testModifiedSettingsErrors(): void
5056
self::assertSniffError($report, 5, RequireNumericLiteralSeparatorSniff::CODE_REQUIRED_NUMERIC_LITERAL_SEPARATOR);
5157
}
5258

59+
public function testShouldNotReportIfSniffIsDisabled(): void
60+
{
61+
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorErrors.php', [
62+
'enable' => false,
63+
]);
64+
65+
self::assertNoSniffErrorInFile($report);
66+
}
67+
5368
}

tests/Sniffs/TypeHints/PropertyTypeHintSniffTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,36 @@
77
class PropertyTypeHintSniffTest extends TestCase
88
{
99

10+
public function testDisabledNativeNoErrors(): void
11+
{
12+
$report = self::checkFile(__DIR__ . '/data/propertyTypeHintDisabledNativeNoErrors.php', [
13+
'enableNativeTypeHint' => false,
14+
]);
15+
self::assertNoSniffErrorInFile($report);
16+
}
17+
18+
public function testDisabledNativeErrors(): void
19+
{
20+
$report = self::checkFile(__DIR__ . '/data/propertyTypeHintDisabledNativeErrors.php', [
21+
'enableNativeTypeHint' => false,
22+
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
23+
]);
24+
25+
self::assertSame(5, $report->getErrorCount());
26+
27+
self::assertSniffError($report, 9, PropertyTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
28+
self::assertSniffError($report, 11, PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
29+
self::assertSniffError($report, 14, PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
30+
self::assertSniffError($report, 17, PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
31+
self::assertSniffError($report, 23, PropertyTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
32+
33+
self::assertAllFixedInFile($report);
34+
}
35+
1036
public function testEnabledNativeNoErrors(): void
1137
{
1238
$report = self::checkFile(__DIR__ . '/data/propertyTypeHintEnabledNativeNoErrors.php', [
39+
'enableNativeTypeHint' => true,
1340
'enableMixedTypeHint' => true,
1441
'enableUnionTypeHint' => false,
1542
'enableIntersectionTypeHint' => false,
@@ -22,6 +49,7 @@ public function testEnabledNativeNoErrors(): void
2249
public function testEnabledNativeErrors(): void
2350
{
2451
$report = self::checkFile(__DIR__ . '/data/propertyTypeHintEnabledNativeErrors.php', [
52+
'enableNativeTypeHint' => true,
2553
'enableMixedTypeHint' => true,
2654
'enableUnionTypeHint' => false,
2755
'enableIntersectionTypeHint' => false,
@@ -92,6 +120,7 @@ public function testEnabledNativeErrors(): void
92120
public function testEnabledNativeWithUnionNoErrors(): void
93121
{
94122
$report = self::checkFile(__DIR__ . '/data/propertyTypeHintEnabledNativeWithUnionNoErrors.php', [
123+
'enableNativeTypeHint' => true,
95124
'enableMixedTypeHint' => true,
96125
'enableUnionTypeHint' => true,
97126
'enableIntersectionTypeHint' => false,
@@ -104,6 +133,7 @@ public function testEnabledNativeWithUnionNoErrors(): void
104133
public function testEnabledNativeWithUnionErrors(): void
105134
{
106135
$report = self::checkFile(__DIR__ . '/data/propertyTypeHintEnabledNativeWithUnionErrors.php', [
136+
'enableNativeTypeHint' => true,
107137
'enableMixedTypeHint' => true,
108138
'enableUnionTypeHint' => true,
109139
'enableIntersectionTypeHint' => false,
@@ -133,6 +163,7 @@ public function testEnabledNativeWithUnionErrors(): void
133163
public function testEnabledNativeWithIntersectionNoErrors(): void
134164
{
135165
$report = self::checkFile(__DIR__ . '/data/propertyTypeHintEnabledNativeWithIntersectionNoErrors.php', [
166+
'enableNativeTypeHint' => true,
136167
'enableMixedTypeHint' => true,
137168
'enableUnionTypeHint' => false,
138169
'enableIntersectionTypeHint' => true,
@@ -145,6 +176,7 @@ public function testEnabledNativeWithIntersectionNoErrors(): void
145176
public function testEnabledNativeWithIntersectionErrors(): void
146177
{
147178
$report = self::checkFile(__DIR__ . '/data/propertyTypeHintEnabledNativeWithIntersectionErrors.php', [
179+
'enableNativeTypeHint' => true,
148180
'enableMixedTypeHint' => true,
149181
'enableUnionTypeHint' => false,
150182
'enableIntersectionTypeHint' => true,
@@ -163,6 +195,7 @@ public function testEnabledNativeWithIntersectionErrors(): void
163195
public function testWithNullTrueFalseErrors(): void
164196
{
165197
$report = self::checkFile(__DIR__ . '/data/propertyTypeHintWithNullTrueFalseErrors.php', [
198+
'enableNativeTypeHint' => true,
166199
'enableMixedTypeHint' => true,
167200
'enableUnionTypeHint' => true,
168201
'enableIntersectionTypeHint' => true,

0 commit comments

Comments
 (0)