Skip to content

Commit 2111e9c

Browse files
xellioRichard Prillwitz
and
Richard Prillwitz
authored
Removed PHP 7.4 support + added new sniff for space at (inline) comment (#15)
* - added sniff for abstract class naming convention; - added sniff for interface naming convention; - added basic tests (consistent to existing ones); * - code styling; * - code styling; * - enable worflow; * - added some debugging output for fixing/checking the github workflow; * - fix expected result; * - revert workflow changes; * - added return type hint; * - check only class on AbstractClassNameSniff; - optimized reading of the class name; * cleanup; * debugging testing output; * removed test debugging output; * - activate DisallowMultipleAssignment (only one assignment per line); - activate AssignmentInCondition rule; - replace custom AbstractClassNameSniff by existing AbstractClassNamePrefix; - replace custom InterfaceNameSniff by existing InterfaceNameSuffix; - cleanup; * - removed testing debug; - update expected test output; * - check for trait naming; * - removed testing output; * added more cs rules; * - removed testing output; * - error instead of warning; * - removed debug output; * - added new sniff for detecting wrong comments (based on PEAR.Commenting.InlineComment but checks for missing leading space after comment start); * - added test script (composer) for diff/fix; * - removed test output; * - removed PHP 7.4 from composer.json; * - separation of concerns; * - update test; Co-authored-by: Richard Prillwitz <richard.prillwitz@proton.ch>
1 parent 86c7508 commit 2111e9c

File tree

10 files changed

+330
-11
lines changed

10 files changed

+330
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
.phpintel
44
/vendor/
55
out.csv
6+
out.diff
67
composer.lock
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proton\Sniffs\Commenting;
6+
7+
use PHP_CodeSniffer\Files\File;
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
10+
class InlineCommentStartsWithSpaceSniff implements Sniff
11+
{
12+
/**
13+
* @inheritDoc
14+
*/
15+
public function register(): array
16+
{
17+
return [T_COMMENT];
18+
}
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function process(File $phpcsFile, $stackPtr): void
24+
{
25+
$tokens = $phpcsFile->getTokens();
26+
$content = $tokens[$stackPtr]['content'];
27+
28+
switch (true) {
29+
case $content[0] === '#':
30+
$commentStyle = '#';
31+
break;
32+
case $content[0] === '/' && $content[1] === '/':
33+
$commentStyle = '//';
34+
break;
35+
case $content[0] === '/' && $content[1] === '*':
36+
$commentStyle = '/*';
37+
break;
38+
default:
39+
return;
40+
}
41+
42+
$phpcsFile->recordMetric($stackPtr, 'Inline comment style', $commentStyle . '...');
43+
44+
$this->checkMissingSpaceSeparator($commentStyle, $content, $phpcsFile, $stackPtr);
45+
}
46+
47+
private function checkMissingSpaceSeparator(string $style, string $content, File $phpcsFile, int $stackPtr): void
48+
{
49+
$styleLength = strlen($style);
50+
if (trim($content) === $style || $content[$styleLength] === ' ') {
51+
return;
52+
}
53+
54+
$error = 'Missing space between comment start and comment description.';
55+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'WrongStyle');
56+
if ($fix === true) {
57+
$type = substr($content, 0, $styleLength);
58+
$newComment = sprintf("%s %s", $type, ltrim($content, $type));
59+
$phpcsFile->fixer->replaceToken($stackPtr, $newComment);
60+
}
61+
}
62+
}

Proton/Sniffs/Spacing/ArrowFunctionSpacingSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function register(): array
2020
/**
2121
* @inheritDoc
2222
*/
23-
public function process(File $phpcsFile, $stackPtr)
23+
public function process(File $phpcsFile, $stackPtr): void
2424
{
2525
$tokens = $phpcsFile->getTokens();
2626

Proton/ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
</rule>
1515

1616
<rule ref="PEAR.Commenting.InlineComment"/>
17+
<rule ref="Proton.Commenting.InlineCommentStartsWithSpace"/>
1718

1819
<rule ref="Generic.Commenting.Todo"/>
1920
<rule ref="Generic.PHP.BacktickOperator"/>

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
"minimum-stability": "dev",
1212
"prefer-stable": true,
1313
"scripts": {
14+
"test-fix": "phpcs --standard=phpcs.xml --report=diff tests > tests/out.diff && diff tests/out.diff tests/expected.diff",
1415
"test-print": "phpcs --standard=phpcs.xml --report=summary tests",
1516
"test": "phpcs --standard=phpcs.xml --report=csv tests | sort -r | cut -f 2,3,4,6 -d ',' > tests/out.csv && diff tests/expected_csv.txt tests/out.csv"
1617
},
1718
"require": {
18-
"php": "^7.4 || ^8.0",
19+
"php": "^8.0",
1920
"slevomat/coding-standard": "^8.0",
2021
"squizlabs/php_codesniffer": "^3.7"
2122
},

tests/correct/ClassOk.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ final class ClassOk
2525

2626
public function __construct(array $config)
2727
{
28+
// This is a valid comment
2829
$this->bar = [
2930
'bar' => 'foo',
3031
];
3132

33+
/* This is another valid comment */
3234
$this->foo = $config + [
3335
'foo' => 'bar',
3436
];

tests/expected.diff

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
--- tests/wrong/file.php
2+
+++ PHP_CodeSniffer
3+
@@ -1,4 +1,5 @@
4+
<?php
5+
6+
-/* */
7+
-return [];
8+
\ No newline at end of file
9+
+declare(strict_types=1);
10+
+
11+
+return [];
12+
--- tests/wrong/Class1.php
13+
+++ PHP_CodeSniffer
14+
@@ -1,19 +1,17 @@
15+
<?php
16+
17+
+declare(strict_types=1);
18+
19+
namespace Test;
20+
21+
-use Something\Foo;
22+
-
23+
-use Something\bar;
24+
-
25+
-abstract final class Test {
26+
-
27+
- private $unused_field;
28+
+abstract final class Test
29+
+{
30+
+ private $unused_field;
31+
32+
const TEST = 'NOTOK';
33+
34+
- use MyTrait, MyTrait2;
35+
+ use MyTrait;
36+
+ use MyTrait2;
37+
38+
public function __construct()
39+
{
40+
@@ -22,49 +20,51 @@
41+
phpinfo2();
42+
43+
print_r([
44+
- 1
45+
- ]);
46+
+ 1,
47+
+ ]);
48+
49+
- $this->test = new class() {
50+
+ $this->test = new class () {
51+
};
52+
53+
call_user_func(fn() => {});
54+
}
55+
56+
- private function unused() {
57+
+ private function unused()
58+
+ {
59+
$fooBar = 1;
60+
- $foo_bar = '1';;
61+
+ $foo_bar = '1';
62+
63+
- IF($foo_bar == '1') {
64+
- return true;
65+
- return true;
66+
- }
67+
-
68+
- $this->foo(
69+
- 'a',
70+
- 2
71+
+
72+
+ if ($foo_bar === '1') {
73+
+ return true;
74+
+ return true;
75+
+ }
76+
+
77+
+ $this->foo(
78+
+ 'a',
79+
+ 2,
80+
);
81+
}
82+
83+
/**
84+
- * @param string $a
85+
* @deprecated
86+
*/
87+
- private static function Foo(string $a, $b, $c) : array |object
88+
+ private static function Foo(string $a, $b, $c): array|object
89+
{
90+
return [$a, $b];
91+
}
92+
93+
- private function testUnion(null|float|int|string $a,): null|int|float
94+
+ private function testUnion(float|int|string|null $a,): int|float|null
95+
{
96+
return 1;
97+
}
98+
99+
- private function testAssignmentInCondition(int $a): void {
100+
- if ($a == 1 && $b = 2) {
101+
+ private function testAssignmentInCondition(int $a): void
102+
+ {
103+
+ if ($a === 1 && $b = 2) {
104+
$c = 3;
105+
}
106+
107+
- $c == 3 && $d = 4;
108+
+ $c === 3 && $d = 4;
109+
}
110+
-}
111+
\ No newline at end of file
112+
+}
113+
--- tests/wrong/ClassWrongAbstract.php
114+
+++ PHP_CodeSniffer
115+
@@ -2,54 +2,33 @@
116+
117+
declare(strict_types=1);
118+
119+
-
120+
namespace wrong;
121+
122+
-use RuntimeException;
123+
-
124+
-/**
125+
- * Class ClassWrongAbstract
126+
- */
127+
abstract class ClassWrongAbstract
128+
{
129+
-
130+
abstract public function setOne(int $one = 1): void;
131+
132+
- /**
133+
- * one getter.
134+
- *
135+
- * Created by me.
136+
- * User: me
137+
- * Date: now
138+
- * Time: now
139+
- *
140+
- */
141+
public function getOne(): int
142+
{
143+
- //There should be a space separator
144+
+ // There should be a space separator
145+
return 1;
146+
}
147+
148+
- #[Attribute1, Attribute2('var')]
149+
- #[Attribute3(), Attribute4]
150+
/**
151+
* Method comment
152+
*/
153+
+ #[Attribute1, Attribute2('var')]
154+
+ #[Attribute3(), Attribute4]
155+
public function method(
156+
+ /** @param int $parameter */
157+
#[Attribute1] #[Attribute2] #[Attribute3]
158+
#[Attribute4] #[Attribute5] #[Attribute6]
159+
- /** @param int $parameter */
160+
int $parameter,
161+
): void {
162+
- /*There should be a space separator*/
163+
+ /* There should be a space separator*/
164+
echo $parameter;
165+
- # This comment is not allowed
166+
- #This is wrong, too
167+
- //
168+
- /**/
169+
-
170+
- //
171+
- //The space is missing again
172+
- //
173+
+ // This comment is not allowed
174+
+ // This is wrong, too
175+
+ // The space is missing again
176+
}
177+
}
178+
--- tests/wrong/ContainsHTML.php
179+
+++ PHP_CodeSniffer
180+
@@ -3,18 +3,16 @@
181+
declare(strict_types=1);
182+
183+
$foo = [
184+
- "foo" => 1,
185+
+ "foo" => 1,
186+
];
187+
188+
$bar = [
189+
- "bar" => 1,
190+
+ "bar" => 1,
191+
];
192+
193+
eval("\$str = \"foo\";");
194+
195+
-//
196+
-
197+
-if (count($foo) == 1) {
198+
+if (count($foo) === 1) {
199+
}
200+
201+
?>
202+
--- tests/wrong/ClassMetrics.php
203+
+++ PHP_CodeSniffer
204+
@@ -1,8 +1,11 @@
205+
<?php
206+
207+
+declare(strict_types=1);
208+
+
209+
namespace Test;
210+
211+
-class ClassMetrics {
212+
+class ClassMetrics
213+
+{
214+
public function foo($a, $b, $c, $d, $e, $f)
215+
{
216+
if ($a) {
217+
@@ -17,10 +20,6 @@
218+
}
219+
}
220+
221+
- if (true) {
222+
- return true;
223+
- } else {
224+
- return false;
225+
- }
226+
+ return true;
227+
}
228+
}
229+

tests/expected.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ PHP CODE SNIFFER REPORT SUMMARY
33
------------------------------------------------------------------------------------------------------
44
FILE ERRORS WARNINGS
55
------------------------------------------------------------------------------------------------------
6-
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 51 6
6+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 57 6
77
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 11 2
8-
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassWrongAbstract.php 3 0
9-
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 2 0
8+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassWrongAbstract.php 24 0
9+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ContainsHTML.php 6 1
10+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 3 0
1011
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/InterfaceWrong.php 1 0
1112
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/TraitInvalid.php 1 0
1213
------------------------------------------------------------------------------------------------------
13-
A TOTAL OF 91 ERRORS AND 9 WARNINGS WERE FOUND IN 7 FILES
14+
A TOTAL OF 103 ERRORS AND 9 WARNINGS WERE FOUND IN 7 FILES
1415
------------------------------------------------------------------------------------------------------
15-
PHPCBF CAN FIX 63 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
16+
PHPCBF CAN FIX 75 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
1617
------------------------------------------------------------------------------------------------------

tests/expected_csv.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@ Line,Column,Type,Source
1313
10,3,error,Generic.Arrays.ArrayIndent.KeyIncorrect
1414
8,1,error,SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse
1515
6,1,error, found 2."
16-
39,37,error,SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment.AttributeBeforeDocComment
17-
33,5,error,SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment.AttributeBeforeDocComment
16+
53,9,error,SlevomatCodingStandard.Commenting.EmptyComment.EmptyComment
17+
52,9,error,Proton.Commenting.InlineCommentStartsWithSpace.WrongStyle
18+
51,9,error,SlevomatCodingStandard.Commenting.EmptyComment.EmptyComment
19+
49,9,error,SlevomatCodingStandard.Commenting.EmptyComment.EmptyComment
20+
48,9,error,SlevomatCodingStandard.Commenting.EmptyComment.EmptyComment
21+
47,9,error,PEAR.Commenting.InlineComment.WrongStyle
22+
47,9,error,Proton.Commenting.InlineCommentStartsWithSpace.WrongStyle
23+
46,9,error,PEAR.Commenting.InlineComment.WrongStyle
24+
44,9,error,Proton.Commenting.InlineCommentStartsWithSpace.WrongStyle
25+
40,37,error,SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment.AttributeBeforeDocComment
26+
34,5,error,SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment.AttributeBeforeDocComment
1827
3,1,error, found 2."
28+
29,9,error,Proton.Commenting.InlineCommentStartsWithSpace.WrongStyle
29+
24,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
1930
23,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
2031
22,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
32+
21,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
2133
19,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
2234
14,1,error,SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces.IncorrectEmptyLinesAfterOpeningBrace
2335
14,1,error,PSR12.Classes.OpeningBraceSpace.Found

0 commit comments

Comments
 (0)