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

Source for file UnusedSystemSniff.php

Documentation is available at UnusedSystemSniff.php

  1. <?php
  2. /**
  3.  * Ensures that systems and asset types are used if they are included.
  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\Channels;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class UnusedSystemSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(T_DOUBLE_COLON);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this sniff, when one of its tokens is encountered.
  33.      *
  34.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  35.      * @param int                         $stackPtr  The position of the current token in
  36.      *                                                the stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         $tokens $phpcsFile->getTokens();
  43.  
  44.         // Check if this is a call to includeSystem, includeAsset or includeWidget.
  45.         $methodName strtolower($tokens[($stackPtr + 1)]['content']);
  46.         if ($methodName === 'includesystem'
  47.             || $methodName === 'includeasset'
  48.             || $methodName === 'includewidget'
  49.         {
  50.             $systemName $phpcsFile->findNext(T_WHITESPACE($stackPtr + 3)nulltrue);
  51.             if ($systemName === false || $tokens[$systemName]['code'!== T_CONSTANT_ENCAPSED_STRING{
  52.                 // Must be using a variable instead of a specific system name.
  53.                 // We can't accurately check that.
  54.                 return;
  55.             }
  56.  
  57.             $systemName trim($tokens[$systemName]['content']" '");
  58.         else {
  59.             return;
  60.         }
  61.  
  62.         if ($methodName === 'includeasset'{
  63.             $systemName .= 'assettype';
  64.         else if ($methodName === 'includewidget'{
  65.             $systemName .= 'widgettype';
  66.         }
  67.  
  68.         $systemName strtolower($systemName);
  69.  
  70.         // Now check if this system is used anywhere in this scope.
  71.         $level $tokens[$stackPtr]['level'];
  72.         for ($i ($stackPtr + 1)$i $phpcsFile->numTokens; $i++{
  73.             if ($tokens[$i]['level'$level{
  74.                 // We have gone out of scope.
  75.                 // If the original include was inside an IF statement that
  76.                 // is checking if the system exists, check the outer scope
  77.                 // as well.
  78.                 if ($tokens[$stackPtr]['level'=== $level{
  79.                     // We are still in the base level, so this is the first
  80.                     // time we have got here.
  81.                     $conditions array_keys($tokens[$stackPtr]['conditions']);
  82.                     if (empty($conditions=== false{
  83.                         $cond array_pop($conditions);
  84.                         if ($tokens[$cond]['code'=== T_IF{
  85.                             $i $tokens[$cond]['scope_closer'];
  86.                             $level--;
  87.                             continue;
  88.                         }
  89.                     }
  90.                 }
  91.  
  92.                 break;
  93.             }//end if
  94.  
  95.             if ($tokens[$i]['code'!== T_DOUBLE_COLON
  96.                 && $tokens[$i]['code'!== T_EXTENDS
  97.                 && $tokens[$i]['code'!== T_IMPLEMENTS
  98.             {
  99.                 continue;
  100.             }
  101.  
  102.             switch ($tokens[$i]['code']{
  103.             case T_DOUBLE_COLON:
  104.                 $usedName strtolower($tokens[($i - 1)]['content']);
  105.                 if ($usedName === $systemName{
  106.                     // The included system was used, so it is fine.
  107.                     return;
  108.                 }
  109.                 break;
  110.             case T_EXTENDS:
  111.                 $classNameToken $phpcsFile->findNext(T_STRING($i + 1));
  112.                 $className      strtolower($tokens[$classNameToken]['content']);
  113.                 if ($className === $systemName{
  114.                     // The included system was used, so it is fine.
  115.                     return;
  116.                 }
  117.                 break;
  118.             case T_IMPLEMENTS:
  119.                 $endImplements $phpcsFile->findNext(array(T_EXTENDST_OPEN_CURLY_BRACKET)($i + 1));
  120.                 for ($x ($i + 1)$x $endImplements$x++{
  121.                     if ($tokens[$x]['code'=== T_STRING{
  122.                         $className strtolower($tokens[$x]['content']);
  123.                         if ($className === $systemName{
  124.                             // The included system was used, so it is fine.
  125.                             return;
  126.                         }
  127.                     }
  128.                 }
  129.                 break;
  130.             }//end switch
  131.         }//end for
  132.  
  133.         // If we get to here, the system was not use.
  134.         $error 'Included system "%s" is never used';
  135.         $data  = array($systemName);
  136.         $phpcsFile->addError($error$stackPtr'Found'$data);
  137.  
  138.     }//end process()
  139.  
  140.  
  141. }//end class

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