Skip to content

Commit c25e07a

Browse files
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.
1 parent 6edffc8 commit c25e07a

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the "php-coding-standards" package.
5+
*
6+
* Copyright (c) 2023 Inpsyde GmbH
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
declare(strict_types=1);
28+
29+
namespace Inpsyde\Sniffs\CodeQuality;
30+
31+
use PHP_CodeSniffer\Files\File;
32+
use PHP_CodeSniffer\Sniffs\Sniff;
33+
use PHPCSUtils\Utils\PassedParameters;
34+
35+
class EncodingCommentSniff implements Sniff
36+
{
37+
private const DISALLOWED_COMMENT = '-*- coding: utf-8 -*-';
38+
39+
/**
40+
* @return list<int>
41+
*/
42+
public function register(): array
43+
{
44+
return [T_COMMENT];
45+
}
46+
47+
/**
48+
* @param File $phpcsFile
49+
* @param int $stackPtr
50+
* @return void
51+
*
52+
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
53+
*/
54+
public function process(File $phpcsFile, $stackPtr): void
55+
{
56+
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
57+
58+
$tokens = $phpcsFile->getTokens();
59+
60+
$comment = isset($tokens[$stackPtr]['content']) && is_string($tokens[$stackPtr]['content'])
61+
? $tokens[$stackPtr]['content']
62+
: '';
63+
64+
if (strpos($comment, self::DISALLOWED_COMMENT) === false) {
65+
return;
66+
}
67+
68+
$fix = $phpcsFile->addFixableWarning(
69+
'Found outdated encoding declaration in comment.',
70+
$stackPtr,
71+
'EncodingComment'
72+
);
73+
74+
if ($fix) {
75+
$this->fix($phpcsFile, $stackPtr);
76+
}
77+
}
78+
79+
private function fix(File $phpcsFile, int $position): void
80+
{
81+
$phpcsFile->fixer->beginChangeset();
82+
83+
$phpcsFile->fixer->replaceToken($position, '');
84+
85+
$phpcsFile->fixer->endChangeset();
86+
}
87+
}

inpsyde-custom-sniffs.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
- Inpsyde.CodeQuality.ConstantVisibility
77
- Inpsyde.CodeQuality.DisallowShortOpenTag
88
- Inpsyde.CodeQuality.ElementNameMinimalLength
9+
- Inpsyde.CodeQuality.EncodingComment
910
- Inpsyde.CodeQuality.ForbiddenPublicProperty
1011
- Inpsyde.CodeQuality.FunctionBodyStart
1112
- Inpsyde.CodeQuality.FunctionLength
1213
- Inpsyde.CodeQuality.HookClosureReturn
14+
- Inpsyde.CodeQuality.HookPriority
1315
- Inpsyde.CodeQuality.LineLength
1416
- Inpsyde.CodeQuality.NestingLevel
1517
- Inpsyde.CodeQuality.NoAccessors
@@ -95,6 +97,15 @@ alternatively, whitelist can be extended via `additionalAllowedNames` config, e.
9597
</rule>
9698
```
9799

100+
-----
101+
## Inpsyde.CodeQuality.EncodingComment
102+
103+
Prevent usage of some outdated encoding definition in PHP comments.
104+
105+
It raises a Warning if something like this is found `-*- coding: utf-8 -*-`.
106+
107+
This sniff has no available configuration.
108+
98109
-----
99110

100111
## Inpsyde.CodeQuality.ForbiddenPublicProperty

tests/fixtures/encoding-comment.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Inpsyde\CodingStandard\Tests\Fixtures;
4+
5+
6+
// @phpcsSniff CodeQuality.EncodingComment
7+
8+
// @phpcsWarningCodeOnNextLine EncodingComment
9+
// -*- coding: utf-8 -*-
10+
?>
11+
12+
<?php # -*- coding: utf-8 -*-
13+
// @phpcsWarningOnPreviousLine CodeQuality.EncodingComment
14+
?>
15+
16+
<?php //-*- coding: utf-8 -*-
17+
// @phpcsWarningOnPreviousLine CodeQuality.EncodingComment
18+
?>

0 commit comments

Comments
 (0)