Skip to content

Commit a5dca47

Browse files
committed
SlevomatCodingStandard.ControlStructures.RequireMultiLineTernaryOperator: Workaround for missing property hooks support in PHPCS
1 parent 81f502f commit a5dca47

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

SlevomatCodingStandard/Helpers/TernaryOperatorHelper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
class TernaryOperatorHelper
3232
{
3333

34+
public static function is(File $phpcsFile, int $pointer): bool
35+
{
36+
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $pointer - 1);
37+
38+
return $phpcsFile->getTokens()[$previousPointer]['code'] !== T_OPEN_PARENTHESIS;
39+
}
40+
3441
public static function isConditionOfTernaryOperator(File $phpcsFile, int $pointer): bool
3542
{
3643
$inlineThenPointer = TokenHelper::findNext($phpcsFile, T_INLINE_THEN, $pointer + 1);

SlevomatCodingStandard/Sniffs/ControlStructures/RequireMultiLineTernaryOperatorSniff.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function register(): array
4040

4141
public function process(File $phpcsFile, int $inlineThenPointer): void
4242
{
43+
if (!TernaryOperatorHelper::is($phpcsFile, $inlineThenPointer)) {
44+
return;
45+
}
46+
4347
$this->lineLengthLimit = SniffSettingsHelper::normalizeInteger($this->lineLengthLimit);
4448
$this->minExpressionsLength = SniffSettingsHelper::normalizeNullableInteger($this->minExpressionsLength);
4549

tests/Sniffs/ControlStructures/RequireMultiLineTernaryOperatorSniffTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class RequireMultiLineTernaryOperatorSniffTest extends TestCase
1010
public function testNoErrors(): void
1111
{
1212
$report = self::checkFile(__DIR__ . '/data/requireMultiLineTernaryOperatorNoErrors.php', [
13-
'lineLengthLimit' => 80,
13+
'lineLengthLimit' => 100,
1414
]);
1515
self::assertNoSniffErrorInFile($report);
1616
}

tests/Sniffs/ControlStructures/data/requireMultiLineTernaryOperatorNoErrors.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php $a = $b === 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' ? 'bbbb' : 'ccccccc';
1+
<?php /* lint >= 8.4 */ $a = $b === 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' ? 'bbbb' : 'ccccccc';
22

33
$a = $b === 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' ? 'bbbb' : 'ccccccc';
44

@@ -45,3 +45,12 @@
4545
$a = $b === 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' ? 'bbbb' : 'ccccccccccccc';
4646

4747
$a = $b === 'bbbbbbbbbbbbbb' ? 'bbbb' : doSomething([], $c);
48+
49+
class Foo {
50+
public private(set) ?string $property {
51+
get => null;
52+
set(?string $value) {
53+
$this->otherProperty = $value;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)