Skip to content

Commit

Permalink
SlevomatCodingStandard.Operators.RequireCombinedAssignmentOperator: T…
Browse files Browse the repository at this point in the history
…ry to ignore string offsets
  • Loading branch information
kukulich committed Aug 2, 2022
1 parent ea4153e commit a2ea28b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
use SlevomatCodingStandard\Helpers\IdentificatorHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function array_key_exists;
use function in_array;
use function sprintf;
use const T_BITWISE_AND;
use const T_BITWISE_OR;
use const T_BITWISE_XOR;
use const T_CLOSE_SQUARE_BRACKET;
use const T_CONSTANT_ENCAPSED_STRING;
use const T_DIVIDE;
use const T_DNUMBER;
use const T_DOUBLE_QUOTED_STRING;
use const T_EQUAL;
use const T_LNUMBER;
use const T_MINUS;
use const T_MODULUS;
use const T_MULTIPLY;
Expand All @@ -23,6 +28,8 @@
use const T_SEMICOLON;
use const T_SL;
use const T_SR;
use const T_START_HEREDOC;
use const T_START_NOWDOC;
use const T_STRING_CONCAT;

class RequireCombinedAssignmentOperatorSniff implements Sniff
Expand Down Expand Up @@ -77,6 +84,21 @@ public function process(File $phpcsFile, $equalPointer): void
return;
}

$isFixable = true;

if ($tokens[$variableEndPointer]['code'] === T_CLOSE_SQUARE_BRACKET) {
$pointerAfterOperator = TokenHelper::findNextEffective($phpcsFile, $operatorPointer + 1);
if (in_array(
$tokens[$pointerAfterOperator]['code'],
[T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_QUOTED_STRING, T_START_HEREDOC, T_START_NOWDOC],
true
)) {
return;
}

$isFixable = in_array($tokens[$pointerAfterOperator]['code'], [T_LNUMBER, T_DNUMBER], true);
}

$variableContent = IdentificatorHelper::getContent($phpcsFile, $variableStartPointer, $variableEndPointer);

/** @var int $beforeEqualEndPointer */
Expand Down Expand Up @@ -104,9 +126,6 @@ public function process(File $phpcsFile, $equalPointer): void
$tokens[$operatorPointer]['content']
);

// Not fixable with possible string offset
$isFixable = $tokens[$variableEndPointer]['code'] !== T_CLOSE_SQUARE_BRACKET;

if (!$isFixable) {
$phpcsFile->addError($errorMessage, $equalPointer, self::CODE_REQUIRED_COMBINED_ASSIGMENT_OPERATOR);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireCombinedAssignmentOperatorErrors.php');

self::assertSame(17, $report->getErrorCount());
self::assertSame(21, $report->getErrorCount());

self::assertSniffError(
$report,
Expand Down Expand Up @@ -96,9 +96,15 @@ public function testErrors(): void
self::assertSniffError($report, 21, RequireCombinedAssignmentOperatorSniff::CODE_REQUIRED_COMBINED_ASSIGMENT_OPERATOR);
self::assertSniffError($report, 24, RequireCombinedAssignmentOperatorSniff::CODE_REQUIRED_COMBINED_ASSIGMENT_OPERATOR);

self::assertSniffError($report, 27, RequireCombinedAssignmentOperatorSniff::CODE_REQUIRED_COMBINED_ASSIGMENT_OPERATOR);
self::assertSniffError($report, 28, RequireCombinedAssignmentOperatorSniff::CODE_REQUIRED_COMBINED_ASSIGMENT_OPERATOR);
self::assertSniffError($report, 30, RequireCombinedAssignmentOperatorSniff::CODE_REQUIRED_COMBINED_ASSIGMENT_OPERATOR);
self::assertSniffError($report, 31, RequireCombinedAssignmentOperatorSniff::CODE_REQUIRED_COMBINED_ASSIGMENT_OPERATOR);

// Not fixable
self::assertSniffError($report, 36, RequireCombinedAssignmentOperatorSniff::CODE_REQUIRED_COMBINED_ASSIGMENT_OPERATOR);
self::assertSniffError($report, 37, RequireCombinedAssignmentOperatorSniff::CODE_REQUIRED_COMBINED_ASSIGMENT_OPERATOR);

self::assertAllFixedInFile($report);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ public function __construct($parameter)

$this
->something += 10;

$a[0] &= 1;
$b[0] |= 1;

$a[0] &= 1.0;
$b[0] |= 1.0;
}

public function possibleStringOffsets($a, $b)
public function notFixable($variable)
{
$a[0] = $a[0] & '';
$b[0] = $b[0] | '';
$a[0] = $a[0] & true;
$b[0] = $b[0] | $variable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ public function __construct($parameter)
$this
->something = $this
->something + 10;

$a[0] = $a[0] & 1;
$b[0] = $b[0] | 1;

$a[0] = $a[0] & 1.0;
$b[0] = $b[0] | 1.0;
}

public function possibleStringOffsets($a, $b)
public function notFixable($variable)
{
$a[0] = $a[0] & '';
$b[0] = $b[0] | '';
$a[0] = $a[0] & true;
$b[0] = $b[0] | $variable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,24 @@ public function moreOperators($a, $b, $c)
$a = $a / $b / $c;
}

public function probablyStringOffsets($a, $b, $c)
{
$a[0] = $a[0] & '';
$b[0] = $b[0] | '';

$a[0] = $a[0] & "";
$b[0] = $b[0] | "";

$a[0] = $a[0] & "{$c}";
$b[0] = $b[0] | "{$c}";

$a[0] = $a[0] & <<<CODE
Something
CODE;
$b[0] = $b[0] | <<<CODE
Something
CODE;
}

}

0 comments on commit a2ea28b

Please sign in to comment.