Skip to content

Commit 85573ab

Browse files
authored
Merge pull request #34 from Lassombria/add-forbidden-t-comment-type-rule
add forbidden T_COMMENT types rule
2 parents 4b9817e + 58636c8 commit 85573ab

File tree

5 files changed

+231
-14
lines changed

5 files changed

+231
-14
lines changed

src/PhpCs/FiveLab/ErrorCodes.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
*/
1919
final class ErrorCodes
2020
{
21-
public const MISSED_LINE_AFTER = 'MissedLineAfter';
22-
public const MISSED_LINE_BEFORE = 'MissedLineBefore';
23-
public const MISSED = 'Missed';
24-
public const WRONG_FORMAT = 'WrongFormat';
25-
public const MULTIPLE = 'Multiple';
26-
public const PROHIBITED = 'Prohibited';
27-
public const UNUSED = 'Unused';
28-
public const MISSED_PARAMETER_TYPE = 'MissedFunctionParameterType';
29-
public const MISSED_RETURN_TYPE = 'MissedFunctionReturnType';
30-
public const LINE_AFTER_NOT_ALLOWED = 'LineAfterNotAllowed';
31-
public const LINE_BEFORE_NOT_ALLOWED = 'LineBeforeNotAllowed';
32-
public const MISSED_CONSTANT_TYPE = 'MissedConstantType';
33-
public const NAMESPACE_WRONG = 'NamespaceWrong';
34-
public const ARRAYS_DOC_VECTOR = 'ArraysDocVector';
21+
public const MISSED_LINE_AFTER = 'MissedLineAfter';
22+
public const MISSED_LINE_BEFORE = 'MissedLineBefore';
23+
public const MISSED = 'Missed';
24+
public const WRONG_FORMAT = 'WrongFormat';
25+
public const MULTIPLE = 'Multiple';
26+
public const PROHIBITED = 'Prohibited';
27+
public const UNUSED = 'Unused';
28+
public const MISSED_PARAMETER_TYPE = 'MissedFunctionParameterType';
29+
public const MISSED_RETURN_TYPE = 'MissedFunctionReturnType';
30+
public const LINE_AFTER_NOT_ALLOWED = 'LineAfterNotAllowed';
31+
public const LINE_BEFORE_NOT_ALLOWED = 'LineBeforeNotAllowed';
32+
public const MISSED_CONSTANT_TYPE = 'MissedConstantType';
33+
public const NAMESPACE_WRONG = 'NamespaceWrong';
34+
public const ARRAYS_DOC_VECTOR = 'ArraysDocVector';
35+
public const COMMENT_OUTSIDE_FUNCTION_BODY = 'CommentOutsideFunctionBody';
3536
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
/*
6+
* This file is part of the FiveLab CiRules package
7+
*
8+
* (c) FiveLab
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code
12+
*/
13+
14+
namespace FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Commenting;
15+
16+
use FiveLab\Component\CiRules\PhpCs\FiveLab\ErrorCodes;
17+
use PHP_CodeSniffer\Files\File;
18+
use PHP_CodeSniffer\Sniffs\Sniff;
19+
20+
class ProhibitedCommentsSniff implements Sniff
21+
{
22+
public function register(): array
23+
{
24+
return [
25+
T_COMMENT,
26+
];
27+
}
28+
29+
public function process(File $phpcsFile, mixed $stackPtr): void
30+
{
31+
$tokens = $phpcsFile->getTokens();
32+
$commentToken = $tokens[$stackPtr];
33+
$isInsideFunction = false;
34+
35+
if (\str_contains($commentToken['content'], '@phpstan')) {
36+
return;
37+
}
38+
39+
if (\str_starts_with($commentToken['content'], '\*') || \str_starts_with($commentToken['content'], ' *') || \str_starts_with($commentToken['content'], '/*')) {
40+
$beginPtr = $stackPtr;
41+
42+
foreach ($tokens as $ptr => $token) {
43+
if (T_NAMESPACE === $token['code']) {
44+
if ($beginPtr < $ptr) {
45+
return;
46+
}
47+
}
48+
}
49+
}
50+
51+
foreach ($tokens as $token) {
52+
if ($token['code'] === T_OPEN_CURLY_BRACKET) {
53+
$conditionToken = $tokens[$token['scope_condition']];
54+
55+
if (\in_array($conditionToken['code'], [T_FUNCTION, T_CLOSURE], true)) {
56+
if ($stackPtr > $token['scope_opener'] && $stackPtr < $token['scope_closer']) {
57+
$isInsideFunction = true;
58+
break;
59+
}
60+
}
61+
}
62+
}
63+
64+
if (!$isInsideFunction) {
65+
$phpcsFile->addError(
66+
'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
67+
$stackPtr,
68+
ErrorCodes::COMMENT_OUTSIDE_FUNCTION_BODY
69+
);
70+
}
71+
}
72+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
/*
6+
* This file is part of the FiveLab CiRules package
7+
*
8+
* (c) FiveLab
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code
12+
*/
13+
14+
namespace FiveLab\Component\CiRules\Tests\PhpCs\FiveLab\Sniffs\Commenting;
15+
16+
use FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Commenting\ProhibitedCommentsSniff;
17+
use FiveLab\Component\CiRules\Tests\PhpCs\FiveLab\Sniffs\SniffTestCase;
18+
19+
class ProhibitedCommentsSniffTest extends SniffTestCase
20+
{
21+
protected function getSniffClass(): string
22+
{
23+
return ProhibitedCommentsSniff::class;
24+
}
25+
26+
public static function provideDataSet(): array
27+
{
28+
return [
29+
'success' => [
30+
__DIR__.'/Resources/prohibited-comments/success.php',
31+
],
32+
33+
'wrong' => [
34+
__DIR__.'/Resources/prohibited-comments/wrong.php',
35+
[
36+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
37+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
38+
],
39+
[
40+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
41+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
42+
],
43+
[
44+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
45+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
46+
],
47+
[
48+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
49+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
50+
],
51+
[
52+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
53+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
54+
],
55+
[
56+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
57+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
58+
],
59+
[
60+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
61+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
62+
],
63+
[
64+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
65+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
66+
],
67+
[
68+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
69+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
70+
],
71+
[
72+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
73+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
74+
],
75+
[
76+
'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.',
77+
'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody',
78+
],
79+
],
80+
];
81+
}
82+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FiveLab CiRules package
5+
*
6+
* (c) FiveLab
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code
10+
*/
11+
12+
namespace\bar\foo\;
13+
14+
// phpcs:ignore
15+
# phpcs:ignore
16+
public function foo(): void
17+
{
18+
// comment
19+
# comment
20+
}
21+
22+
class Some
23+
{
24+
# @phpstan-ignore-line
25+
public function foo(): void // @phpstan-ignore-line
26+
{
27+
// comment
28+
# comment
29+
30+
$d = 1; // comment
31+
$d = 3; # comment
32+
33+
$r = static function (): void {
34+
$e = 3; // comment
35+
$e = 4; # comment
36+
};
37+
}
38+
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
// comment
4+
# comment
5+
public function foo(): void {}
6+
7+
// comment
8+
class Some
9+
{
10+
// comment
11+
private string $foo; // comment
12+
13+
# comment
14+
private string $foo2; # comment
15+
16+
// comment
17+
public function foo(): void // comment
18+
{}
19+
20+
# comment
21+
public function bar(): void # comment
22+
{}
23+
}

0 commit comments

Comments
 (0)