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

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