PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file FunctionCommentSniff.php

Documentation is available at FunctionCommentSniff.php

  1. <?php
  2. /**
  3.  * Parses and verifies the doc comments for functions.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting;
  11.  
  12. use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff as PEARFunctionCommentSniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Config;
  15. use PHP_CodeSniffer\Util\Common;
  16.  
  17. class FunctionCommentSniff extends PEARFunctionCommentSniff
  18. {
  19.  
  20.     /**
  21.      * The current PHP version.
  22.      *
  23.      * @var integer 
  24.      */
  25.     private $phpVersion = null;
  26.  
  27.  
  28.     /**
  29.      * Process the return comment of this function comment.
  30.      *
  31.      * @param \PHP_CodeSniffer\Files\File $phpcsFile    The file being scanned.
  32.      * @param int                         $stackPtr     The position of the current token
  33.      *                                                   in the stack passed in $tokens.
  34.      * @param int                         $commentStart The position in the stack where the comment started.
  35.      *
  36.      * @return void 
  37.      */
  38.     protected function processReturn(File $phpcsFile$stackPtr$commentStart)
  39.     {
  40.         $tokens $phpcsFile->getTokens();
  41.  
  42.         // Skip constructor and destructor.
  43.         $methodName      $phpcsFile->getDeclarationName($stackPtr);
  44.         $isSpecialMethod ($methodName === '__construct' || $methodName === '__destruct');
  45.  
  46.         $return = null;
  47.         foreach ($tokens[$commentStart]['comment_tags'as $tag{
  48.             if ($tokens[$tag]['content'=== '@return'{
  49.                 if ($return !== null{
  50.                     $error 'Only 1 @return tag is allowed in a function comment';
  51.                     $phpcsFile->addError($error$tag'DuplicateReturn');
  52.                     return;
  53.                 }
  54.  
  55.                 $return $tag;
  56.             }
  57.         }
  58.  
  59.         if ($isSpecialMethod === true{
  60.             return;
  61.         }
  62.  
  63.         if ($return !== null{
  64.             $content $tokens[($return + 2)]['content'];
  65.             if (empty($content=== true || $tokens[($return + 2)]['code'!== T_DOC_COMMENT_STRING{
  66.                 $error 'Return type missing for @return tag in function comment';
  67.                 $phpcsFile->addError($error$return'MissingReturnType');
  68.             else {
  69.                 // Support both a return type and a description.
  70.                 $split preg_match('`^((?:\|?(?:array\([^\)]*\)|[\\\\a-z0-9\[\]]+))*)( .*)?`i'$content$returnParts);
  71.                 if (isset($returnParts[1]=== false{
  72.                     return;
  73.                 }
  74.  
  75.                 $returnType $returnParts[1];
  76.  
  77.                 // Check return type (can be multiple, separated by '|').
  78.                 $typeNames      explode('|'$returnType);
  79.                 $suggestedNames = array();
  80.                 foreach ($typeNames as $i => $typeName{
  81.                     $suggestedName = Common::suggestType($typeName);
  82.                     if (in_array($suggestedName$suggestedNames=== false{
  83.                         $suggestedNames[$suggestedName;
  84.                     }
  85.                 }
  86.  
  87.                 $suggestedType implode('|'$suggestedNames);
  88.                 if ($returnType !== $suggestedType{
  89.                     $error 'Expected "%s" but found "%s" for function return type';
  90.                     $data  = array(
  91.                               $suggestedType,
  92.                               $returnType,
  93.                              );
  94.                     $fix   $phpcsFile->addFixableError($error$return'InvalidReturn'$data);
  95.                     if ($fix === true{
  96.                         $replacement $suggestedType;
  97.                         if (empty($returnParts[2]=== false{
  98.                             $replacement .= $returnParts[2];
  99.                         }
  100.  
  101.                         $phpcsFile->fixer->replaceToken(($return + 2)$replacement);
  102.                         unset($replacement);
  103.                     }
  104.                 }
  105.  
  106.                 // If the return type is void, make sure there is
  107.                 // no return statement in the function.
  108.                 if ($returnType === 'void'{
  109.                     if (isset($tokens[$stackPtr]['scope_closer']=== true{
  110.                         $endToken $tokens[$stackPtr]['scope_closer'];
  111.                         for ($returnToken $stackPtr$returnToken $endToken$returnToken++{
  112.                             if ($tokens[$returnToken]['code'=== T_CLOSURE
  113.                                 || $tokens[$returnToken]['code'=== T_ANON_CLASS
  114.                             {
  115.                                 $returnToken $tokens[$returnToken]['scope_closer'];
  116.                                 continue;
  117.                             }
  118.  
  119.                             if ($tokens[$returnToken]['code'=== T_RETURN
  120.                                 || $tokens[$returnToken]['code'=== T_YIELD
  121.                                 || $tokens[$returnToken]['code'=== T_YIELD_FROM
  122.                             {
  123.                                 break;
  124.                             }
  125.                         }
  126.  
  127.                         if ($returnToken !== $endToken{
  128.                             // If the function is not returning anything, just
  129.                             // exiting, then there is no problem.
  130.                             $semicolon $phpcsFile->findNext(T_WHITESPACE($returnToken + 1)nulltrue);
  131.                             if ($tokens[$semicolon]['code'!== T_SEMICOLON{
  132.                                 $error 'Function return type is void, but function contains return statement';
  133.                                 $phpcsFile->addError($error$return'InvalidReturnVoid');
  134.                             }
  135.                         }
  136.                     }//end if
  137.                 else if ($returnType !== 'mixed' && in_array('void'$typeNamestrue=== false{
  138.                     // If return type is not void, there needs to be a return statement
  139.                     // somewhere in the function that returns something.
  140.                     if (isset($tokens[$stackPtr]['scope_closer']=== true{
  141.                         $endToken    $tokens[$stackPtr]['scope_closer'];
  142.                         $returnToken $phpcsFile->findNext(array(T_RETURNT_YIELDT_YIELD_FROM)$stackPtr$endToken);
  143.                         if ($returnToken === false{
  144.                             $error 'Function return type is not void, but function has no return statement';
  145.                             $phpcsFile->addError($error$return'InvalidNoReturn');
  146.                         else {
  147.                             $semicolon $phpcsFile->findNext(T_WHITESPACE($returnToken + 1)nulltrue);
  148.                             if ($tokens[$semicolon]['code'=== T_SEMICOLON{
  149.                                 $error 'Function return type is not void, but function is returning void here';
  150.                                 $phpcsFile->addError($error$returnToken'InvalidReturnNotVoid');
  151.                             }
  152.                         }
  153.                     }
  154.                 }//end if
  155.             }//end if
  156.         else {
  157.             $error 'Missing @return tag in function comment';
  158.             $phpcsFile->addError($error$tokens[$commentStart]['comment_closer']'MissingReturn');
  159.         }//end if
  160.  
  161.     }//end processReturn()
  162.  
  163.  
  164.     /**
  165.      * Process any throw tags that this function comment has.
  166.      *
  167.      * @param \PHP_CodeSniffer\Files\File $phpcsFile    The file being scanned.
  168.      * @param int                         $stackPtr     The position of the current token
  169.      *                                                   in the stack passed in $tokens.
  170.      * @param int                         $commentStart The position in the stack where the comment started.
  171.      *
  172.      * @return void 
  173.      */
  174.     protected function processThrows(File $phpcsFile$stackPtr$commentStart)
  175.     {
  176.         $tokens $phpcsFile->getTokens();
  177.  
  178.         $throws = array();
  179.         foreach ($tokens[$commentStart]['comment_tags'as $pos => $tag{
  180.             if ($tokens[$tag]['content'!== '@throws'{
  181.                 continue;
  182.             }
  183.  
  184.             $exception = null;
  185.             $comment   = null;
  186.             if ($tokens[($tag + 2)]['code'=== T_DOC_COMMENT_STRING{
  187.                 $matches = array();
  188.                 preg_match('/([^\s]+)(?:\s+(.*))?/'$tokens[($tag + 2)]['content']$matches);
  189.                 $exception $matches[1];
  190.                 if (isset($matches[2]=== true && trim($matches[2]!== ''{
  191.                     $comment $matches[2];
  192.                 }
  193.             }
  194.  
  195.             if ($exception === null{
  196.                 $error 'Exception type and comment missing for @throws tag in function comment';
  197.                 $phpcsFile->addError($error$tag'InvalidThrows');
  198.             else if ($comment === null{
  199.                 $error 'Comment missing for @throws tag in function comment';
  200.                 $phpcsFile->addError($error$tag'EmptyThrows');
  201.             else {
  202.                 // Any strings until the next tag belong to this comment.
  203.                 if (isset($tokens[$commentStart]['comment_tags'][($pos + 1)]=== true{
  204.                     $end $tokens[$commentStart]['comment_tags'][($pos + 1)];
  205.                 else {
  206.                     $end $tokens[$commentStart]['comment_closer'];
  207.                 }
  208.  
  209.                 for ($i ($tag + 3)$i $end$i++{
  210.                     if ($tokens[$i]['code'=== T_DOC_COMMENT_STRING{
  211.                         $comment .= ' '.$tokens[$i]['content'];
  212.                     }
  213.                 }
  214.  
  215.                 // Starts with a capital letter and ends with a fullstop.
  216.                 $firstChar $comment{0};
  217.                 if (strtoupper($firstChar!== $firstChar{
  218.                     $error '@throws tag comment must start with a capital letter';
  219.                     $phpcsFile->addError($error($tag + 2)'ThrowsNotCapital');
  220.                 }
  221.  
  222.                 $lastChar substr($comment-1);
  223.                 if ($lastChar !== '.'{
  224.                     $error '@throws tag comment must end with a full stop';
  225.                     $phpcsFile->addError($error($tag + 2)'ThrowsNoFullStop');
  226.                 }
  227.             }//end if
  228.         }//end foreach
  229.  
  230.     }//end processThrows()
  231.  
  232.  
  233.     /**
  234.      * Process the function parameter comments.
  235.      *
  236.      * @param \PHP_CodeSniffer\Files\File $phpcsFile    The file being scanned.
  237.      * @param int                         $stackPtr     The position of the current token
  238.      *                                                   in the stack passed in $tokens.
  239.      * @param int                         $commentStart The position in the stack where the comment started.
  240.      *
  241.      * @return void 
  242.      */
  243.     protected function processParams(File $phpcsFile$stackPtr$commentStart)
  244.     {
  245.         if ($this->phpVersion === null{
  246.             $this->phpVersion = Config::getConfigData('php_version');
  247.             if ($this->phpVersion === null{
  248.                 $this->phpVersion = PHP_VERSION_ID;
  249.             }
  250.         }
  251.  
  252.         $tokens $phpcsFile->getTokens();
  253.  
  254.         $params  = array();
  255.         $maxType = 0;
  256.         $maxVar  = 0;
  257.         foreach ($tokens[$commentStart]['comment_tags'as $pos => $tag{
  258.             if ($tokens[$tag]['content'!== '@param'{
  259.                 continue;
  260.             }
  261.  
  262.             $type         '';
  263.             $typeSpace    = 0;
  264.             $var          '';
  265.             $varSpace     = 0;
  266.             $comment      '';
  267.             $commentLines = array();
  268.             if ($tokens[($tag + 2)]['code'=== T_DOC_COMMENT_STRING{
  269.                 $matches = array();
  270.                 preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/'$tokens[($tag + 2)]['content']$matches);
  271.  
  272.                 if (empty($matches=== false{
  273.                     $typeLen   strlen($matches[1]);
  274.                     $type      trim($matches[1]);
  275.                     $typeSpace ($typeLen strlen($type));
  276.                     $typeLen   strlen($type);
  277.                     if ($typeLen $maxType{
  278.                         $maxType $typeLen;
  279.                     }
  280.                 }
  281.  
  282.                 if (isset($matches[2]=== true{
  283.                     $var    $matches[2];
  284.                     $varLen strlen($var);
  285.                     if ($varLen $maxVar{
  286.                         $maxVar $varLen;
  287.                     }
  288.  
  289.                     if (isset($matches[4]=== true{
  290.                         $varSpace       strlen($matches[3]);
  291.                         $comment        $matches[4];
  292.                         $commentLines[= array(
  293.                                            'comment' => $comment,
  294.                                            'token'   => ($tag + 2),
  295.                                            'indent'  => $varSpace,
  296.                                           );
  297.  
  298.                         // Any strings until the next tag belong to this comment.
  299.                         if (isset($tokens[$commentStart]['comment_tags'][($pos + 1)]=== true{
  300.                             $end $tokens[$commentStart]['comment_tags'][($pos + 1)];
  301.                         else {
  302.                             $end $tokens[$commentStart]['comment_closer'];
  303.                         }
  304.  
  305.                         for ($i ($tag + 3)$i $end$i++{
  306.                             if ($tokens[$i]['code'=== T_DOC_COMMENT_STRING{
  307.                                 $indent = 0;
  308.                                 if ($tokens[($i - 1)]['code'=== T_DOC_COMMENT_WHITESPACE{
  309.                                     $indent strlen($tokens[($i - 1)]['content']);
  310.                                 }
  311.  
  312.                                 $comment       .= ' '.$tokens[$i]['content'];
  313.                                 $commentLines[= array(
  314.                                                    'comment' => $tokens[$i]['content'],
  315.                                                    'token'   => $i,
  316.                                                    'indent'  => $indent,
  317.                                                   );
  318.                             }
  319.                         }
  320.                     else {
  321.                         $error 'Missing parameter comment';
  322.                         $phpcsFile->addError($error$tag'MissingParamComment');
  323.                         $commentLines[= array('comment' => '');
  324.                     }//end if
  325.                 else {
  326.                     $error 'Missing parameter name';
  327.                     $phpcsFile->addError($error$tag'MissingParamName');
  328.                 }//end if
  329.             else {
  330.                 $error 'Missing parameter type';
  331.                 $phpcsFile->addError($error$tag'MissingParamType');
  332.             }//end if
  333.  
  334.             $params[= array(
  335.                          'tag'          => $tag,
  336.                          'type'         => $type,
  337.                          'var'          => $var,
  338.                          'comment'      => $comment,
  339.                          'commentLines' => $commentLines,
  340.                          'type_space'   => $typeSpace,
  341.                          'var_space'    => $varSpace,
  342.                         );
  343.         }//end foreach
  344.  
  345.         $realParams  $phpcsFile->getMethodParameters($stackPtr);
  346.         $foundParams = array();
  347.  
  348.         // We want to use ... for all variable length arguments, so added
  349.         // this prefix to the variable name so comparisons are easier.
  350.         foreach ($realParams as $pos => $param{
  351.             if ($param['variable_length'=== true{
  352.                 $realParams[$pos]['name''...'.$realParams[$pos]['name'];
  353.             }
  354.         }
  355.  
  356.         foreach ($params as $pos => $param{
  357.             // If the type is empty, the whole line is empty.
  358.             if ($param['type'=== ''{
  359.                 continue;
  360.             }
  361.  
  362.             // Check the param type value.
  363.             $typeNames          explode('|'$param['type']);
  364.             $suggestedTypeNames = array();
  365.  
  366.             foreach ($typeNames as $typeName{
  367.                 $suggestedName        = Common::suggestType($typeName);
  368.                 $suggestedTypeNames[$suggestedName;
  369.  
  370.                 if (count($typeNames> 1{
  371.                     continue;
  372.                 }
  373.  
  374.                 // Check type hint for array and custom type.
  375.                 $suggestedTypeHint '';
  376.                 if (strpos($suggestedName'array'!== false || substr($suggestedName-2=== '[]'{
  377.                     $suggestedTypeHint 'array';
  378.                 else if (strpos($suggestedName'callable'!== false{
  379.                     $suggestedTypeHint 'callable';
  380.                 else if (strpos($suggestedName'callback'!== false{
  381.                     $suggestedTypeHint 'callable';
  382.                 else if (in_array($suggestedNameCommon::$allowedTypes=== false{
  383.                     $suggestedTypeHint $suggestedName;
  384.                 }
  385.  
  386.                 if ($this->phpVersion >= 70000{
  387.                     if ($suggestedName === 'string'{
  388.                         $suggestedTypeHint 'string';
  389.                     else if ($suggestedName === 'int' || $suggestedName === 'integer'{
  390.                         $suggestedTypeHint 'int';
  391.                     else if ($suggestedName === 'float'{
  392.                         $suggestedTypeHint 'float';
  393.                     else if ($suggestedName === 'bool' || $suggestedName === 'boolean'{
  394.                         $suggestedTypeHint 'bool';
  395.                     }
  396.                 }
  397.  
  398.                 if ($suggestedTypeHint !== '' && isset($realParams[$pos]=== true{
  399.                     $typeHint $realParams[$pos]['type_hint'];
  400.                     if ($typeHint === ''{
  401.                         $error 'Type hint "%s" missing for %s';
  402.                         $data  = array(
  403.                                   $suggestedTypeHint,
  404.                                   $param['var'],
  405.                                  );
  406.  
  407.                         $errorCode 'TypeHintMissing';
  408.                         if ($suggestedTypeHint === 'string'
  409.                             || $suggestedTypeHint === 'int'
  410.                             || $suggestedTypeHint === 'float'
  411.                             || $suggestedTypeHint === 'bool'
  412.                         {
  413.                             $errorCode 'Scalar'.$errorCode;
  414.                         }
  415.  
  416.                         $phpcsFile->addError($error$stackPtr$errorCode$data);
  417.                     else if ($typeHint !== substr($suggestedTypeHint(strlen($typeHint* -1))) {
  418.                         $error 'Expected type hint "%s"; found "%s" for %s';
  419.                         $data  = array(
  420.                                   $suggestedTypeHint,
  421.                                   $typeHint,
  422.                                   $param['var'],
  423.                                  );
  424.                         $phpcsFile->addError($error$stackPtr'IncorrectTypeHint'$data);
  425.                     }//end if
  426.                 else if ($suggestedTypeHint === '' && isset($realParams[$pos]=== true{
  427.                     $typeHint $realParams[$pos]['type_hint'];
  428.                     if ($typeHint !== ''{
  429.                         $error 'Unknown type hint "%s" found for %s';
  430.                         $data  = array(
  431.                                   $typeHint,
  432.                                   $param['var'],
  433.                                  );
  434.                         $phpcsFile->addError($error$stackPtr'InvalidTypeHint'$data);
  435.                     }
  436.                 }//end if
  437.             }//end foreach
  438.  
  439.             $suggestedType implode($suggestedTypeNames'|');
  440.             if ($param['type'!== $suggestedType{
  441.                 $error 'Expected "%s" but found "%s" for parameter type';
  442.                 $data  = array(
  443.                           $suggestedType,
  444.                           $param['type'],
  445.                          );
  446.  
  447.                 $fix $phpcsFile->addFixableError($error$param['tag']'IncorrectParamVarName'$data);
  448.                 if ($fix === true{
  449.                     $phpcsFile->fixer->beginChangeset();
  450.  
  451.                     $content  $suggestedType;
  452.                     $content .= str_repeat(' '$param['type_space']);
  453.                     $content .= $param['var'];
  454.                     $content .= str_repeat(' '$param['var_space']);
  455.                     if (isset($param['commentLines'][0]=== true{
  456.                         $content .= $param['commentLines'][0]['comment'];
  457.                     }
  458.  
  459.                     $phpcsFile->fixer->replaceToken(($param['tag'+ 2)$content);
  460.  
  461.                     // Fix up the indent of additional comment lines.
  462.                     foreach ($param['commentLines'as $lineNum => $line{
  463.                         if ($lineNum === 0
  464.                             || $param['commentLines'][$lineNum]['indent'=== 0
  465.                         {
  466.                             continue;
  467.                         }
  468.  
  469.                         $diff      (strlen($param['type']strlen($suggestedType));
  470.                         $newIndent ($param['commentLines'][$lineNum]['indent'$diff);
  471.                         $phpcsFile->fixer->replaceToken(
  472.                             ($param['commentLines'][$lineNum]['token'- 1),
  473.                             str_repeat(' '$newIndent)
  474.                         );
  475.                     }
  476.  
  477.                     $phpcsFile->fixer->endChangeset();
  478.                 }//end if
  479.             }//end if
  480.  
  481.             if ($param['var'=== ''{
  482.                 continue;
  483.             }
  484.  
  485.             $foundParams[$param['var'];
  486.  
  487.             // Check number of spaces after the type.
  488.             $this->checkSpacingAfterParamType($phpcsFile$param$maxType);
  489.  
  490.             // Make sure the param name is correct.
  491.             if (isset($realParams[$pos]=== true{
  492.                 $realName $realParams[$pos]['name'];
  493.                 if ($realName !== $param['var']{
  494.                     $code 'ParamNameNoMatch';
  495.                     $data = array(
  496.                              $param['var'],
  497.                              $realName,
  498.                             );
  499.  
  500.                     $error 'Doc comment for parameter %s does not match ';
  501.                     if (strtolower($param['var']=== strtolower($realName)) {
  502.                         $error .= 'case of ';
  503.                         $code   'ParamNameNoCaseMatch';
  504.                     }
  505.  
  506.                     $error .= 'actual variable name %s';
  507.  
  508.                     $phpcsFile->addError($error$param['tag']$code$data);
  509.                 }
  510.             else if (substr($param['var']-4!== ',...'{
  511.                 // We must have an extra parameter comment.
  512.                 $error 'Superfluous parameter comment';
  513.                 $phpcsFile->addError($error$param['tag']'ExtraParamComment');
  514.             }//end if
  515.  
  516.             if ($param['comment'=== ''{
  517.                 continue;
  518.             }
  519.  
  520.             // Check number of spaces after the var name.
  521.             $this->checkSpacingAfterParamName($phpcsFile$param$maxVar);
  522.  
  523.             // Param comments must start with a capital letter and end with the full stop.
  524.             if (preg_match('/^(\p{Ll}|\P{L})/u'$param['comment']=== 1{
  525.                 $error 'Parameter comment must start with a capital letter';
  526.                 $phpcsFile->addError($error$param['tag']'ParamCommentNotCapital');
  527.             }
  528.  
  529.             $lastChar substr($param['comment']-1);
  530.             if ($lastChar !== '.'{
  531.                 $error 'Parameter comment must end with a full stop';
  532.                 $phpcsFile->addError($error$param['tag']'ParamCommentFullStop');
  533.             }
  534.         }//end foreach
  535.  
  536.         $realNames = array();
  537.         foreach ($realParams as $realParam{
  538.             $realNames[$realParam['name'];
  539.         }
  540.  
  541.         // Report missing comments.
  542.         $diff array_diff($realNames$foundParams);
  543.         foreach ($diff as $neededParam{
  544.             $error 'Doc comment for parameter "%s" missing';
  545.             $data  = array($neededParam);
  546.             $phpcsFile->addError($error$commentStart'MissingParamTag'$data);
  547.         }
  548.  
  549.     }//end processParams()
  550.  
  551.  
  552.     /**
  553.      * Check the spacing after the type of a parameter.
  554.      *
  555.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  556.      * @param array                       $param     The parameter to be checked.
  557.      * @param int                         $maxType   The maxlength of the longest parameter type.
  558.      * @param int                         $spacing   The number of spaces to add after the type.
  559.      *
  560.      * @return void 
  561.      */
  562.     protected function checkSpacingAfterParamType(File $phpcsFile$param$maxType$spacing=1)
  563.     {
  564.         // Check number of spaces after the type.
  565.         $spaces ($maxType strlen($param['type']$spacing);
  566.         if ($param['type_space'!== $spaces{
  567.             $error 'Expected %s spaces after parameter type; %s found';
  568.             $data  = array(
  569.                       $spaces,
  570.                       $param['type_space'],
  571.                      );
  572.  
  573.             $fix $phpcsFile->addFixableError($error$param['tag']'SpacingAfterParamType'$data);
  574.             if ($fix === true{
  575.                 $phpcsFile->fixer->beginChangeset();
  576.  
  577.                 $content  $param['type'];
  578.                 $content .= str_repeat(' '$spaces);
  579.                 $content .= $param['var'];
  580.                 $content .= str_repeat(' '$param['var_space']);
  581.                 $content .= $param['commentLines'][0]['comment'];
  582.                 $phpcsFile->fixer->replaceToken(($param['tag'+ 2)$content);
  583.  
  584.                 // Fix up the indent of additional comment lines.
  585.                 foreach ($param['commentLines'as $lineNum => $line{
  586.                     if ($lineNum === 0
  587.                         || $param['commentLines'][$lineNum]['indent'=== 0
  588.                     {
  589.                         continue;
  590.                     }
  591.  
  592.                     $diff      ($param['type_space'$spaces);
  593.                     $newIndent ($param['commentLines'][$lineNum]['indent'$diff);
  594.                     $phpcsFile->fixer->replaceToken(
  595.                         ($param['commentLines'][$lineNum]['token'- 1),
  596.                         str_repeat(' '$newIndent)
  597.                     );
  598.                 }
  599.  
  600.                 $phpcsFile->fixer->endChangeset();
  601.             }//end if
  602.         }//end if
  603.  
  604.     }//end checkSpacingAfterParamType()
  605.  
  606.  
  607.     /**
  608.      * Check the spacing after the name of a parameter.
  609.      *
  610.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  611.      * @param array                       $param     The parameter to be checked.
  612.      * @param int                         $maxVar    The maxlength of the longest parameter name.
  613.      * @param int                         $spacing   The number of spaces to add after the type.
  614.      *
  615.      * @return void 
  616.      */
  617.     protected function checkSpacingAfterParamName(File $phpcsFile$param$maxVar$spacing=1)
  618.     {
  619.         // Check number of spaces after the var name.
  620.         $spaces ($maxVar strlen($param['var']$spacing);
  621.         if ($param['var_space'!== $spaces{
  622.             $error 'Expected %s spaces after parameter name; %s found';
  623.             $data  = array(
  624.                       $spaces,
  625.                       $param['var_space'],
  626.                      );
  627.  
  628.             $fix $phpcsFile->addFixableError($error$param['tag']'SpacingAfterParamName'$data);
  629.             if ($fix === true{
  630.                 $phpcsFile->fixer->beginChangeset();
  631.  
  632.                 $content  $param['type'];
  633.                 $content .= str_repeat(' '$param['type_space']);
  634.                 $content .= $param['var'];
  635.                 $content .= str_repeat(' '$spaces);
  636.                 $content .= $param['commentLines'][0]['comment'];
  637.                 $phpcsFile->fixer->replaceToken(($param['tag'+ 2)$content);
  638.  
  639.                 // Fix up the indent of additional comment lines.
  640.                 foreach ($param['commentLines'as $lineNum => $line{
  641.                     if ($lineNum === 0
  642.                         || $param['commentLines'][$lineNum]['indent'=== 0
  643.                     {
  644.                         continue;
  645.                     }
  646.  
  647.                     $diff      ($param['var_space'$spaces);
  648.                     $newIndent ($param['commentLines'][$lineNum]['indent'$diff);
  649.                     $phpcsFile->fixer->replaceToken(
  650.                         ($param['commentLines'][$lineNum]['token'- 1),
  651.                         str_repeat(' '$newIndent)
  652.                     );
  653.                 }
  654.  
  655.                 $phpcsFile->fixer->endChangeset();
  656.             }//end if
  657.         }//end if
  658.  
  659.     }//end checkSpacingAfterParamName()
  660.  
  661.  
  662. }//end class

Documentation generated on Mon, 11 Mar 2019 14:17:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.