Skip to content

Commit

Permalink
Add check for Emacs UTF-8 file header
Browse files Browse the repository at this point in the history
This actually prevents the usage of that definition in every comment, not only on the same line of PHP tag opening.
  • Loading branch information
antonioeatgoat committed Oct 27, 2023
1 parent 6edffc8 commit c25e07a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Inpsyde/Sniffs/CodeQuality/EncodingCommentSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/*
* This file is part of the "php-coding-standards" package.
*
* Copyright (c) 2023 Inpsyde GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types=1);

namespace Inpsyde\Sniffs\CodeQuality;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHPCSUtils\Utils\PassedParameters;

class EncodingCommentSniff implements Sniff
{
private const DISALLOWED_COMMENT = '-*- coding: utf-8 -*-';

/**
* @return list<int>
*/
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();
}
}
11 changes: 11 additions & 0 deletions inpsyde-custom-sniffs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,6 +97,15 @@ alternatively, whitelist can be extended via `additionalAllowedNames` config, e.
</rule>
```

-----
## 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
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/encoding-comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Inpsyde\CodingStandard\Tests\Fixtures;


// @phpcsSniff CodeQuality.EncodingComment

// @phpcsWarningCodeOnNextLine EncodingComment
// -*- coding: utf-8 -*-
?>

<?php # -*- coding: utf-8 -*-
// @phpcsWarningOnPreviousLine CodeQuality.EncodingComment
?>

<?php //-*- coding: utf-8 -*-
// @phpcsWarningOnPreviousLine CodeQuality.EncodingComment
?>

0 comments on commit c25e07a

Please sign in to comment.