Skip to content

Short function spacing sniff #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 3 commits into from
Apr 8, 2021
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
2 changes: 1 addition & 1 deletion DatacodeStandard/Sniffs/Classes/OpeningBraceSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function register() {
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return int
* @return integer
*/
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
Expand Down
61 changes: 61 additions & 0 deletions DatacodeStandard/Sniffs/Functions/ShortFunctionSpacingSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace DatacodeStandard\Sniffs\Functions;

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

/**
* Checks there is a space after a short function (fn)
*/
class ShortFunctionSpacingSniff implements Sniff {

public function register() {
return [
T_FN,
];
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return integer
*/
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();

$shouldBeWhiteSpace = $tokens[($stackPtr + 1)];

if ($shouldBeWhiteSpace['type'] === 'T_OPEN_PARENTHESIS') {
if ($tokens[($stackPtr + 1)]['content'] === $phpcsFile->eolChar) {
$spaces = 'newline';
} else if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
$spaces = $tokens[($stackPtr + 1)]['length'];
} else {
$spaces = 0;
}

if ($spaces !== 1) {
$error = 'Expected 1 space after FN keyword; %s found';
$data = [$spaces];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterFn', $data);

if ($fix === true) {
if ($spaces === 0) {
$phpcsFile->fixer->addContent($stackPtr, ' ');
} else {
$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
}
}
}
}

return ($phpcsFile->numTokens + 1);
}

}
8 changes: 8 additions & 0 deletions DatacodeStandard/Sniffs/PHP/RequireStrictTypesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public function register() {
];
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return integer
*/
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();

Expand Down
1 change: 1 addition & 0 deletions DatacodeStandard/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<rule ref="Squiz.Commenting.EmptyCatchComment"/>

<rule ref="./Sniffs/Commenting/FunctionCommentSniff.php"/>
<rule ref="./Sniffs/Functions/ShortFunctionSpacingSniff.php"/>

<rule ref="Generic.ControlStructures.InlineControlStructure"/>
<rule ref="Squiz.Operators.ValidLogicalOperators"/>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datacodetech/phpcs-ruleset",
"version": "2.1.1",
"version": "2.2.0",
"type": "library",
"license": "MIT",
"description": "phpcs ruleset used for all PHP projects",
Expand Down