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

Source for file PropertyDeclarationSniff.php

Documentation is available at PropertyDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Verifies that properties are declared correctly.
  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\PSR2\Sniffs\Classes;
  11.  
  12. use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
  13. use PHP_CodeSniffer\Util\Tokens;
  14. use PHP_CodeSniffer\Files\File;
  15.  
  16. class PropertyDeclarationSniff extends AbstractVariableSniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Processes the function tokens within the class.
  22.      *
  23.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  24.      * @param int                         $stackPtr  The position where the token was found.
  25.      *
  26.      * @return void 
  27.      */
  28.     protected function processMemberVar(File $phpcsFile$stackPtr)
  29.     {
  30.         $tokens $phpcsFile->getTokens();
  31.  
  32.         if ($tokens[$stackPtr]['content'][1=== '_'{
  33.             $error 'Property name "%s" should not be prefixed with an underscore to indicate visibility';
  34.             $data  = array($tokens[$stackPtr]['content']);
  35.             $phpcsFile->addWarning($error$stackPtr'Underscore'$data);
  36.         }
  37.  
  38.         // Detect multiple properties defined at the same time. Throw an error
  39.         // for this, but also only process the first property in the list so we don't
  40.         // repeat errors.
  41.         $find = Tokens::$scopeModifiers;
  42.         $find = array_merge($findarray(T_VARIABLET_VART_SEMICOLON));
  43.         $prev $phpcsFile->findPrevious($find($stackPtr - 1));
  44.         if ($tokens[$prev]['code'=== T_VARIABLE{
  45.             return;
  46.         }
  47.  
  48.         if ($tokens[$prev]['code'=== T_VAR{
  49.             $error 'The var keyword must not be used to declare a property';
  50.             $phpcsFile->addError($error$stackPtr'VarUsed');
  51.         }
  52.  
  53.         $next $phpcsFile->findNext(array(T_VARIABLET_SEMICOLON)($stackPtr + 1));
  54.         if ($tokens[$next]['code'=== T_VARIABLE{
  55.             $error 'There must not be more than one property declared per statement';
  56.             $phpcsFile->addError($error$stackPtr'Multiple');
  57.         }
  58.  
  59.         $modifier $phpcsFile->findPrevious(Tokens::$scopeModifiers$stackPtr);
  60.         if (($modifier === false|| ($tokens[$modifier]['line'!== $tokens[$stackPtr]['line'])) {
  61.             $error 'Visibility must be declared on property "%s"';
  62.             $data  = array($tokens[$stackPtr]['content']);
  63.             $phpcsFile->addError($error$stackPtr'ScopeMissing'$data);
  64.         }
  65.  
  66.     }//end processMemberVar()
  67.  
  68.  
  69.     /**
  70.      * Processes normal variables.
  71.      *
  72.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  73.      * @param int                         $stackPtr  The position where the token was found.
  74.      *
  75.      * @return void 
  76.      */
  77.     protected function processVariable(File $phpcsFile$stackPtr)
  78.     {
  79.         /*
  80.             We don't care about normal variables.
  81.         */
  82.  
  83.     }//end processVariable()
  84.  
  85.  
  86.     /**
  87.      * Processes variables in double quoted strings.
  88.      *
  89.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  90.      * @param int                         $stackPtr  The position where the token was found.
  91.      *
  92.      * @return void 
  93.      */
  94.     protected function processVariableInString(File $phpcsFile$stackPtr)
  95.     {
  96.         /*
  97.             We don't care about normal variables.
  98.         */
  99.  
  100.     }//end processVariableInString()
  101.  
  102.  
  103. }//end class

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