Skip to content

Commit 9ccd5a7

Browse files
committed
2 parents 3497dd1 + dc1722a commit 9ccd5a7

File tree

1 file changed

+110
-74
lines changed

1 file changed

+110
-74
lines changed

CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 110 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -459,43 +459,7 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
459459
$foundParams[] = $param['var'];
460460

461461
// Check number of spaces after the type.
462-
$spaces = ($maxType - strlen($param['type']) + 1);
463-
if ($param['type_space'] !== $spaces) {
464-
$error = 'Expected %s spaces after parameter type; %s found';
465-
$data = array(
466-
$spaces,
467-
$param['type_space'],
468-
);
469-
470-
$fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data);
471-
if ($fix === true) {
472-
$phpcsFile->fixer->beginChangeset();
473-
474-
$content = $param['type'];
475-
$content .= str_repeat(' ', $spaces);
476-
$content .= $param['var'];
477-
$content .= str_repeat(' ', $param['var_space']);
478-
$content .= $param['commentLines'][0]['comment'];
479-
$phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content);
480-
481-
// Fix up the indent of additional comment lines.
482-
foreach ($param['commentLines'] as $lineNum => $line) {
483-
if ($lineNum === 0
484-
|| $param['commentLines'][$lineNum]['indent'] === 0
485-
) {
486-
continue;
487-
}
488-
489-
$newIndent = ($param['commentLines'][$lineNum]['indent'] + $spaces - $param['type_space']);
490-
$phpcsFile->fixer->replaceToken(
491-
($param['commentLines'][$lineNum]['token'] - 1),
492-
str_repeat(' ', $newIndent)
493-
);
494-
}
495-
496-
$phpcsFile->fixer->endChangeset();
497-
}//end if
498-
}//end if
462+
$this->checkSpacingAfterParamType($phpcsFile, $param, $maxType);
499463

500464
// Make sure the param name is correct.
501465
if (isset($realParams[$pos]) === true) {
@@ -528,43 +492,7 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
528492
}
529493

530494
// Check number of spaces after the var name.
531-
$spaces = ($maxVar - strlen($param['var']) + 1);
532-
if ($param['var_space'] !== $spaces) {
533-
$error = 'Expected %s spaces after parameter name; %s found';
534-
$data = array(
535-
$spaces,
536-
$param['var_space'],
537-
);
538-
539-
$fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamName', $data);
540-
if ($fix === true) {
541-
$phpcsFile->fixer->beginChangeset();
542-
543-
$content = $param['type'];
544-
$content .= str_repeat(' ', $param['type_space']);
545-
$content .= $param['var'];
546-
$content .= str_repeat(' ', $spaces);
547-
$content .= $param['commentLines'][0]['comment'];
548-
$phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content);
549-
550-
// Fix up the indent of additional comment lines.
551-
foreach ($param['commentLines'] as $lineNum => $line) {
552-
if ($lineNum === 0
553-
|| $param['commentLines'][$lineNum]['indent'] === 0
554-
) {
555-
continue;
556-
}
557-
558-
$newIndent = ($param['commentLines'][$lineNum]['indent'] + $spaces - $param['var_space']);
559-
$phpcsFile->fixer->replaceToken(
560-
($param['commentLines'][$lineNum]['token'] - 1),
561-
str_repeat(' ', $newIndent)
562-
);
563-
}
564-
565-
$phpcsFile->fixer->endChangeset();
566-
}//end if
567-
}//end if
495+
$this->checkSpacingAfterParamName($phpcsFile, $param, $maxVar);
568496

569497
// Param comments must start with a capital letter and end with the full stop.
570498
if (preg_match('/^(\p{Ll}|\P{L})/u', $param['comment']) === 1) {
@@ -595,4 +523,112 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
595523
}//end processParams()
596524

597525

526+
/**
527+
* Check the spacing after the type of a parameter.
528+
*
529+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
530+
* @param array $param The parameter to be checked.
531+
* @param int $maxType The maxlength of the longest parameter type.
532+
* @param int $spacing The number of spaces to add after the type.
533+
*
534+
* @return void
535+
*/
536+
protected function checkSpacingAfterParamType(PHP_CodeSniffer_File $phpcsFile, $param, $maxType, $spacing = 1)
537+
{
538+
// Check number of spaces after the type.
539+
$spaces = ($maxType - strlen($param['type']) + $spacing);
540+
if ($param['type_space'] !== $spaces) {
541+
$error = 'Expected %s spaces after parameter type; %s found';
542+
$data = array(
543+
$spaces,
544+
$param['type_space'],
545+
);
546+
547+
$fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data);
548+
if ($fix === true) {
549+
$phpcsFile->fixer->beginChangeset();
550+
551+
$content = $param['type'];
552+
$content .= str_repeat(' ', $spaces);
553+
$content .= $param['var'];
554+
$content .= str_repeat(' ', $param['var_space']);
555+
$content .= $param['commentLines'][0]['comment'];
556+
$phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content);
557+
558+
// Fix up the indent of additional comment lines.
559+
foreach ($param['commentLines'] as $lineNum => $line) {
560+
if ($lineNum === 0
561+
|| $param['commentLines'][$lineNum]['indent'] === 0
562+
) {
563+
continue;
564+
}
565+
566+
$newIndent = ($param['commentLines'][$lineNum]['indent'] + $spaces - $param['type_space']);
567+
$phpcsFile->fixer->replaceToken(
568+
($param['commentLines'][$lineNum]['token'] - 1),
569+
str_repeat(' ', $newIndent)
570+
);
571+
}
572+
573+
$phpcsFile->fixer->endChangeset();
574+
}//end if
575+
}//end if
576+
577+
}//end checkSpacingAfterParamType()
578+
579+
580+
/**
581+
* Check the spacing after the name of a parameter.
582+
*
583+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
584+
* @param array $param The parameter to be checked.
585+
* @param int $maxVar The maxlength of the longest parameter name.
586+
* @param int $spacing The number of spaces to add after the type.
587+
*
588+
* @return void
589+
*/
590+
protected function checkSpacingAfterParamName(PHP_CodeSniffer_File $phpcsFile, $param, $maxVar, $spacing = 1)
591+
{
592+
// Check number of spaces after the var name.
593+
$spaces = ($maxVar - strlen($param['var']) + $spacing);
594+
if ($param['var_space'] !== $spaces) {
595+
$error = 'Expected %s spaces after parameter name; %s found';
596+
$data = array(
597+
$spaces,
598+
$param['var_space'],
599+
);
600+
601+
$fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamName', $data);
602+
if ($fix === true) {
603+
$phpcsFile->fixer->beginChangeset();
604+
605+
$content = $param['type'];
606+
$content .= str_repeat(' ', $param['type_space']);
607+
$content .= $param['var'];
608+
$content .= str_repeat(' ', $spaces);
609+
$content .= $param['commentLines'][0]['comment'];
610+
$phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content);
611+
612+
// Fix up the indent of additional comment lines.
613+
foreach ($param['commentLines'] as $lineNum => $line) {
614+
if ($lineNum === 0
615+
|| $param['commentLines'][$lineNum]['indent'] === 0
616+
) {
617+
continue;
618+
}
619+
620+
$newIndent = ($param['commentLines'][$lineNum]['indent'] + $spaces - $param['var_space']);
621+
$phpcsFile->fixer->replaceToken(
622+
($param['commentLines'][$lineNum]['token'] - 1),
623+
str_repeat(' ', $newIndent)
624+
);
625+
}
626+
627+
$phpcsFile->fixer->endChangeset();
628+
}//end if
629+
}//end if
630+
631+
}//end checkSpacingAfterParamName()
632+
633+
598634
}//end class

0 commit comments

Comments
 (0)