Skip to content

Removed PHP 7.4 support + added new sniff for space at (inline) comment #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1d13a3f
- added sniff for abstract class naming convention;
xellio Dec 1, 2022
5423d18
- code styling;
xellio Dec 1, 2022
4beddbc
- code styling;
xellio Dec 1, 2022
eee2a33
- enable worflow;
xellio Dec 1, 2022
625c2d0
- added some debugging output for fixing/checking the github workflow;
xellio Dec 1, 2022
a7b4f80
- fix expected result;
xellio Dec 1, 2022
db01629
- revert workflow changes;
xellio Dec 1, 2022
96c924a
- added return type hint;
xellio Dec 2, 2022
1526e8d
- check only class on AbstractClassNameSniff;
Dec 8, 2022
9e0cba7
cleanup;
Dec 8, 2022
178664d
merged sniffs;
Dec 8, 2022
76bdd03
debugging testing output;
Dec 8, 2022
9ae8ab6
removed test debugging output;
Dec 8, 2022
fb28eea
Merge branch 'ProtonMail:master' into master
xellio Dec 9, 2022
557f8f2
- activate DisallowMultipleAssignment (only one assignment per line);
Dec 9, 2022
a28dec8
- removed testing debug;
Dec 9, 2022
1df400d
- check for trait naming;
Dec 9, 2022
cd9a33d
- removed testing output;
Dec 9, 2022
0cae80e
added more cs rules;
Dec 12, 2022
c972c2e
- removed testing output;
Dec 12, 2022
915c68b
- error instead of warning;
Dec 12, 2022
5f2338a
- removed debug output;
Dec 12, 2022
54b6b65
Merge branch 'ProtonMail:master' into master
xellio Dec 12, 2022
37ddb0b
- added new sniff for detecting wrong comments (based on PEAR.Comment…
Dec 12, 2022
b183241
- added test script (composer) for diff/fix;
Dec 12, 2022
764917e
- removed test output;
Dec 12, 2022
00fe4db
- removed PHP 7.4 from composer.json;
Dec 12, 2022
1364aaf
- separation of concerns;
Dec 13, 2022
5e4c2bd
- update test;
Dec 13, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
.phpintel
/vendor/
out.csv
out.diff
composer.lock
62 changes: 62 additions & 0 deletions Proton/Sniffs/Commenting/InlineCommentStartsWithSpaceSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Proton\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class InlineCommentStartsWithSpaceSniff implements Sniff
{
/**
* @inheritDoc
*/
public function register(): array
{
return [T_COMMENT];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
$content = $tokens[$stackPtr]['content'];

switch (true) {
case $content[0] === '#':
$commentStyle = '#';
break;
case $content[0] === '/' && $content[1] === '/':
$commentStyle = '//';
break;
case $content[0] === '/' && $content[1] === '*':
$commentStyle = '/*';
break;
default:
return;
}

$phpcsFile->recordMetric($stackPtr, 'Inline comment style', $commentStyle . '...');

$this->checkMissingSpaceSeparator($commentStyle, $content, $phpcsFile, $stackPtr);
}

private function checkMissingSpaceSeparator(string $style, string $content, File $phpcsFile, int $stackPtr): void
{
$styleLength = strlen($style);
if (trim($content) === $style || $content[$styleLength] === ' ') {
return;
}

$error = 'Missing space between comment start and comment description.';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'WrongStyle');
if ($fix === true) {
$type = substr($content, 0, $styleLength);
$newComment = sprintf("%s %s", $type, ltrim($content, $type));
$phpcsFile->fixer->replaceToken($stackPtr, $newComment);
}
}
}
2 changes: 1 addition & 1 deletion Proton/Sniffs/Spacing/ArrowFunctionSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function register(): array
/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();

Expand Down
1 change: 1 addition & 0 deletions Proton/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</rule>

<rule ref="PEAR.Commenting.InlineComment"/>
<rule ref="Proton.Commenting.InlineCommentStartsWithSpace"/>

<rule ref="Generic.Commenting.Todo"/>
<rule ref="Generic.PHP.BacktickOperator"/>
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"test-fix": "phpcs --standard=phpcs.xml --report=diff tests > tests/out.diff && diff tests/out.diff tests/expected.diff",
"test-print": "phpcs --standard=phpcs.xml --report=summary tests",
"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"
},
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"slevomat/coding-standard": "^8.0",
"squizlabs/php_codesniffer": "^3.7"
},
Expand Down
2 changes: 2 additions & 0 deletions tests/correct/ClassOk.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ final class ClassOk

public function __construct(array $config)
{
// This is a valid comment
$this->bar = [
'bar' => 'foo',
];

/* This is another valid comment */
$this->foo = $config + [
'foo' => 'bar',
];
Expand Down
229 changes: 229 additions & 0 deletions tests/expected.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
--- tests/wrong/file.php
+++ PHP_CodeSniffer
@@ -1,4 +1,5 @@
<?php

-/* */
-return [];
\ No newline at end of file
+declare(strict_types=1);
+
+return [];
--- tests/wrong/Class1.php
+++ PHP_CodeSniffer
@@ -1,19 +1,17 @@
<?php

+declare(strict_types=1);

namespace Test;

-use Something\Foo;
-
-use Something\bar;
-
-abstract final class Test {
-
- private $unused_field;
+abstract final class Test
+{
+ private $unused_field;

const TEST = 'NOTOK';

- use MyTrait, MyTrait2;
+ use MyTrait;
+ use MyTrait2;

public function __construct()
{
@@ -22,49 +20,51 @@
phpinfo2();

print_r([
- 1
- ]);
+ 1,
+ ]);

- $this->test = new class() {
+ $this->test = new class () {
};

call_user_func(fn() => {});
}

- private function unused() {
+ private function unused()
+ {
$fooBar = 1;
- $foo_bar = '1';;
+ $foo_bar = '1';

- IF($foo_bar == '1') {
- return true;
- return true;
- }
-
- $this->foo(
- 'a',
- 2
+
+ if ($foo_bar === '1') {
+ return true;
+ return true;
+ }
+
+ $this->foo(
+ 'a',
+ 2,
);
}

/**
- * @param string $a
* @deprecated
*/
- private static function Foo(string $a, $b, $c) : array |object
+ private static function Foo(string $a, $b, $c): array|object
{
return [$a, $b];
}

- private function testUnion(null|float|int|string $a,): null|int|float
+ private function testUnion(float|int|string|null $a,): int|float|null
{
return 1;
}

- private function testAssignmentInCondition(int $a): void {
- if ($a == 1 && $b = 2) {
+ private function testAssignmentInCondition(int $a): void
+ {
+ if ($a === 1 && $b = 2) {
$c = 3;
}

- $c == 3 && $d = 4;
+ $c === 3 && $d = 4;
}
-}
\ No newline at end of file
+}
--- tests/wrong/ClassWrongAbstract.php
+++ PHP_CodeSniffer
@@ -2,54 +2,33 @@

declare(strict_types=1);

-
namespace wrong;

-use RuntimeException;
-
-/**
- * Class ClassWrongAbstract
- */
abstract class ClassWrongAbstract
{
-
abstract public function setOne(int $one = 1): void;

- /**
- * one getter.
- *
- * Created by me.
- * User: me
- * Date: now
- * Time: now
- *
- */
public function getOne(): int
{
- //There should be a space separator
+ // There should be a space separator
return 1;
}

- #[Attribute1, Attribute2('var')]
- #[Attribute3(), Attribute4]
/**
* Method comment
*/
+ #[Attribute1, Attribute2('var')]
+ #[Attribute3(), Attribute4]
public function method(
+ /** @param int $parameter */
#[Attribute1] #[Attribute2] #[Attribute3]
#[Attribute4] #[Attribute5] #[Attribute6]
- /** @param int $parameter */
int $parameter,
): void {
- /*There should be a space separator*/
+ /* There should be a space separator*/
echo $parameter;
- # This comment is not allowed
- #This is wrong, too
- //
- /**/
-
- //
- //The space is missing again
- //
+ // This comment is not allowed
+ // This is wrong, too
+ // The space is missing again
}
}
--- tests/wrong/ContainsHTML.php
+++ PHP_CodeSniffer
@@ -3,18 +3,16 @@
declare(strict_types=1);

$foo = [
- "foo" => 1,
+ "foo" => 1,
];

$bar = [
- "bar" => 1,
+ "bar" => 1,
];

eval("\$str = \"foo\";");

-//
-
-if (count($foo) == 1) {
+if (count($foo) === 1) {
}

?>
--- tests/wrong/ClassMetrics.php
+++ PHP_CodeSniffer
@@ -1,8 +1,11 @@
<?php

+declare(strict_types=1);
+
namespace Test;

-class ClassMetrics {
+class ClassMetrics
+{
public function foo($a, $b, $c, $d, $e, $f)
{
if ($a) {
@@ -17,10 +20,6 @@
}
}

- if (true) {
- return true;
- } else {
- return false;
- }
+ return true;
}
}

11 changes: 6 additions & 5 deletions tests/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ PHP CODE SNIFFER REPORT SUMMARY
------------------------------------------------------------------------------------------------------
FILE ERRORS WARNINGS
------------------------------------------------------------------------------------------------------
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 51 6
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 57 6
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 11 2
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassWrongAbstract.php 3 0
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 2 0
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassWrongAbstract.php 24 0
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ContainsHTML.php 6 1
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 3 0
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/InterfaceWrong.php 1 0
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/TraitInvalid.php 1 0
------------------------------------------------------------------------------------------------------
A TOTAL OF 91 ERRORS AND 9 WARNINGS WERE FOUND IN 7 FILES
A TOTAL OF 103 ERRORS AND 9 WARNINGS WERE FOUND IN 7 FILES
------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX 63 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 75 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------
16 changes: 14 additions & 2 deletions tests/expected_csv.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@ Line,Column,Type,Source
10,3,error,Generic.Arrays.ArrayIndent.KeyIncorrect
8,1,error,SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse
6,1,error, found 2."
39,37,error,SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment.AttributeBeforeDocComment
33,5,error,SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment.AttributeBeforeDocComment
53,9,error,SlevomatCodingStandard.Commenting.EmptyComment.EmptyComment
52,9,error,Proton.Commenting.InlineCommentStartsWithSpace.WrongStyle
51,9,error,SlevomatCodingStandard.Commenting.EmptyComment.EmptyComment
49,9,error,SlevomatCodingStandard.Commenting.EmptyComment.EmptyComment
48,9,error,SlevomatCodingStandard.Commenting.EmptyComment.EmptyComment
47,9,error,PEAR.Commenting.InlineComment.WrongStyle
47,9,error,Proton.Commenting.InlineCommentStartsWithSpace.WrongStyle
46,9,error,PEAR.Commenting.InlineComment.WrongStyle
44,9,error,Proton.Commenting.InlineCommentStartsWithSpace.WrongStyle
40,37,error,SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment.AttributeBeforeDocComment
34,5,error,SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment.AttributeBeforeDocComment
3,1,error, found 2."
29,9,error,Proton.Commenting.InlineCommentStartsWithSpace.WrongStyle
24,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
23,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
22,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
21,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
19,8,error,SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden
14,1,error,SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces.IncorrectEmptyLinesAfterOpeningBrace
14,1,error,PSR12.Classes.OpeningBraceSpace.Found
Expand Down
Loading