-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from 27 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 5423d18
- code styling;
xellio 4beddbc
- code styling;
xellio eee2a33
- enable worflow;
xellio 625c2d0
- added some debugging output for fixing/checking the github workflow;
xellio a7b4f80
- fix expected result;
xellio db01629
- revert workflow changes;
xellio 96c924a
- added return type hint;
xellio 1526e8d
- check only class on AbstractClassNameSniff;
9e0cba7
cleanup;
178664d
merged sniffs;
76bdd03
debugging testing output;
9ae8ab6
removed test debugging output;
fb28eea
Merge branch 'ProtonMail:master' into master
xellio 557f8f2
- activate DisallowMultipleAssignment (only one assignment per line);
a28dec8
- removed testing debug;
1df400d
- check for trait naming;
cd9a33d
- removed testing output;
0cae80e
added more cs rules;
c972c2e
- removed testing output;
915c68b
- error instead of warning;
5f2338a
- removed debug output;
54b6b65
Merge branch 'ProtonMail:master' into master
xellio 37ddb0b
- added new sniff for detecting wrong comments (based on PEAR.Comment…
b183241
- added test script (composer) for diff/fix;
764917e
- removed test output;
00fe4db
- removed PHP 7.4 from composer.json;
1364aaf
- separation of concerns;
5e4c2bd
- update test;
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
.phpintel | ||
/vendor/ | ||
out.csv | ||
out.diff | ||
composer.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Proton\Sniffs\Commenting; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
class InlineCommentSniff implements Sniff | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function register(): array | ||
{ | ||
return [T_COMMENT]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr): void | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
if ($tokens[$stackPtr]['content'][0] === '#') { | ||
$phpcsFile->recordMetric($stackPtr, 'Inline comment style', '# ...'); | ||
BafS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$error = 'Perl-style comments are not allowed. Use "// Comment."'; | ||
$error .= ' or "/* comment */" instead.'; | ||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'WrongStyle'); | ||
if ($fix === true) { | ||
$newComment = ltrim($tokens[$stackPtr]['content'], '# '); | ||
$newComment = '// ' . $newComment; | ||
$phpcsFile->fixer->replaceToken($stackPtr, $newComment); | ||
} | ||
} elseif ($tokens[$stackPtr]['content'][0] === '/' | ||
&& $tokens[$stackPtr]['content'][1] === '/' | ||
) { | ||
$phpcsFile->recordMetric($stackPtr, 'Inline comment style', '// ...'); | ||
|
||
$this->checkMissingSpaceSeparator($tokens[$stackPtr]['content'], $phpcsFile, $stackPtr); | ||
} elseif ($tokens[$stackPtr]['content'][0] === '/' | ||
&& $tokens[$stackPtr]['content'][1] === '*' | ||
) { | ||
$phpcsFile->recordMetric($stackPtr, 'Inline comment style', '/* ... */'); | ||
|
||
$this->checkMissingSpaceSeparator($tokens[$stackPtr]['content'], $phpcsFile, $stackPtr); | ||
} | ||
} | ||
|
||
private function checkMissingSpaceSeparator(string $content, File $phpcsFile, int $stackPtr): void | ||
{ | ||
if ($content[2] === ' ') { | ||
return; | ||
} | ||
|
||
$error = 'Missing space between comment start and comment description.'; | ||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'WrongStyle'); | ||
if ($fix === true) { | ||
$type = substr($content, 0, 2); | ||
$newComment = sprintf("%s %s", $type, ltrim($content, $type)); | ||
$phpcsFile->fixer->replaceToken($stackPtr, $newComment); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
--- 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,48 +2,32 @@ | ||
|
||
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 | ||
+ // This comment is not allowed | ||
+ // This is wrong, too | ||
} | ||
} | ||
--- tests/wrong/ContainsHTML.php | ||
+++ PHP_CodeSniffer | ||
@@ -3,18 +3,18 @@ | ||
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; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.