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 variables and 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\Squiz\Sniffs\NamingConventions;
  11.  
  12. use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
  13. use PHP_CodeSniffer\Util\Common;
  14. use PHP_CodeSniffer\Files\File;
  15.  
  16. class ValidVariableNameSniff extends AbstractVariableSniff
  17. {
  18.  
  19.     /**
  20.      * Tokens to ignore so that we can find a DOUBLE_COLON.
  21.      *
  22.      * @var array 
  23.      */
  24.     private $ignore = array(
  25.                        T_WHITESPACE,
  26.                        T_COMMENT,
  27.                       );
  28.  
  29.  
  30.     /**
  31.      * Processes this test, when one of its tokens is encountered.
  32.      *
  33.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  34.      * @param int                         $stackPtr  The position of the current token in the
  35.      *                                                stack passed in $tokens.
  36.      *
  37.      * @return void 
  38.      */
  39.     protected function processVariable(File $phpcsFile$stackPtr)
  40.     {
  41.         $tokens  $phpcsFile->getTokens();
  42.         $varName ltrim($tokens[$stackPtr]['content']'$');
  43.  
  44.         $phpReservedVars = array(
  45.                             '_SERVER',
  46.                             '_GET',
  47.                             '_POST',
  48.                             '_REQUEST',
  49.                             '_SESSION',
  50.                             '_ENV',
  51.                             '_COOKIE',
  52.                             '_FILES',
  53.                             'GLOBALS',
  54.                             'http_response_header',
  55.                             'HTTP_RAW_POST_DATA',
  56.                             'php_errormsg',
  57.                            );
  58.  
  59.         // If it's a php reserved var, then its ok.
  60.         if (in_array($varName$phpReservedVars=== true{
  61.             return;
  62.         }
  63.  
  64.         $objOperator $phpcsFile->findNext(array(T_WHITESPACE)($stackPtr + 1)nulltrue);
  65.         if ($tokens[$objOperator]['code'=== T_OBJECT_OPERATOR{
  66.             // Check to see if we are using a variable from an object.
  67.             $var $phpcsFile->findNext(array(T_WHITESPACE)($objOperator + 1)nulltrue);
  68.             if ($tokens[$var]['code'=== T_STRING{
  69.                 $bracket $phpcsFile->findNext(array(T_WHITESPACE)($var + 1)nulltrue);
  70.                 if ($tokens[$bracket]['code'!== T_OPEN_PARENTHESIS{
  71.                     $objVarName $tokens[$var]['content'];
  72.  
  73.                     // There is no way for us to know if the var is public or
  74.                     // private, so we have to ignore a leading underscore if there is
  75.                     // one and just check the main part of the variable name.
  76.                     $originalVarName $objVarName;
  77.                     if (substr($objVarName01=== '_'{
  78.                         $objVarName substr($objVarName1);
  79.                     }
  80.  
  81.                     if (Common::isCamelCaps($objVarNamefalsetruefalse=== false{
  82.                         $error 'Variable "%s" is not in valid camel caps format';
  83.                         $data  = array($originalVarName);
  84.                         $phpcsFile->addError($error$var'NotCamelCaps'$data);
  85.                     }
  86.                 }//end if
  87.             }//end if
  88.         }//end if
  89.  
  90.         // There is no way for us to know if the var is public or private,
  91.         // so we have to ignore a leading underscore if there is one and just
  92.         // check the main part of the variable name.
  93.         $originalVarName $varName;
  94.         if (substr($varName01=== '_'{
  95.             $objOperator $phpcsFile->findPrevious(array(T_WHITESPACE)($stackPtr - 1)nulltrue);
  96.             if ($tokens[$objOperator]['code'=== T_DOUBLE_COLON{
  97.                 // The variable lives within a class, and is referenced like
  98.                 // this: MyClass::$_variable, so we don't know its scope.
  99.                 $inClass = true;
  100.             else {
  101.                 $inClass $phpcsFile->hasCondition($stackPtrarray(T_CLASST_INTERFACET_TRAIT));
  102.             }
  103.  
  104.             if ($inClass === true{
  105.                 $varName substr($varName1);
  106.             }
  107.         }
  108.  
  109.         if (Common::isCamelCaps($varNamefalsetruefalse=== false{
  110.             $error 'Variable "%s" is not in valid camel caps format';
  111.             $data  = array($originalVarName);
  112.             $phpcsFile->addError($error$stackPtr'NotCamelCaps'$data);
  113.         }
  114.  
  115.     }//end processVariable()
  116.  
  117.  
  118.     /**
  119.      * Processes class member variables.
  120.      *
  121.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  122.      * @param int                         $stackPtr  The position of the current token in the
  123.      *                                                stack passed in $tokens.
  124.      *
  125.      * @return void 
  126.      */
  127.     protected function processMemberVar(File $phpcsFile$stackPtr)
  128.     {
  129.         $tokens $phpcsFile->getTokens();
  130.  
  131.         $varName     ltrim($tokens[$stackPtr]['content']'$');
  132.         $memberProps $phpcsFile->getMemberProperties($stackPtr);
  133.         if (empty($memberProps=== true{
  134.             // Couldn't get any info about this variable, which
  135.             // generally means it is invalid or possibly has a parse
  136.             // error. Any errors will be reported by the core, so
  137.             // we can ignore it.
  138.             return;
  139.         }
  140.  
  141.         $public    ($memberProps['scope'!== 'private');
  142.         $errorData = array($varName);
  143.  
  144.         if ($public === true{
  145.             if (substr($varName01=== '_'{
  146.                 $error '%s member variable "%s" must not contain a leading underscore';
  147.                 $data  = array(
  148.                           ucfirst($memberProps['scope']),
  149.                           $errorData[0],
  150.                          );
  151.                 $phpcsFile->addError($error$stackPtr'PublicHasUnderscore'$data);
  152.                 return;
  153.             }
  154.         else {
  155.             if (substr($varName01!== '_'{
  156.                 $error 'Private member variable "%s" must contain a leading underscore';
  157.                 $phpcsFile->addError($error$stackPtr'PrivateNoUnderscore'$errorData);
  158.                 return;
  159.             }
  160.         }
  161.  
  162.         if (Common::isCamelCaps($varNamefalse$publicfalse=== false{
  163.             $error 'Member variable "%s" is not in valid camel caps format';
  164.             $phpcsFile->addError($error$stackPtr'MemberNotCamelCaps'$errorData);
  165.         }
  166.  
  167.     }//end processMemberVar()
  168.  
  169.  
  170.     /**
  171.      * Processes the variable found within a double quoted string.
  172.      *
  173.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  174.      * @param int                         $stackPtr  The position of the double quoted
  175.      *                                                string.
  176.      *
  177.      * @return void 
  178.      */
  179.     protected function processVariableInString(File $phpcsFile$stackPtr)
  180.     {
  181.         $tokens $phpcsFile->getTokens();
  182.  
  183.         $phpReservedVars = array(
  184.                             '_SERVER',
  185.                             '_GET',
  186.                             '_POST',
  187.                             '_REQUEST',
  188.                             '_SESSION',
  189.                             '_ENV',
  190.                             '_COOKIE',
  191.                             '_FILES',
  192.                             'GLOBALS',
  193.                             'http_response_header',
  194.                             'HTTP_RAW_POST_DATA',
  195.                             'php_errormsg',
  196.                            );
  197.  
  198.         if (preg_match_all('|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|'$tokens[$stackPtr]['content']$matches!== 0{
  199.             foreach ($matches[1as $varName{
  200.                 // If it's a php reserved var, then its ok.
  201.                 if (in_array($varName$phpReservedVars=== true{
  202.                     continue;
  203.                 }
  204.  
  205.                 if (Common::isCamelCaps($varNamefalsetruefalse=== false{
  206.                     $error 'Variable "%s" is not in valid camel caps format';
  207.                     $data  = array($varName);
  208.                     $phpcsFile->addError($error$stackPtr'StringNotCamelCaps'$data);
  209.                 }
  210.             }
  211.         }
  212.  
  213.     }//end processVariableInString()
  214.  
  215.  
  216. }//end class

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