Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
10 changes: 10 additions & 0 deletions inpsyde-custom-sniffs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Inpsyde.CodeQuality.ConstantVisibility
- Inpsyde.CodeQuality.DisallowShortOpenTag
- Inpsyde.CodeQuality.ElementNameMinimalLength
- Inpsyde.CodeQuality.EncodingComment
- Inpsyde.CodeQuality.ForbiddenPublicProperty
- Inpsyde.CodeQuality.FunctionBodyStart
- Inpsyde.CodeQuality.FunctionLength
Expand Down Expand Up @@ -95,6 +96,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
?>