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

Source for file DisallowSelfActionsSniff.php

Documentation is available at DisallowSelfActionsSniff.php

  1. <?php
  2. /**
  3.  * Ensures that self and static are not used to call public methods in action classes.
  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. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class DisallowSelfActionsSniff 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_CLASS);
  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.         // We are not interested in abstract classes.
  46.         $prev $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  47.         if ($prev !== false && $tokens[$prev]['code'=== T_ABSTRACT{
  48.             return;
  49.         }
  50.  
  51.         // We are only interested in Action classes.
  52.         $classNameToken $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  53.         $className      $tokens[$classNameToken]['content'];
  54.         if (substr($className-7!== 'Actions'{
  55.             return;
  56.         }
  57.  
  58.         $foundFunctions = array();
  59.         $foundCalls     = array();
  60.  
  61.         // Find all static method calls in the form self::method() in the class.
  62.         $classEnd $tokens[$stackPtr]['scope_closer'];
  63.         for ($i ($classNameToken + 1)$i $classEnd$i++{
  64.             if ($tokens[$i]['code'!== T_DOUBLE_COLON{
  65.                 if ($tokens[$i]['code'=== T_FUNCTION{
  66.                     // Cache the function information.
  67.                     $funcName  $phpcsFile->findNext(T_STRING($i + 1));
  68.                     $funcScope $phpcsFile->findPrevious(Tokens::$scopeModifiers($i - 1));
  69.  
  70.                     $foundFunctions[$tokens[$funcName]['content']] = strtolower($tokens[$funcScope]['content']);
  71.                 }
  72.  
  73.                 continue;
  74.             }
  75.  
  76.             $prevToken $phpcsFile->findPrevious(T_WHITESPACE($i - 1)nulltrue);
  77.             if ($tokens[$prevToken]['content'!== 'self'
  78.                 && $tokens[$prevToken]['content'!== 'static'
  79.             {
  80.                 continue;
  81.             }
  82.  
  83.             $funcNameToken $phpcsFile->findNext(T_WHITESPACE($i + 1)nulltrue);
  84.             if ($tokens[$funcNameToken]['code'=== T_VARIABLE{
  85.                 // We are only interested in function calls.
  86.                 continue;
  87.             }
  88.  
  89.             $funcName $tokens[$funcNameToken]['content'];
  90.  
  91.             // We've found the function, now we need to find it and see if it is
  92.             // public, private or protected. If it starts with an underscore we
  93.             // can assume it is private.
  94.             if ($funcName{0=== '_'{
  95.                 continue;
  96.             }
  97.  
  98.             $foundCalls[$i= array(
  99.                                'name' => $funcName,
  100.                                'type' => strtolower($tokens[$prevToken]['content']),
  101.                               );
  102.         }//end for
  103.  
  104.         $errorClassName substr($className0-7);
  105.  
  106.         foreach ($foundCalls as $token => $funcData{
  107.             if (isset($foundFunctions[$funcData['name']]=== false{
  108.                 // Function was not in this class, might have come from the parent.
  109.                 // Either way, we can't really check this.
  110.                 continue;
  111.             else if ($foundFunctions[$funcData['name']] === 'public'{
  112.                 $type  $funcData['type'];
  113.                 $error = "Static calls to public methods in Action classes must not use the $type keyword; use %s::%s() instead";
  114.                 $data  = array(
  115.                           $errorClassName,
  116.                           $funcName,
  117.                          );
  118.                 $phpcsFile->addError($error$token'Found'.ucfirst($funcData['type'])$data);
  119.             }
  120.         }
  121.  
  122.     }//end process()
  123.  
  124.  
  125. }//end class

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