Skip to content

Commit 34475d8

Browse files
committed
feat: introduce InpsydeTemplate standard; add TrailingSemicolonSniff
1 parent 7de8480 commit 34475d8

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 InpsydeTemplates\Sniffs\Formatting;
30+
31+
use PHP_CodeSniffer\Files\File;
32+
use PHP_CodeSniffer\Sniffs\Sniff;
33+
use PHP_CodeSniffer\Util\Tokens;
34+
35+
/**
36+
* @psalm-type Token = array{
37+
* type: string,
38+
* code: string|int,
39+
* line: int
40+
* }
41+
*/
42+
final class TrailingSemicolonSniff implements Sniff
43+
{
44+
/**
45+
* @return list<int|string>
46+
*/
47+
public function register(): array
48+
{
49+
return [
50+
T_SEMICOLON,
51+
];
52+
}
53+
54+
/**
55+
* @param File $phpcsFile
56+
* @param int $stackPtr
57+
*
58+
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
59+
*/
60+
public function process(File $phpcsFile, $stackPtr): void
61+
{
62+
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
63+
64+
/** @var array<int, Token> $tokens */
65+
$tokens = $phpcsFile->getTokens();
66+
$currentLine = $tokens[$stackPtr]['line'];
67+
68+
$nextNonEmptyPosition = $phpcsFile->findNext(
69+
Tokens::$emptyTokens,
70+
($stackPtr + 1),
71+
null,
72+
true
73+
);
74+
75+
if (!is_int($nextNonEmptyPosition) || !isset($tokens[$nextNonEmptyPosition])) {
76+
return;
77+
}
78+
79+
$nextNonEmptyToken = $tokens[$nextNonEmptyPosition];
80+
81+
if ($nextNonEmptyToken['line'] !== $currentLine) {
82+
return;
83+
}
84+
85+
if ($nextNonEmptyToken['code'] !== T_CLOSE_TAG) {
86+
return;
87+
}
88+
89+
$message = sprintf('Trailing semicolon found at line %d.', $currentLine);
90+
91+
if ($phpcsFile->addFixableWarning($message, $stackPtr, 'Found')) {
92+
$this->fix($stackPtr, $phpcsFile);
93+
}
94+
}
95+
96+
/**
97+
* @param int $position
98+
* @param File $file
99+
*/
100+
private function fix(int $position, File $file): void
101+
{
102+
$fixer = $file->fixer;
103+
$fixer->beginChangeset();
104+
105+
$fixer->replaceToken($position, '');
106+
107+
$fixer->endChangeset();
108+
}
109+
}

InpsydeTemplates/ruleset.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Inpsyde PHP Templates Coding Standard">
3+
4+
<description>Coding standards for PHP templates.</description>
5+
6+
</ruleset>

0 commit comments

Comments
 (0)