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

Source for file ObjectOperatorSpacingSniff.php

Documentation is available at ObjectOperatorSpacingSniff.php

  1. <?php
  2. /**
  3.  * Ensure there is no whitespace before/after an object operator.
  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\WhiteSpace;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ObjectOperatorSpacingSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * Allow newlines instead of spaces.
  20.      *
  21.      * @var boolean 
  22.      */
  23.     public $ignoreNewlines = false;
  24.  
  25.  
  26.     /**
  27.      * Returns an array of tokens this test wants to listen for.
  28.      *
  29.      * @return array 
  30.      */
  31.     public function register()
  32.     {
  33.         return array(
  34.                 T_OBJECT_OPERATOR,
  35.                 T_DOUBLE_COLON,
  36.                );
  37.  
  38.     }//end register()
  39.  
  40.  
  41.     /**
  42.      * Processes this test, when one of its tokens is encountered.
  43.      *
  44.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  45.      * @param int                         $stackPtr  The position of the current token
  46.      *                                                in the stack passed in $tokens.
  47.      *
  48.      * @return void 
  49.      */
  50.     public function process(File $phpcsFile$stackPtr)
  51.     {
  52.         $tokens $phpcsFile->getTokens();
  53.         if ($tokens[($stackPtr - 1)]['code'!== T_WHITESPACE{
  54.             $before = 0;
  55.         else {
  56.             if ($tokens[($stackPtr - 2)]['line'!== $tokens[$stackPtr]['line']{
  57.                 $before 'newline';
  58.             else {
  59.                 $before $tokens[($stackPtr - 1)]['length'];
  60.             }
  61.         }
  62.  
  63.         if ($tokens[($stackPtr + 1)]['code'!== T_WHITESPACE{
  64.             $after = 0;
  65.         else {
  66.             if ($tokens[($stackPtr + 2)]['line'!== $tokens[$stackPtr]['line']{
  67.                 $after 'newline';
  68.             else {
  69.                 $after $tokens[($stackPtr + 1)]['length'];
  70.             }
  71.         }
  72.  
  73.         $phpcsFile->recordMetric($stackPtr'Spacing before object operator'$before);
  74.         $phpcsFile->recordMetric($stackPtr'Spacing after object operator'$after);
  75.  
  76.         $this->checkSpacingBeforeOperator($phpcsFile$stackPtr$before);
  77.         $this->checkSpacingAfterOperator($phpcsFile$stackPtr$after);
  78.  
  79.     }//end process()
  80.  
  81.  
  82.     /**
  83.      * Check the spacing before the operator.
  84.      *
  85.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  86.      * @param int                         $stackPtr  The position of the current token
  87.      *                                                in the stack passed in $tokens.
  88.      * @param mixed                       $before    The number of spaces found before the
  89.      *                                                operator or the string 'newline'.
  90.      *
  91.      * @return boolean true if there was no error, false otherwise.
  92.      */
  93.     protected function checkSpacingBeforeOperator(File $phpcsFile$stackPtr$before)
  94.     {
  95.         if ($before !== 0
  96.             && ($before !== 'newline' || $this->ignoreNewlines === false)
  97.         {
  98.             $error 'Space found before object operator';
  99.             $fix   $phpcsFile->addFixableError($error$stackPtr'Before');
  100.             if ($fix === true{
  101.                 $phpcsFile->fixer->replaceToken(($stackPtr - 1)'');
  102.             }
  103.  
  104.             return false;
  105.         }
  106.  
  107.         return true;
  108.  
  109.     }//end checkSpacingBeforeOperator()
  110.  
  111.  
  112.     /**
  113.      * Check the spacing after the operator.
  114.      *
  115.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  116.      * @param int                         $stackPtr  The position of the current token
  117.      *                                                in the stack passed in $tokens.
  118.      * @param mixed                       $after     The number of spaces found after the
  119.      *                                                operator or the string 'newline'.
  120.      *
  121.      * @return boolean true if there was no error, false otherwise.
  122.      */
  123.     protected function checkSpacingAfterOperator(File $phpcsFile$stackPtr$after)
  124.     {
  125.         if ($after !== 0
  126.             && ($after !== 'newline' || $this->ignoreNewlines === false)
  127.         {
  128.             $error 'Space found after object operator';
  129.             $fix   $phpcsFile->addFixableError($error$stackPtr'After');
  130.             if ($fix === true{
  131.                 $phpcsFile->fixer->replaceToken(($stackPtr + 1)'');
  132.             }
  133.  
  134.             return false;
  135.         }
  136.  
  137.         return true;
  138.  
  139.     }//end checkSpacingAfterOperator()
  140.  
  141.  
  142. }//end class

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