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

Source for file LineLengthSniff.php

Documentation is available at LineLengthSniff.php

  1. <?php
  2. /**
  3.  * Checks the length of all lines in a file.
  4.  *
  5.  * Checks all lines in the file, and throws warnings if they are over 80
  6.  * characters in length and errors if they are over 100. Both these
  7.  * figures can be changed in a ruleset.xml file.
  8.  *
  9.  * @author    Greg Sherwood <gsherwood@squiz.net>
  10.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  11.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12.  */
  13.  
  14. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Files;
  15.  
  16. use PHP_CodeSniffer\Sniffs\Sniff;
  17. use PHP_CodeSniffer\Files\File;
  18.  
  19. class LineLengthSniff implements Sniff
  20. {
  21.  
  22.     /**
  23.      * The limit that the length of a line should not exceed.
  24.      *
  25.      * @var integer 
  26.      */
  27.     public $lineLimit = 80;
  28.  
  29.     /**
  30.      * The limit that the length of a line must not exceed.
  31.      *
  32.      * Set to zero (0) to disable.
  33.      *
  34.      * @var integer 
  35.      */
  36.     public $absoluteLineLimit = 100;
  37.  
  38.     /**
  39.      * Whether or not to ignore comment lines.
  40.      *
  41.      * @var boolean 
  42.      */
  43.     public $ignoreComments = false;
  44.  
  45.  
  46.     /**
  47.      * Returns an array of tokens this test wants to listen for.
  48.      *
  49.      * @return array 
  50.      */
  51.     public function register()
  52.     {
  53.         return array(T_OPEN_TAG);
  54.  
  55.     }//end register()
  56.  
  57.  
  58.     /**
  59.      * Processes this test, when one of its tokens is encountered.
  60.      *
  61.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  62.      * @param int                         $stackPtr  The position of the current token in
  63.      *                                                the stack passed in $tokens.
  64.      *
  65.      * @return int 
  66.      */
  67.     public function process(File $phpcsFile$stackPtr)
  68.     {
  69.         $tokens $phpcsFile->getTokens();
  70.         for ($i = 1; $i $phpcsFile->numTokens; $i++{
  71.             if ($tokens[$i]['column'=== 1{
  72.                 $this->checkLineLength($phpcsFile$tokens$i);
  73.             }
  74.         }
  75.  
  76.         $this->checkLineLength($phpcsFile$tokens$i);
  77.  
  78.         // Ignore the rest of the file.
  79.         return ($phpcsFile->numTokens + 1);
  80.  
  81.     }//end process()
  82.  
  83.  
  84.     /**
  85.      * Checks if a line is too long.
  86.      *
  87.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  88.      * @param array                       $tokens    The token stack.
  89.      * @param int                         $stackPtr  The first token on the next line.
  90.      *
  91.      * @return null|false
  92.      */
  93.     protected function checkLineLength($phpcsFile$tokens$stackPtr)
  94.     {
  95.         // The passed token is the first on the line.
  96.         $stackPtr--;
  97.  
  98.         if ($tokens[$stackPtr]['column'=== 1
  99.             && $tokens[$stackPtr]['length'=== 0
  100.         {
  101.             // Blank line.
  102.             return;
  103.         }
  104.  
  105.         if ($tokens[$stackPtr]['column'!== 1
  106.             && $tokens[$stackPtr]['content'=== $phpcsFile->eolChar
  107.         {
  108.             $stackPtr--;
  109.         }
  110.  
  111.         $lineLength ($tokens[$stackPtr]['column'$tokens[$stackPtr]['length'- 1);
  112.  
  113.         // Record metrics for common line length groupings.
  114.         if ($lineLength <= 80{
  115.             $phpcsFile->recordMetric($stackPtr'Line length''80 or less');
  116.         else if ($lineLength <= 120{
  117.             $phpcsFile->recordMetric($stackPtr'Line length''81-120');
  118.         else if ($lineLength <= 150{
  119.             $phpcsFile->recordMetric($stackPtr'Line length''121-150');
  120.         else {
  121.             $phpcsFile->recordMetric($stackPtr'Line length''151 or more');
  122.         }
  123.  
  124.         if ($tokens[$stackPtr]['code'=== T_COMMENT
  125.             || $tokens[$stackPtr]['code'=== T_DOC_COMMENT_STRING
  126.         {
  127.             if ($this->ignoreComments === true{
  128.                 return;
  129.             }
  130.  
  131.             // If this is a long comment, check if it can be broken up onto multiple lines.
  132.             // Some comments contain unbreakable strings like URLs and so it makes sense
  133.             // to ignore the line length in these cases if the URL would be longer than the max
  134.             // line length once you indent it to the correct level.
  135.             if ($lineLength $this->lineLimit{
  136.                 $oldLength strlen($tokens[$stackPtr]['content']);
  137.                 $newLength strlen(ltrim($tokens[$stackPtr]['content']"/#\t "));
  138.                 $indent    (($tokens[$stackPtr]['column'- 1($oldLength $newLength));
  139.  
  140.                 $nonBreakingLength $tokens[$stackPtr]['length'];
  141.  
  142.                 $space strrpos($tokens[$stackPtr]['content']' ');
  143.                 if ($space !== false{
  144.                     $nonBreakingLength -= ($space + 1);
  145.                 }
  146.  
  147.                 if (($nonBreakingLength $indent$this->lineLimit{
  148.                     return;
  149.                 }
  150.             }
  151.         }//end if
  152.  
  153.         if ($this->absoluteLineLimit > 0
  154.             && $lineLength $this->absoluteLineLimit
  155.         {
  156.             $data = array(
  157.                      $this->absoluteLineLimit,
  158.                      $lineLength,
  159.                     );
  160.  
  161.             $error 'Line exceeds maximum limit of %s characters; contains %s characters';
  162.             $phpcsFile->addError($error$stackPtr'MaxExceeded'$data);
  163.         else if ($lineLength $this->lineLimit{
  164.             $data = array(
  165.                      $this->lineLimit,
  166.                      $lineLength,
  167.                     );
  168.  
  169.             $warning 'Line exceeds %s characters; contains %s characters';
  170.             $phpcsFile->addWarning($warning$stackPtr'TooLong'$data);
  171.         }
  172.  
  173.     }//end checkLineLength()
  174.  
  175.  
  176. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.