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

Source for file AbstractScopeSniff.php

Documentation is available at AbstractScopeSniff.php

  1. <?php
  2. /**
  3.  * Allows tests that extend this class to listen for tokens within a particular scope.
  4.  *
  5.  * Below is a test that listens to methods that exist only within classes:
  6.  * <code>
  7.  * class ClassScopeTest extends PHP_CodeSniffer_Standards_AbstractScopeSniff
  8.  * {
  9.  *     public function __construct()
  10.  *     {
  11.  *         parent::__construct(array(T_CLASS), array(T_FUNCTION));
  12.  *     }
  13.  *
  14.  *     protected function processTokenWithinScope(\PHP_CodeSniffer\Files\File $phpcsFile, $)
  15.  *     {
  16.  *         $className = $phpcsFile->getDeclarationName($currScope);
  17.  *         echo 'encountered a method within class '.$className;
  18.  *     }
  19.  * }
  20.  * </code>
  21.  *
  22.  * @author    Greg Sherwood <gsherwood@squiz.net>
  23.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  24.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  25.  */
  26.  
  27. namespace PHP_CodeSniffer\Sniffs;
  28.  
  29. use PHP_CodeSniffer\Files\File;
  30. use PHP_CodeSniffer\Exceptions\RuntimeException;
  31.  
  32. abstract class AbstractScopeSniff implements Sniff
  33. {
  34.  
  35.     /**
  36.      * The token types that this test wishes to listen to within the scope.
  37.      *
  38.      * @var array 
  39.      */
  40.     private $tokens = array();
  41.  
  42.     /**
  43.      * The type of scope opener tokens that this test wishes to listen to.
  44.      *
  45.      * @var string 
  46.      */
  47.     private $scopeTokens = array();
  48.  
  49.     /**
  50.      * True if this test should fire on tokens outside of the scope.
  51.      *
  52.      * @var boolean 
  53.      */
  54.     private $listenOutside = false;
  55.  
  56.  
  57.     /**
  58.      * Constructs a new AbstractScopeTest.
  59.      *
  60.      * @param array   $scopeTokens   The type of scope the test wishes to listen to.
  61.      * @param array   $tokens        The tokens that the test wishes to listen to
  62.      *                                within the scope.
  63.      * @param boolean $listenOutside If true this test will also alert the
  64.      *                                extending class when a token is found outside
  65.      *                                the scope, by calling the
  66.      *                                processTokenOutsideScope method.
  67.      *
  68.      * @see    PHP_CodeSniffer.getValidScopeTokeners()
  69.      * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified tokens array is empty.
  70.      */
  71.     public function __construct(
  72.         array $scopeTokens,
  73.         array $tokens,
  74.         $listenOutside=false
  75.     {
  76.         if (empty($scopeTokens=== true{
  77.             $error 'The scope tokens list cannot be empty';
  78.             throw new RuntimeException($error);
  79.         }
  80.  
  81.         if (empty($tokens=== true{
  82.             $error 'The tokens list cannot be empty';
  83.             throw new RuntimeException($error);
  84.         }
  85.  
  86.         $invalidScopeTokens array_intersect($scopeTokens$tokens);
  87.         if (empty($invalidScopeTokens=== false{
  88.             $invalid implode(', '$invalidScopeTokens);
  89.             $error   = "Scope tokens [$invalid] can't be in the tokens array";
  90.             throw new RuntimeException($error);
  91.         }
  92.  
  93.         $this->listenOutside = $listenOutside;
  94.         $this->scopeTokens   = array_flip($scopeTokens);
  95.         $this->tokens        = $tokens;
  96.  
  97.     }//end __construct()
  98.  
  99.  
  100.     /**
  101.      * The method that is called to register the tokens this test wishes to
  102.      * listen to.
  103.      *
  104.      * DO NOT OVERRIDE THIS METHOD. Use the constructor of this class to register
  105.      * for the desired tokens and scope.
  106.      *
  107.      * @return int[] 
  108.      * @see    __constructor()
  109.      */
  110.     final public function register()
  111.     {
  112.         return $this->tokens;
  113.  
  114.     }//end register()
  115.  
  116.  
  117.     /**
  118.      * Processes the tokens that this test is listening for.
  119.      *
  120.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  121.      * @param int                         $stackPtr  The position in the stack where this
  122.      *                                                token was found.
  123.      *
  124.      * @return void 
  125.      * @see    processTokenWithinScope()
  126.      */
  127.     final public function process(File $phpcsFile$stackPtr)
  128.     {
  129.         $tokens $phpcsFile->getTokens();
  130.  
  131.         $foundScope = false;
  132.         foreach ($tokens[$stackPtr]['conditions'as $scope => $code{
  133.             if (isset($this->scopeTokens[$code]=== true{
  134.                 $this->processTokenWithinScope($phpcsFile$stackPtr$scope);
  135.                 $foundScope = true;
  136.             }
  137.         }
  138.  
  139.         if ($this->listenOutside === true && $foundScope === false{
  140.             $this->processTokenOutsideScope($phpcsFile$stackPtr);
  141.         }
  142.  
  143.     }//end process()
  144.  
  145.  
  146.     /**
  147.      * Processes a token that is found within the scope that this test is
  148.      * listening to.
  149.      *
  150.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  151.      * @param int                         $stackPtr  The position in the stack where this
  152.      *                                                token was found.
  153.      * @param int                         $currScope The position in the tokens array that
  154.      *                                                opened the scope that this test is
  155.      *                                                listening for.
  156.      *
  157.      * @return void 
  158.      */
  159.     abstract protected function processTokenWithinScope(File $phpcsFile$stackPtr$currScope);
  160.  
  161.  
  162.     /**
  163. /**
  164.      * Processes a token that is found outside the scope that this test is
  165.      * listening to.
  166.      *
  167.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  168.      * @param int                         $stackPtr  The position in the stack where this
  169.      *                                                token was found.
  170.      *
  171.      * @return void 
  172.      */
  173.     abstract protected function processTokenOutsideScope(File $phpcsFile$stackPtr);
  174.  
  175.  
  176. //end class

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