From c25e07a09795c7a4fc650faa2c58aac1318fb891 Mon Sep 17 00:00:00 2001 From: Antonio Mangiacapra <11173091+antonioeatgoat@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:36:45 +0200 Subject: [PATCH] Add check for Emacs UTF-8 file header This actually prevents the usage of that definition in every comment, not only on the same line of PHP tag opening. --- .../CodeQuality/EncodingCommentSniff.php | 87 +++++++++++++++++++ inpsyde-custom-sniffs.md | 11 +++ tests/fixtures/encoding-comment.php | 18 ++++ 3 files changed, 116 insertions(+) create mode 100644 Inpsyde/Sniffs/CodeQuality/EncodingCommentSniff.php create mode 100644 tests/fixtures/encoding-comment.php diff --git a/Inpsyde/Sniffs/CodeQuality/EncodingCommentSniff.php b/Inpsyde/Sniffs/CodeQuality/EncodingCommentSniff.php new file mode 100644 index 0000000..e8f306a --- /dev/null +++ b/Inpsyde/Sniffs/CodeQuality/EncodingCommentSniff.php @@ -0,0 +1,87 @@ + + */ + public function register(): array + { + return [T_COMMENT]; + } + + /** + * @param File $phpcsFile + * @param int $stackPtr + * @return void + * + * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration + */ + public function process(File $phpcsFile, $stackPtr): void + { + // phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration + + $tokens = $phpcsFile->getTokens(); + + $comment = isset($tokens[$stackPtr]['content']) && is_string($tokens[$stackPtr]['content']) + ? $tokens[$stackPtr]['content'] + : ''; + + if (strpos($comment, self::DISALLOWED_COMMENT) === false) { + return; + } + + $fix = $phpcsFile->addFixableWarning( + 'Found outdated encoding declaration in comment.', + $stackPtr, + 'EncodingComment' + ); + + if ($fix) { + $this->fix($phpcsFile, $stackPtr); + } + } + + private function fix(File $phpcsFile, int $position): void + { + $phpcsFile->fixer->beginChangeset(); + + $phpcsFile->fixer->replaceToken($position, ''); + + $phpcsFile->fixer->endChangeset(); + } +} diff --git a/inpsyde-custom-sniffs.md b/inpsyde-custom-sniffs.md index fa80934..72967d7 100644 --- a/inpsyde-custom-sniffs.md +++ b/inpsyde-custom-sniffs.md @@ -6,10 +6,12 @@ - Inpsyde.CodeQuality.ConstantVisibility - Inpsyde.CodeQuality.DisallowShortOpenTag - Inpsyde.CodeQuality.ElementNameMinimalLength +- Inpsyde.CodeQuality.EncodingComment - Inpsyde.CodeQuality.ForbiddenPublicProperty - Inpsyde.CodeQuality.FunctionBodyStart - Inpsyde.CodeQuality.FunctionLength - Inpsyde.CodeQuality.HookClosureReturn +- Inpsyde.CodeQuality.HookPriority - Inpsyde.CodeQuality.LineLength - Inpsyde.CodeQuality.NestingLevel - Inpsyde.CodeQuality.NoAccessors @@ -95,6 +97,15 @@ alternatively, whitelist can be extended via `additionalAllowedNames` config, e. ``` +----- +## Inpsyde.CodeQuality.EncodingComment + +Prevent usage of some outdated encoding definition in PHP comments. + +It raises a Warning if something like this is found `-*- coding: utf-8 -*-`. + +This sniff has no available configuration. + ----- ## Inpsyde.CodeQuality.ForbiddenPublicProperty diff --git a/tests/fixtures/encoding-comment.php b/tests/fixtures/encoding-comment.php new file mode 100644 index 0000000..98b9ce3 --- /dev/null +++ b/tests/fixtures/encoding-comment.php @@ -0,0 +1,18 @@ + + + + + \ No newline at end of file