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\Zend\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'              => true,
  46.                             '_GET'                 => true,
  47.                             '_POST'                => true,
  48.                             '_REQUEST'             => true,
  49.                             '_SESSION'             => true,
  50.                             '_ENV'                 => true,
  51.                             '_COOKIE'              => true,
  52.                             '_FILES'               => true,
  53.                             'GLOBALS'              => true,
  54.                             'http_response_header' => true,
  55.                             'HTTP_RAW_POST_DATA'   => true,
  56.                             'php_errormsg'         => true,
  57.                            );
  58.  
  59.         // If it's a php reserved var, then its ok.
  60.         if (isset($phpReservedVars[$varName]=== 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.                 // Either a var name or a function call, so check for bracket.
  70.                 $bracket $phpcsFile->findNext(array(T_WHITESPACE)($var + 1)nulltrue);
  71.  
  72.                 if ($tokens[$bracket]['code'!== T_OPEN_PARENTHESIS{
  73.                     $objVarName $tokens[$var]['content'];
  74.  
  75.                     // There is no way for us to know if the var is public or private,
  76.                     // so we have to ignore a leading underscore if there is one and just
  77.                     // check the main part of the variable name.
  78.                     $originalVarName $objVarName;
  79.                     if (substr($objVarName01=== '_'{
  80.                         $objVarName substr($objVarName1);
  81.                     }
  82.  
  83.                     if (Common::isCamelCaps($objVarNamefalsetruefalse=== false{
  84.                         $error 'Variable "%s" is not in valid camel caps format';
  85.                         $data  = array($originalVarName);
  86.                         $phpcsFile->addError($error$var'NotCamelCaps'$data);
  87.                     else if (preg_match('|\d|'$objVarName=== 1{
  88.                         $warning 'Variable "%s" contains numbers but this is discouraged';
  89.                         $data    = array($originalVarName);
  90.                         $phpcsFile->addWarning($warning$stackPtr'ContainsNumbers'$data);
  91.                     }
  92.                 }//end if
  93.             }//end if
  94.         }//end if
  95.  
  96.         // There is no way for us to know if the var is public or private,
  97.         // so we have to ignore a leading underscore if there is one and just
  98.         // check the main part of the variable name.
  99.         $originalVarName $varName;
  100.         if (substr($varName01=== '_'{
  101.             $objOperator $phpcsFile->findPrevious(array(T_WHITESPACE)($stackPtr - 1)nulltrue);
  102.             if ($tokens[$objOperator]['code'=== T_DOUBLE_COLON{
  103.                 // The variable lives within a class, and is referenced like
  104.                 // this: MyClass::$_variable, so we don't know its scope.
  105.                 $inClass = true;
  106.             else {
  107.                 $inClass $phpcsFile->hasCondition($stackPtrarray(T_CLASST_INTERFACET_TRAIT));
  108.             }
  109.  
  110.             if ($inClass === true{
  111.                 $varName substr($varName1);
  112.             }
  113.         }
  114.  
  115.         if (Common::isCamelCaps($varNamefalsetruefalse=== false{
  116.             $error 'Variable "%s" is not in valid camel caps format';
  117.             $data  = array($originalVarName);
  118.             $phpcsFile->addError($error$stackPtr'NotCamelCaps'$data);
  119.         else if (preg_match('|\d|'$varName=== 1{
  120.             $warning 'Variable "%s" contains numbers but this is discouraged';
  121.             $data    = array($originalVarName);
  122.             $phpcsFile->addWarning($warning$stackPtr'ContainsNumbers'$data);
  123.         }
  124.  
  125.     }//end processVariable()
  126.  
  127.  
  128.     /**
  129.      * Processes class member variables.
  130.      *
  131.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  132.      * @param int                         $stackPtr  The position of the current token in the
  133.      *                                                stack passed in $tokens.
  134.      *
  135.      * @return void 
  136.      */
  137.     protected function processMemberVar(File $phpcsFile$stackPtr)
  138.     {
  139.         $tokens      $phpcsFile->getTokens();
  140.         $varName     ltrim($tokens[$stackPtr]['content']'$');
  141.         $memberProps $phpcsFile->getMemberProperties($stackPtr);
  142.         $public      ($memberProps['scope'=== 'public');
  143.  
  144.         if ($public === true{
  145.             if (substr($varName01=== '_'{
  146.                 $error 'Public member variable "%s" must not contain a leading underscore';
  147.                 $data  = array($varName);
  148.                 $phpcsFile->addError($error$stackPtr'PublicHasUnderscore'$data);
  149.                 return;
  150.             }
  151.         else {
  152.             if (substr($varName01!== '_'{
  153.                 $scope ucfirst($memberProps['scope']);
  154.                 $error '%s member variable "%s" must contain a leading underscore';
  155.                 $data  = array(
  156.                           $scope,
  157.                           $varName,
  158.                          );
  159.                 $phpcsFile->addError($error$stackPtr'PrivateNoUnderscore'$data);
  160.                 return;
  161.             }
  162.         }
  163.  
  164.         if (Common::isCamelCaps($varNamefalse$publicfalse=== false{
  165.             $error 'Member variable "%s" is not in valid camel caps format';
  166.             $data  = array($varName);
  167.             $phpcsFile->addError($error$stackPtr'MemberVarNotCamelCaps'$data);
  168.         else if (preg_match('|\d|'$varName=== 1{
  169.             $warning 'Member variable "%s" contains numbers but this is discouraged';
  170.             $data    = array($varName);
  171.             $phpcsFile->addWarning($warning$stackPtr'MemberVarContainsNumbers'$data);
  172.         }
  173.  
  174.     }//end processMemberVar()
  175.  
  176.  
  177.     /**
  178.      * Processes the variable found within a double quoted string.
  179.      *
  180.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  181.      * @param int                         $stackPtr  The position of the double quoted
  182.      *                                                string.
  183.      *
  184.      * @return void 
  185.      */
  186.     protected function processVariableInString(File $phpcsFile$stackPtr)
  187.     {
  188.         $tokens $phpcsFile->getTokens();
  189.  
  190.         $phpReservedVars = array(
  191.                             '_SERVER',
  192.                             '_GET',
  193.                             '_POST',
  194.                             '_REQUEST',
  195.                             '_SESSION',
  196.                             '_ENV',
  197.                             '_COOKIE',
  198.                             '_FILES',
  199.                             'GLOBALS',
  200.                             'http_response_header',
  201.                             'HTTP_RAW_POST_DATA',
  202.                             'php_errormsg',
  203.                            );
  204.  
  205.         if (preg_match_all('|[^\\\]\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|'$tokens[$stackPtr]['content']$matches!== 0{
  206.             foreach ($matches[1as $varName{
  207.                 // If it's a php reserved var, then its ok.
  208.                 if (in_array($varName$phpReservedVars=== true{
  209.                     continue;
  210.                 }
  211.  
  212.                 if (Common::isCamelCaps($varNamefalsetruefalse=== false{
  213.                     $error 'Variable "%s" is not in valid camel caps format';
  214.                     $data  = array($varName);
  215.                     $phpcsFile->addError($error$stackPtr'StringVarNotCamelCaps'$data);
  216.                 else if (preg_match('|\d|'$varName=== 1{
  217.                     $warning 'Variable "%s" contains numbers but this is discouraged';
  218.                     $data    = array($varName);
  219.                     $phpcsFile->addWarning($warning$stackPtr'StringVarContainsNumbers'$data);
  220.                 }
  221.             }//end foreach
  222.         }//end if
  223.  
  224.     }//end processVariableInString()
  225.  
  226.  
  227. }//end class

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