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

<?php declare(strict_types = 1);

namespace SlevomatCodingStandard\Sniffs\Functions;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SlevomatCodingStandard\Helpers\FunctionHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function in_array;
use const T_CLOSE_CURLY_BRACKET;
use const T_FUNCTION;
use const T_OPEN_CURLY_BRACKET;
use const T_PRIVATE;
use const T_PROTECTED;
use const T_SEMICOLON;
use const T_WHITESPACE;

class DisallowEmptyFunctionSniff implements Sniff
{

	public const CODE_EMPTY_FUNCTION = 'EmptyFunction';

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

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

		if (FunctionHelper::isAbstract($phpcsFile, $functionPointer)) {
			return;
		}

		if (FunctionHelper::getName($phpcsFile, $functionPointer) === '__construct') {
			$previousPointer = TokenHelper::findPrevious(
				$phpcsFile,
				[T_PRIVATE, T_SEMICOLON, T_CLOSE_CURLY_BRACKET, T_OPEN_CURLY_BRACKET],
				$functionPointer - 1,
			);

			if ($previousPointer !== null && in_array($tokens[$previousPointer]['code'], [T_PRIVATE, T_PROTECTED], true)) {
				return;
			}

			$propertyPromotion = TokenHelper::findNext(
				$phpcsFile,
				Tokens::$scopeModifiers,
				$tokens[$functionPointer]['parenthesis_opener'] + 1,
				$tokens[$functionPointer]['parenthesis_closer'],
			);

			if ($propertyPromotion !== null) {
				return;
			}
		}

		$firstContent = TokenHelper::findNextExcluding(
			$phpcsFile,
			T_WHITESPACE,
			$tokens[$functionPointer]['scope_opener'] + 1,
			$tokens[$functionPointer]['scope_closer'],
		);

		if ($firstContent !== null) {
			return;
		}

		$phpcsFile->addError(
			'Empty function body must have at least a comment to explain why is empty.',
			$functionPointer,
			self::CODE_EMPTY_FUNCTION,
		);
	}

}

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).