Skip to content

Commit 81fa72a

Browse files
Majkl578kukulich
authored andcommitted
Make fixability of DisallowShortTernaryOperator configurable
1 parent 4eedbb1 commit 81fa72a

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ Requires `new` with parentheses.
179179

180180
Disallows short ternary operator `?:`.
181181

182+
Sniff provides the following settings:
183+
184+
* `fixable`: the sniff is fixable by default, however in strict code it makes sense to forbid this weakly typed form of ternary altogether, you can disable fixability with this option.
185+
182186
#### SlevomatCodingStandard.ControlStructures.RequireShortTernaryOperator 🔧
183187

184188
Requires short ternary operator `?:` when possible.

SlevomatCodingStandard/Sniffs/ControlStructures/DisallowShortTernaryOperatorSniff.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class DisallowShortTernaryOperatorSniff implements \PHP_CodeSniffer\Sniffs\Sniff
99

1010
public const CODE_DISALLOWED_SHORT_TERNARY_OPERATOR = 'DisallowedShortTernaryOperator';
1111

12+
/** @var bool */
13+
public $fixable = true;
14+
1215
/**
1316
* @return mixed[]
1417
*/
@@ -43,6 +46,11 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $inlineThenPoint
4346
return;
4447
}
4548

49+
if (!$this->fixable) {
50+
$phpcsFile->addError($message, $inlineThenPointer, self::CODE_DISALLOWED_SHORT_TERNARY_OPERATOR);
51+
return;
52+
}
53+
4654
$fix = $phpcsFile->addFixableError($message, $inlineThenPointer, self::CODE_DISALLOWED_SHORT_TERNARY_OPERATOR);
4755

4856
if (!$fix) {

tests/Sniffs/ControlStructures/DisallowShortTernaryOperatorSniffTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,17 @@ public function testErrors(): void
2323
$this->assertAllFixedInFile($report);
2424
}
2525

26+
public function testFixableDisabled(): void
27+
{
28+
$report = self::checkFile(__DIR__ . '/data/disallowShortTernaryOperatorErrorsFixableDisabled.php', ['fixable' => false]);
29+
30+
self::assertSame(3, $report->getErrorCount());
31+
32+
self::assertSniffError($report, 3, DisallowShortTernaryOperatorSniff::CODE_DISALLOWED_SHORT_TERNARY_OPERATOR);
33+
self::assertSniffError($report, 5, DisallowShortTernaryOperatorSniff::CODE_DISALLOWED_SHORT_TERNARY_OPERATOR);
34+
self::assertSniffError($report, 7, DisallowShortTernaryOperatorSniff::CODE_DISALLOWED_SHORT_TERNARY_OPERATOR);
35+
36+
$this->assertAllFixedInFile($report);
37+
}
38+
2639
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$x = $a ?: true;
4+
5+
sprintf('%s', $x ?: 'string');
6+
7+
$z = isset($x) ?: false;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$x = $a ?: true;
4+
5+
sprintf('%s', $x ?: 'string');
6+
7+
$z = isset($x) ?: false;

0 commit comments

Comments
 (0)