PHP 8.2.31
Preview: DisallowTrailingCommaInClosureUseSniff.php Size: 2.33 KB
/opt/cpanel/ea-wappspector/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/DisallowTrailingCommaInClosureUseSniff.php

<?php declare(strict_types = 1);

namespace SlevomatCodingStandard\Sniffs\Functions;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use const T_CLOSURE;
use const T_COMMA;
use const T_USE;
use const T_WHITESPACE;

class DisallowTrailingCommaInClosureUseSniff implements Sniff
{

	public const CODE_DISALLOWED_TRAILING_COMMA = 'DisallowedTrailingComma';

	public bool $onlySingleLine = false;

	/**
	 * @return array<int, (int|string)>
	 */
	public function register(): array
	{
		return [
			T_CLOSURE,
		];
	}

	/**
	 * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
	 * @param int $functionPointer
	 */
	public function process(File $phpcsFile, $functionPointer): void
	{
		$tokens = $phpcsFile->getTokens();

		$parenthesisCloserPointer = $tokens[$functionPointer]['parenthesis_closer'];

		$usePointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisCloserPointer + 1);

		if ($tokens[$usePointer]['code'] !== T_USE) {
			return;
		}

		$useParenthesisOpenerPointer = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
		$useParenthesisCloserPointer = $tokens[$useParenthesisOpenerPointer]['parenthesis_closer'];

		$pointerBeforeUseParenthesisCloser = TokenHelper::findPreviousExcluding(
			$phpcsFile,
			T_WHITESPACE,
			$tokens[$useParenthesisOpenerPointer]['parenthesis_closer'] - 1,
			$useParenthesisOpenerPointer,
		);

		if ($tokens[$pointerBeforeUseParenthesisCloser]['code'] !== T_COMMA) {
			return;
		}

		if ($this->onlySingleLine && $tokens[$useParenthesisOpenerPointer]['line'] !== $tokens[$useParenthesisCloserPointer]['line']) {
			return;
		}

		$fix = $phpcsFile->addFixableError(
			'Trailing comma after the last inherited variable in "use" of closure declaration is disallowed.',
			$pointerBeforeUseParenthesisCloser,
			self::CODE_DISALLOWED_TRAILING_COMMA,
		);

		if (!$fix) {
			return;
		}

		$phpcsFile->fixer->beginChangeset();
		FixerHelper::replace($phpcsFile, $pointerBeforeUseParenthesisCloser, '');

		if ($tokens[$pointerBeforeUseParenthesisCloser]['line'] === $tokens[$useParenthesisCloserPointer]['line']) {
			FixerHelper::removeBetween($phpcsFile, $pointerBeforeUseParenthesisCloser, $useParenthesisCloserPointer);
		}

		$phpcsFile->fixer->endChangeset();
	}

}

Directory Contents

Dirs: 0 × Files: 21

Name Size Perms Modified Actions
2.59 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
5.06 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
755 B lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.03 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
925 B lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.58 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.33 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.96 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.62 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.92 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
4.23 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
7.53 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
5.37 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.72 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.21 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.94 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.57 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
3.37 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
3.55 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
3.49 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.13 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).