Skip to content

Commit 4ee6ff0

Browse files
authored
Merge pull request #4 from kylekatarnls/feature/arrow-function-spacing
Add ArrowFunctionSpacingSniff
2 parents 2212cd5 + db5754f commit 4ee6ff0

File tree

7 files changed

+60
-24
lines changed

7 files changed

+60
-24
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proton\Sniffs\Spacing;
6+
7+
use PHP_CodeSniffer\Files\File;
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
10+
class ArrowFunctionSpacingSniff implements Sniff
11+
{
12+
public function register()
13+
{
14+
return [T_FN];
15+
}
16+
17+
public function process(File $phpcsFile, $stackPtr)
18+
{
19+
$tokens = $phpcsFile->getTokens();
20+
21+
if ($tokens[$stackPtr]['code'] === T_FN && $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
22+
$phpcsFile->addError(
23+
'The fn arrow function token should always be followed by a space.',
24+
$stackPtr,
25+
'Found'
26+
);
27+
}
28+
}
29+
}

Proton/ruleset.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@
204204
<severity>1</severity>
205205
</rule>
206206

207+
<rule ref="Proton.Spacing.ArrowFunctionSpacing"/>
208+
207209
<exclude-pattern>*/node_modules/*</exclude-pattern>
208210
<exclude-pattern>*/vendor/*</exclude-pattern>
209211
<exclude-pattern>*/reports/*</exclude-pattern>

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"prefer-stable": true,
1313
"scripts": {
1414
"test-print": "phpcs --standard=tests/phpcs.xml --report=summary tests",
15-
"test": "phpcs --standard=tests/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"
15+
"test": "phpcs --standard=tests/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"
1616
},
1717
"require": {
18-
"php": "^7.1",
18+
"php": "^7.1 || ^8.0",
1919
"slevomat/coding-standard": "^6.4",
2020
"squizlabs/php_codesniffer": "^3.5"
2121
},

tests/correct/ClassOk.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function __construct(array $config)
2424
];
2525

2626
phpinfo2();
27+
28+
call_user_func(fn () => {});
2729
}
2830

2931
public function ping(Request $request)

tests/expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PHP CODE SNIFFER REPORT SUMMARY
33
------------------------------------------------------------------------------------------------
44
FILE ERRORS WARNINGS
55
------------------------------------------------------------------------------------------------
6-
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 31 3
6+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 39 4
77
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 4 2
88
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 2 0
99
------------------------------------------------------------------------------------------------

tests/expected_csv.txt

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,33 @@ Line,Column,Type,Source
99
1,1,error,SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
1010
8,1,error, found 1."
1111
6,18,error,PSR12.Files.FileHeader.SpacingInsideBlock
12-
54,1,error,PSR2.Files.EndFileNewline.NoneFound
13-
50,54,error,SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing.WhitespaceBeforeColon
14-
50,52,error,PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon
15-
50,20,warning,Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
16-
50,20,error,PSR1.Methods.CamelCapsMethodName.NotCamelCaps
17-
48,8,error,SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration.MissingDescription
18-
43,13,error,SlevomatCodingStandard.Functions.TrailingCommaInCall.MissingTrailingComma
19-
42,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
12+
56,1,error,PSR2.Files.EndFileNewline.NoneFound
13+
52,54,error,SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing.WhitespaceBeforeColon
14+
52,52,error,PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon
15+
52,20,warning,Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
16+
52,20,error,PSR1.Methods.CamelCapsMethodName.NotCamelCaps
17+
50,8,error,SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration.MissingDescription
18+
45,13,error,SlevomatCodingStandard.Functions.TrailingCommaInCall.MissingTrailingComma
19+
44,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
20+
43,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
2021
41,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
22+
40,13,warning,Squiz.PHP.NonExecutableCode.Unreachable
23+
40,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
2124
4,1,error, found 2."
2225
39,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
23-
38,13,warning,Squiz.PHP.NonExecutableCode.Unreachable
26+
38,9,error,Generic.PHP.LowerCaseKeyword.Found
27+
38,9,error,Squiz.ControlStructures.LowercaseDeclaration.FoundUppercase
28+
38,9,error,Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword
29+
38,12,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
2430
38,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
25-
37,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
26-
36,9,error,Generic.PHP.LowerCaseKeyword.Found
27-
36,9,error,Squiz.ControlStructures.LowercaseDeclaration.FoundUppercase
28-
36,9,error,Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword
29-
36,12,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
31+
36,9,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
32+
36,24,warning,Generic.CodeAnalysis.EmptyPHPStatement.SemicolonWithoutCodeDetected
33+
36,24,error,SlevomatCodingStandard.PHP.UselessSemicolon.UselessSemicolon
34+
36,24,error,Generic.Formatting.DisallowMultipleStatements.SameLine
3035
36,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
31-
34,9,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
32-
34,24,warning,Generic.CodeAnalysis.EmptyPHPStatement.SemicolonWithoutCodeDetected
33-
34,24,error,SlevomatCodingStandard.PHP.UselessSemicolon.UselessSemicolon
34-
34,24,error,Generic.Formatting.DisallowMultipleStatements.SameLine
35-
34,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
36-
32,31,error,Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine
37-
32,13,error,SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
36+
34,31,error,Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine
37+
34,13,error,SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
38+
31,24,error,Proton.Spacing.ArrowFunctionSpacing.Found
3839
28,27,error,PSR12.Classes.AnonClassDeclaration.SpaceAfterKeyword
3940
25,13,error,SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
4041
22,9,error,Proton.Architecture.ForbiddenNamespacedFunctions.FoundWithAlternative

tests/wrong/Class1.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public function __construct()
2727

2828
$this->test = new class() {
2929
};
30+
31+
call_user_func(fn() => {});
3032
}
3133

3234
private function unused() {

0 commit comments

Comments
 (0)