Skip to content

Add ArrowFunctionSpacingSniff #4

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 1 commit into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions Proton/Sniffs/Spacing/ArrowFunctionSpacingSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Proton\Sniffs\Spacing;

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

class ArrowFunctionSpacingSniff implements Sniff
{
public function register()
{
return [T_FN];
}

public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if ($tokens[$stackPtr]['code'] === T_FN && $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
$phpcsFile->addError(
'The fn arrow function token should always be followed by a space.',
$stackPtr,
'Found'
);
}
}
}
2 changes: 2 additions & 0 deletions Proton/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@
<severity>1</severity>
</rule>

<rule ref="Proton.Spacing.ArrowFunctionSpacing"/>

<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/reports/*</exclude-pattern>
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"prefer-stable": true,
"scripts": {
"test-print": "phpcs --standard=tests/phpcs.xml --report=summary tests",
"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"
"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"
},
"require": {
"php": "^7.1",
"php": "^7.1 || ^8.0",
"slevomat/coding-standard": "^6.4",
"squizlabs/php_codesniffer": "^3.5"
},
Expand Down
2 changes: 2 additions & 0 deletions tests/correct/ClassOk.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function __construct(array $config)
];

phpinfo2();

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

public function ping(Request $request)
Expand Down
2 changes: 1 addition & 1 deletion tests/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PHP CODE SNIFFER REPORT SUMMARY
------------------------------------------------------------------------------------------------
FILE ERRORS WARNINGS
------------------------------------------------------------------------------------------------
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 31 3
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 39 4
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 4 2
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 2 0
------------------------------------------------------------------------------------------------
Expand Down
43 changes: 22 additions & 21 deletions tests/expected_csv.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@ Line,Column,Type,Source
1,1,error,SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
8,1,error, found 1."
6,18,error,PSR12.Files.FileHeader.SpacingInsideBlock
54,1,error,PSR2.Files.EndFileNewline.NoneFound
50,54,error,SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing.WhitespaceBeforeColon
50,52,error,PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon
50,20,warning,Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
50,20,error,PSR1.Methods.CamelCapsMethodName.NotCamelCaps
48,8,error,SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration.MissingDescription
43,13,error,SlevomatCodingStandard.Functions.TrailingCommaInCall.MissingTrailingComma
42,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
56,1,error,PSR2.Files.EndFileNewline.NoneFound
52,54,error,SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing.WhitespaceBeforeColon
52,52,error,PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon
52,20,warning,Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
52,20,error,PSR1.Methods.CamelCapsMethodName.NotCamelCaps
50,8,error,SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration.MissingDescription
45,13,error,SlevomatCodingStandard.Functions.TrailingCommaInCall.MissingTrailingComma
44,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
43,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
41,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
40,13,warning,Squiz.PHP.NonExecutableCode.Unreachable
40,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
4,1,error, found 2."
39,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
38,13,warning,Squiz.PHP.NonExecutableCode.Unreachable
38,9,error,Generic.PHP.LowerCaseKeyword.Found
38,9,error,Squiz.ControlStructures.LowercaseDeclaration.FoundUppercase
38,9,error,Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword
38,12,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
38,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
37,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
36,9,error,Generic.PHP.LowerCaseKeyword.Found
36,9,error,Squiz.ControlStructures.LowercaseDeclaration.FoundUppercase
36,9,error,Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword
36,12,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
36,9,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
36,24,warning,Generic.CodeAnalysis.EmptyPHPStatement.SemicolonWithoutCodeDetected
36,24,error,SlevomatCodingStandard.PHP.UselessSemicolon.UselessSemicolon
36,24,error,Generic.Formatting.DisallowMultipleStatements.SameLine
36,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
34,9,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
34,24,warning,Generic.CodeAnalysis.EmptyPHPStatement.SemicolonWithoutCodeDetected
34,24,error,SlevomatCodingStandard.PHP.UselessSemicolon.UselessSemicolon
34,24,error,Generic.Formatting.DisallowMultipleStatements.SameLine
34,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
32,31,error,Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine
32,13,error,SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
34,31,error,Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine
34,13,error,SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
31,24,error,Proton.Spacing.ArrowFunctionSpacing.Found
28,27,error,PSR12.Classes.AnonClassDeclaration.SpaceAfterKeyword
25,13,error,SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
22,9,error,Proton.Architecture.ForbiddenNamespacedFunctions.FoundWithAlternative
Expand Down
2 changes: 2 additions & 0 deletions tests/wrong/Class1.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function __construct()

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

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

private function unused() {
Expand Down