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

Source for file ValidVariableNameSniff.php

Documentation is available at ValidVariableNameSniff.php

  1. <?php
  2. /**
  3.  * Checks the naming of member variables.
  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\PEAR\Sniffs\NamingConventions;
  11.  
  12. use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ValidVariableNameSniff extends AbstractVariableSniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Processes class member variables.
  21.      *
  22.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  23.      * @param int                         $stackPtr  The position of the current token
  24.      *                                                in the stack passed in $tokens.
  25.      *
  26.      * @return void 
  27.      */
  28.     protected function processMemberVar(File $phpcsFile$stackPtr)
  29.     {
  30.         $tokens $phpcsFile->getTokens();
  31.  
  32.         $memberProps $phpcsFile->getMemberProperties($stackPtr);
  33.         if (empty($memberProps=== true{
  34.             return;
  35.         }
  36.  
  37.         $memberName     ltrim($tokens[$stackPtr]['content']'$');
  38.         $scope          $memberProps['scope'];
  39.         $scopeSpecified $memberProps['scope_specified'];
  40.  
  41.         if ($memberProps['scope'=== 'private'{
  42.             $isPublic = false;
  43.         else {
  44.             $isPublic = true;
  45.         }
  46.  
  47.         // If it's a private member, it must have an underscore on the front.
  48.         if ($isPublic === false && $memberName{0!== '_'{
  49.             $error 'Private member variable "%s" must be prefixed with an underscore';
  50.             $data  = array($memberName);
  51.             $phpcsFile->addError($error$stackPtr'PrivateNoUnderscore'$data);
  52.             return;
  53.         }
  54.  
  55.         // If it's not a private member, it must not have an underscore on the front.
  56.         if ($isPublic === true && $scopeSpecified === true && $memberName{0=== '_'{
  57.             $error '%s member variable "%s" must not be prefixed with an underscore';
  58.             $data  = array(
  59.                       ucfirst($scope),
  60.                       $memberName,
  61.                      );
  62.             $phpcsFile->addError($error$stackPtr'PublicUnderscore'$data);
  63.             return;
  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:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.