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

Source for file IncludingFileSniff.php

Documentation is available at IncludingFileSniff.php

  1. <?php
  2. /**
  3.  * Ensure include_once is used in conditional situations and require_once is used elsewhere.
  4.  *
  5.  * Also checks that brackets do not surround the file being included.
  6.  *
  7.  * @author    Greg Sherwood <gsherwood@squiz.net>
  8.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  9.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  10.  */
  11.  
  12. namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\Files;
  13.  
  14. use PHP_CodeSniffer\Sniffs\Sniff;
  15. use PHP_CodeSniffer\Files\File;
  16. use PHP_CodeSniffer\Util\Tokens;
  17.  
  18. class IncludingFileSniff implements Sniff
  19. {
  20.  
  21.  
  22.     /**
  23.      * Returns an array of tokens this test wants to listen for.
  24.      *
  25.      * @return array 
  26.      */
  27.     public function register()
  28.     {
  29.         return array(
  30.                 T_INCLUDE_ONCE,
  31.                 T_REQUIRE_ONCE,
  32.                 T_REQUIRE,
  33.                 T_INCLUDE,
  34.                );
  35.  
  36.     }//end register()
  37.  
  38.  
  39.     /**
  40.      * Processes this test, when one of its tokens is encountered.
  41.      *
  42.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  43.      * @param int                         $stackPtr  The position of the current token in the
  44.      *                                                stack passed in $tokens.
  45.      *
  46.      * @return void 
  47.      */
  48.     public function process(File $phpcsFile$stackPtr)
  49.     {
  50.         $tokens $phpcsFile->getTokens();
  51.  
  52.         $nextToken $phpcsFile->findNext(Tokens::$emptyTokens($stackPtr + 1)nulltrue);
  53.         if ($tokens[$nextToken]['code'=== T_OPEN_PARENTHESIS{
  54.             $error '"%s" is a statement not a function; no parentheses are required';
  55.             $data  = array($tokens[$stackPtr]['content']);
  56.             $fix   $phpcsFile->addFixableError($error$stackPtr'BracketsNotRequired'$data);
  57.             if ($fix === true{
  58.                 $phpcsFile->fixer->beginChangeset();
  59.                 $phpcsFile->fixer->replaceToken($tokens[$nextToken]['parenthesis_closer']'');
  60.                 if ($tokens[($nextToken - 1)]['code'!== T_WHITESPACE{
  61.                     $phpcsFile->fixer->replaceToken($nextToken' ');
  62.                 else {
  63.                     $phpcsFile->fixer->replaceToken($nextToken'');
  64.                 }
  65.  
  66.                 $phpcsFile->fixer->endChangeset();
  67.             }
  68.         }
  69.  
  70.         if (count($tokens[$stackPtr]['conditions']!== 0{
  71.             $inCondition = true;
  72.         else {
  73.             $inCondition = false;
  74.         }
  75.  
  76.         // Check to see if this including statement is within the parenthesis
  77.         // of a condition. If that's the case then we need to process it as being
  78.         // within a condition, as they are checking the return value.
  79.         if (isset($tokens[$stackPtr]['nested_parenthesis']=== true{
  80.             foreach ($tokens[$stackPtr]['nested_parenthesis'as $left => $right{
  81.                 if (isset($tokens[$left]['parenthesis_owner']=== true{
  82.                     $inCondition = true;
  83.                 }
  84.             }
  85.         }
  86.  
  87.         // Check to see if they are assigning the return value of this
  88.         // including call. If they are then they are probably checking it, so
  89.         // it's conditional.
  90.         $previous $phpcsFile->findPrevious(Tokens::$emptyTokens($stackPtr - 1)nulltrue);
  91.         if (isset(Tokens::$assignmentTokens[$tokens[$previous]['code']]=== true{
  92.             // The have assigned the return value to it, so its conditional.
  93.             $inCondition = true;
  94.         }
  95.  
  96.         $tokenCode $tokens[$stackPtr]['code'];
  97.         if ($inCondition === true{
  98.             // We are inside a conditional statement. We need an include_once.
  99.             if ($tokenCode === T_REQUIRE_ONCE{
  100.                 $error  'File is being conditionally included; ';
  101.                 $error .= 'use "include_once" instead';
  102.                 $fix    $phpcsFile->addFixableError($error$stackPtr'UseIncludeOnce');
  103.                 if ($fix === true{
  104.                     $phpcsFile->fixer->replaceToken($stackPtr'include_once');
  105.                 }
  106.             else if ($tokenCode === T_REQUIRE{
  107.                 $error  'File is being conditionally included; ';
  108.                 $error .= 'use "include" instead';
  109.                 $fix    $phpcsFile->addFixableError($error$stackPtr'UseInclude');
  110.                 if ($fix === true{
  111.                     $phpcsFile->fixer->replaceToken($stackPtr'include');
  112.                 }
  113.             }
  114.         else {
  115.             // We are unconditionally including, we need a require_once.
  116.             if ($tokenCode === T_INCLUDE_ONCE{
  117.                 $error  'File is being unconditionally included; ';
  118.                 $error .= 'use "require_once" instead';
  119.                 $fix    $phpcsFile->addFixableError($error$stackPtr'UseRequireOnce');
  120.                 if ($fix === true{
  121.                     $phpcsFile->fixer->replaceToken($stackPtr'require_once');
  122.                 }
  123.             else if ($tokenCode === T_INCLUDE{
  124.                 $error  'File is being unconditionally included; ';
  125.                 $error .= 'use "require" instead';
  126.                 $fix    $phpcsFile->addFixableError($error$stackPtr'UseRequire');
  127.                 if ($fix === true{
  128.                     $phpcsFile->fixer->replaceToken($stackPtr'require');
  129.                 }
  130.             }
  131.         }//end if
  132.  
  133.     }//end process()
  134.  
  135.  
  136. }//end class

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