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

Source for file EvalObjectFactorySniff.php

Documentation is available at EvalObjectFactorySniff.php

  1. <?php
  2. /**
  3.  * Ensures that eval() is not used to create objects.
  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\MySource\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class EvalObjectFactorySniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         return array(T_EVAL);
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Processes this sniff, when one of its tokens is encountered.
  34.      *
  35.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  36.      * @param int                         $stackPtr  The position of the current token in
  37.      *                                                the stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.  
  45.         /*
  46.             We need to find all strings that will be in the eval
  47.             to determine if the "new" keyword is being used.
  48.         */
  49.  
  50.         $openBracket  $phpcsFile->findNext(T_OPEN_PARENTHESIS($stackPtr + 1));
  51.         $closeBracket $tokens[$openBracket]['parenthesis_closer'];
  52.  
  53.         $strings = array();
  54.         $vars    = array();
  55.  
  56.         for ($i ($openBracket + 1)$i $closeBracket$i++{
  57.             if (isset(Tokens::$stringTokens[$tokens[$i]['code']]=== true{
  58.                 $strings[$i$tokens[$i]['content'];
  59.             else if ($tokens[$i]['code'=== T_VARIABLE{
  60.                 $vars[$i$tokens[$i]['content'];
  61.             }
  62.         }
  63.  
  64.         /*
  65.             We now have some variables that we need to expand into
  66.             the strings that were assigned to them, if any.
  67.         */
  68.  
  69.         foreach ($vars as $varPtr => $varName{
  70.             while (($prev $phpcsFile->findPrevious(T_VARIABLE($varPtr - 1))) !== false{
  71.                 // Make sure this is an assignment of the variable. That means
  72.                 // it will be the first thing on the line.
  73.                 $prevContent $phpcsFile->findPrevious(T_WHITESPACE($prev - 1)nulltrue);
  74.                 if ($tokens[$prevContent]['line'=== $tokens[$prev]['line']{
  75.                     $varPtr $prevContent;
  76.                     continue;
  77.                 }
  78.  
  79.                 if ($tokens[$prev]['content'!== $varName{
  80.                     // This variable has a different name.
  81.                     $varPtr $prevContent;
  82.                     continue;
  83.                 }
  84.  
  85.                 // We found one.
  86.                 break;
  87.             }//end while
  88.  
  89.             if ($prev !== false{
  90.                 // Find all strings on the line.
  91.                 $lineEnd $phpcsFile->findNext(T_SEMICOLON($prev + 1));
  92.                 for ($i ($prev + 1)$i $lineEnd$i++{
  93.                     if (isset(Tokens::$stringTokens[$tokens[$i]['code']]=== true{
  94.                         $strings[$i$tokens[$i]['content'];
  95.                     }
  96.                 }
  97.             }
  98.         }//end foreach
  99.  
  100.         foreach ($strings as $string{
  101.             // If the string has "new" in it, it is not allowed.
  102.             // We don't bother checking if the word "new" is echo'd
  103.             // because that is unlikely to happen. We assume the use
  104.             // of "new" is for object instantiation.
  105.             if (strstr($string' new '!== false{
  106.                 $error 'Do not use eval() to create objects dynamically; use reflection instead';
  107.                 $phpcsFile->addWarning($error$stackPtr'Found');
  108.             }
  109.         }
  110.  
  111.     }//end process()
  112.  
  113.  
  114. }//end class

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