Skip to content

Commit 5828d26

Browse files
committed
Squiz/FunctionDeclarationArgumentSpacing: refactor logic for for "spacing after comma" check
This refactor has two purposes: * Reduce code duplication. * Prevent introducing even more code duplication when a new error code will be introduced in the next commit.
1 parent 5ba0444 commit 5828d26

File tree

1 file changed

+39
-48
lines changed

1 file changed

+39
-48
lines changed

src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -370,55 +370,46 @@ public function processBracket($phpcsFile, $openBracket)
370370
}
371371

372372
if ($checkComma === true) {
373-
if ($param['type_hint_token'] === false) {
374-
$spacesAfter = 0;
375-
if ($tokens[($commaToken + 1)]['code'] === T_WHITESPACE) {
376-
$spacesAfter = $tokens[($commaToken + 1)]['length'];
377-
}
373+
$typeOfNext = 'argument';
374+
$typeOfNextShort = 'Arg';
375+
$contentOfNext = $param['name'];
376+
377+
if ($param['type_hint_token'] !== false) {
378+
$typeOfNext = 'type hint';
379+
$typeOfNextShort = 'Hint';
380+
$contentOfNext = $param['type_hint'];
381+
}
378382

379-
if ($spacesAfter === 0) {
380-
$error = 'Expected 1 space between comma and argument "%s"; 0 found';
381-
$data = [$param['name']];
382-
$fix = $phpcsFile->addFixableError($error, $commaToken, 'NoSpaceBeforeArg', $data);
383-
if ($fix === true) {
384-
$phpcsFile->fixer->addContent($commaToken, ' ');
385-
}
386-
} else if ($spacesAfter !== 1) {
387-
$error = 'Expected 1 space between comma and argument "%s"; %s found';
388-
$data = [
389-
$param['name'],
390-
$spacesAfter,
391-
];
392-
393-
$fix = $phpcsFile->addFixableError($error, $commaToken, 'SpacingBeforeArg', $data);
394-
if ($fix === true) {
395-
$phpcsFile->fixer->replaceToken(($commaToken + 1), ' ');
396-
}
397-
}//end if
398-
} else {
399-
$hint = $param['type_hint'];
400-
401-
if ($tokens[($commaToken + 1)]['code'] !== T_WHITESPACE) {
402-
$error = 'Expected 1 space between comma and type hint "%s"; 0 found';
403-
$data = [$hint];
404-
$fix = $phpcsFile->addFixableError($error, $commaToken, 'NoSpaceBeforeHint', $data);
405-
if ($fix === true) {
406-
$phpcsFile->fixer->addContent($commaToken, ' ');
407-
}
408-
} else {
409-
$gap = $tokens[($commaToken + 1)]['length'];
410-
if ($gap !== 1) {
411-
$error = 'Expected 1 space between comma and type hint "%s"; %s found';
412-
$data = [
413-
$hint,
414-
$gap,
415-
];
416-
$fix = $phpcsFile->addFixableError($error, $commaToken, 'SpacingBeforeHint', $data);
417-
if ($fix === true) {
418-
$phpcsFile->fixer->replaceToken(($commaToken + 1), ' ');
419-
}
420-
}
421-
}//end if
383+
$spacesAfter = 0;
384+
if ($tokens[($commaToken + 1)]['code'] === T_WHITESPACE) {
385+
$spacesAfter = $tokens[($commaToken + 1)]['length'];
386+
}
387+
388+
if ($spacesAfter === 0) {
389+
$error = 'Expected 1 space between comma and %s "%s"; 0 found';
390+
$errorCode = 'NoSpaceBefore'.$typeOfNextShort;
391+
$data = [
392+
$typeOfNext,
393+
$contentOfNext,
394+
];
395+
396+
$fix = $phpcsFile->addFixableError($error, $commaToken, $errorCode, $data);
397+
if ($fix === true) {
398+
$phpcsFile->fixer->addContent($commaToken, ' ');
399+
}
400+
} else if ($spacesAfter !== 1) {
401+
$error = 'Expected 1 space between comma and %s "%s"; %s found';
402+
$errorCode = 'SpacingBefore'.$typeOfNextShort;
403+
$data = [
404+
$typeOfNext,
405+
$contentOfNext,
406+
$spacesAfter,
407+
];
408+
409+
$fix = $phpcsFile->addFixableError($error, $commaToken, $errorCode, $data);
410+
if ($fix === true) {
411+
$phpcsFile->fixer->replaceToken(($commaToken + 1), ' ');
412+
}
422413
}//end if
423414
}//end if
424415
}//end if

0 commit comments

Comments
 (0)