Skip to content

Commit

Permalink
Merge pull request #366 from cakephp/5.x-fn-spacing
Browse files Browse the repository at this point in the history
Add spacing check for arrow function closures.
  • Loading branch information
othercorey authored Nov 8, 2022
2 parents 947b2ac + 90baca0 commit 6cbe404
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CakePHP/Sniffs/Functions/ClosureDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ClosureDeclarationSniff implements Sniff
*/
public function register()
{
return [T_CLOSURE];
return [T_CLOSURE, T_FN];
}

/**
Expand All @@ -43,7 +43,8 @@ public function process(File $phpcsFile, $stackPtr)
}

if ($spaces !== 1) {
$error = 'Expected 1 space after closure\'s function keyword; %s found';
$keyword = $tokens[$stackPtr]['code'] === T_FN ? 'fn' : 'function';
$error = "Expected 1 space after closure's $keyword keyword; %s found";
$data = [$spaces];
$phpcsFile->addError($error, $stackPtr, 'SpaceAfterFunction', $data);
}
Expand Down
5 changes: 4 additions & 1 deletion CakePHP/Tests/Functions/ClosureDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class TraitUser
echo 'It works';
};
$visitor($this);

$visitor = fn($expression) => echo 'It Fails';
$visitor = fn ($expression) => echo 'It works';
}
}

Expand All @@ -33,4 +36,4 @@ $zum = function()use ($foo, $bar) {

$zum = function ()use ($foo, $bar) {
return $foo;
};
};
3 changes: 2 additions & 1 deletion CakePHP/Tests/Functions/ClosureDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public function getErrorList()
{
return [
17 => 1,
30 => 1,
25 => 1,
33 => 1,
];
}

Expand Down

0 comments on commit 6cbe404

Please sign in to comment.