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

Source for file IncludeOwnSystemSniff.php

Documentation is available at IncludeOwnSystemSniff.php

  1. <?php
  2. /**
  3.  * Ensures that a system does not include itself.
  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 IncludeOwnSystemSniff 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.         $fileName $phpcsFile->getFilename();
  43.         $matches  = array();
  44.         if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i'$fileName$matches=== 0{
  45.             // Not an actions file.
  46.             return;
  47.         }
  48.  
  49.         $ownClass $matches[2];
  50.         $tokens   $phpcsFile->getTokens();
  51.  
  52.         $typeName $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING($stackPtr + 2)nullfalsetrue);
  53.         $typeName trim($tokens[$typeName]['content']" '");
  54.         switch (strtolower($tokens[($stackPtr + 1)]['content'])) {
  55.         case 'includesystem' :
  56.             $included strtolower($typeName);
  57.             break;
  58.         case 'includeasset' :
  59.             $included strtolower($typeName).'assettype';
  60.             break;
  61.         case 'includewidget' :
  62.             $included strtolower($typeName).'widgettype';
  63.             break;
  64.         default:
  65.             return;
  66.         }
  67.  
  68.         if ($included === strtolower($ownClass)) {
  69.             $error "You do not need to include \"%s\" from within the system's own actions file";
  70.             $data  = array($ownClass);
  71.             $phpcsFile->addError($error$stackPtr'NotRequired'$data);
  72.         }
  73.  
  74.     }//end process()
  75.  
  76.  
  77.     /**
  78.      * Determines the included class name from given token.
  79.      *
  80.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  81.      * @param array                       $tokens    The array of file tokens.
  82.      * @param int                         $stackPtr  The position in the tokens array of the
  83.      *                                                potentially included class.
  84.      *
  85.      * @return string 
  86.      */
  87.     protected function getIncludedClassFromToken(
  88.         $phpcsFile,
  89.         array $tokens,
  90.         $stackPtr
  91.     {
  92.  
  93.         return false;
  94.  
  95.     }//end getIncludedClassFromToken()
  96.  
  97.  
  98. }//end class

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