class RequireNullCoalesceEqualOperatorSniff
Hierarchy
- class \SlevomatCodingStandard\Sniffs\ControlStructures\RequireNullCoalesceEqualOperatorSniff implements \PHP_CodeSniffer\Sniffs\Sniff
Expanded class hierarchy of RequireNullCoalesceEqualOperatorSniff
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ RequireNullCoalesceEqualOperatorSniff.php, line 16
Namespace
SlevomatCodingStandard\Sniffs\ControlStructuresView source
class RequireNullCoalesceEqualOperatorSniff implements Sniff {
public const CODE_REQUIRED_NULL_COALESCE_EQUAL_OPERATOR = 'RequiredNullCoalesceEqualOperator';
/** @var bool|null */
public $enable = null;
/**
* @return array<int, (int|string)>
*/
public function register() : array {
return [
T_EQUAL,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $equalPointer
*/
public function process(File $phpcsFile, $equalPointer) : void {
$this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 70400);
if (!$this->enable) {
return;
}
/** @var int $variableStartPointer */
$variableStartPointer = TokenHelper::findNextEffective($phpcsFile, $equalPointer + 1);
$variableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $variableStartPointer);
if ($variableEndPointer === null) {
return;
}
$nullCoalescePointer = TokenHelper::findNextEffective($phpcsFile, $variableEndPointer + 1);
$tokens = $phpcsFile->getTokens();
if ($tokens[$nullCoalescePointer]['code'] !== T_COALESCE) {
return;
}
$variableContent = IdentificatorHelper::getContent($phpcsFile, $variableStartPointer, $variableEndPointer);
/** @var int $beforeEqualEndPointer */
$beforeEqualEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $equalPointer - 1);
$beforeEqualStartPointer = IdentificatorHelper::findStartPointer($phpcsFile, $beforeEqualEndPointer);
if ($beforeEqualStartPointer === null) {
return;
}
$beforeEqualVariableContent = IdentificatorHelper::getContent($phpcsFile, $beforeEqualStartPointer, $beforeEqualEndPointer);
if ($beforeEqualVariableContent !== $variableContent) {
return;
}
$semicolonPointer = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $equalPointer + 1);
if (TokenHelper::findNext($phpcsFile, Tokens::$operators, $nullCoalescePointer + 1, $semicolonPointer) !== null) {
return;
}
$fix = $phpcsFile->addFixableError('Use "??=" operator instead of "=" and "??".', $equalPointer, self::CODE_REQUIRED_NULL_COALESCE_EQUAL_OPERATOR);
if (!$fix) {
return;
}
$phpcsFile->fixer
->beginChangeset();
FixerHelper::change($phpcsFile, $equalPointer, $nullCoalescePointer, '??=');
$phpcsFile->fixer
->endChangeset();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
RequireNullCoalesceEqualOperatorSniff::$enable | public | property | @var bool|null | |
RequireNullCoalesceEqualOperatorSniff::CODE_REQUIRED_NULL_COALESCE_EQUAL_OPERATOR | public | constant | ||
RequireNullCoalesceEqualOperatorSniff::process | public | function | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * |
Overrides Sniff::process |
RequireNullCoalesceEqualOperatorSniff::register | public | function | * | Overrides Sniff::register |