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

Source for file SelfMemberReferenceSniff.php

Documentation is available at SelfMemberReferenceSniff.php

  1. <?php
  2. /**
  3.  * Tests self member references.
  4.  *
  5.  * Verifies that :
  6.  * - self:: is used instead of Self::
  7.  * - self:: is used for local static member reference
  8.  * - self:: is used instead of self ::
  9.  *
  10.  * @author    Greg Sherwood <gsherwood@squiz.net>
  11.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  12.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  13.  */
  14.  
  15. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes;
  16.  
  17. use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
  18. use PHP_CodeSniffer\Files\File;
  19.  
  20. class SelfMemberReferenceSniff extends AbstractScopeSniff
  21. {
  22.  
  23.  
  24.     /**
  25.      * Constructs a Squiz_Sniffs_Classes_SelfMemberReferenceSniff.
  26.      */
  27.     public function __construct()
  28.     {
  29.         parent::__construct(array(T_CLASS)array(T_DOUBLE_COLON));
  30.  
  31.     }//end __construct()
  32.  
  33.  
  34.     /**
  35.      * Processes the function tokens within the class.
  36.      *
  37.      * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
  38.      * @param int                  $stackPtr  The position where the token was found.
  39.      * @param int                  $currScope The current scope opener token.
  40.      *
  41.      * @return void 
  42.      */
  43.     protected function processTokenWithinScope(File $phpcsFile$stackPtr$currScope)
  44.     {
  45.         $tokens $phpcsFile->getTokens();
  46.  
  47.         $calledClassName ($stackPtr - 1);
  48.         if ($tokens[$calledClassName]['code'=== T_SELF{
  49.             if (strtolower($tokens[$calledClassName]['content']!== $tokens[$calledClassName]['content']{
  50.                 $error 'Must use "self::" for local static member reference; found "%s::"';
  51.                 $data  = array($tokens[$calledClassName]['content']);
  52.                 $phpcsFile->addError($error$calledClassName'IncorrectCase'$data);
  53.                 return;
  54.             }
  55.         else if ($tokens[$calledClassName]['code'=== T_STRING{
  56.             // If the class is called with a namespace prefix, build fully qualified
  57.             // namespace calls for both current scope class and requested class.
  58.             if ($tokens[($calledClassName - 1)]['code'=== T_NS_SEPARATOR{
  59.                 $declarationName         $this->getDeclarationNameWithNamespace($tokens$calledClassName);
  60.                 $declarationName         substr($declarationName1);
  61.                 $fullQualifiedClassName  $this->getNamespaceOfScope($phpcsFile$currScope);
  62.                 $fullQualifiedClassName .= '\\'.$phpcsFile->getDeclarationName($currScope);
  63.             else {
  64.                 $declarationName        $phpcsFile->getDeclarationName($currScope);
  65.                 $fullQualifiedClassName $tokens[$calledClassName]['content'];
  66.             }
  67.  
  68.             if ($declarationName === $fullQualifiedClassName{
  69.                 // Class name is the same as the current class, which is not allowed
  70.                 // except if being used inside a closure.
  71.                 if ($phpcsFile->hasCondition($stackPtrT_CLOSURE=== false{
  72.                     $error 'Must use "self::" for local static member reference';
  73.                     $fix   $phpcsFile->addFixableError($error$calledClassName'NotUsed');
  74.  
  75.                     if ($fix === true{
  76.                         $prev $phpcsFile->findPrevious(array(T_NS_SEPARATORT_STRING)($stackPtr - 1)nulltrue);
  77.                         $phpcsFile->fixer->beginChangeset();
  78.                         for ($i ($prev + 1)$i $stackPtr$i++{
  79.                             $phpcsFile->fixer->replaceToken($i'');
  80.                         }
  81.  
  82.                         $phpcsFile->fixer->replaceToken($stackPtr'self::');
  83.                         $phpcsFile->fixer->endChangeset();
  84.                     }
  85.  
  86.                     return;
  87.                 }
  88.             }//end if
  89.         }//end if
  90.  
  91.         if ($tokens[($stackPtr - 1)]['code'=== T_WHITESPACE{
  92.             $found strlen($tokens[($stackPtr - 1)]['content']);
  93.             $error 'Expected 0 spaces before double colon; %s found';
  94.             $data  = array($found);
  95.             $fix   $phpcsFile->addFixableError($error$calledClassName'SpaceBefore'$data);
  96.  
  97.             if ($fix === true{
  98.                 $phpcsFile->fixer->replaceToken(($stackPtr - 1)'');
  99.             }
  100.         }
  101.  
  102.         if ($tokens[($stackPtr + 1)]['code'=== T_WHITESPACE{
  103.             $found strlen($tokens[($stackPtr + 1)]['content']);
  104.             $error 'Expected 0 spaces after double colon; %s found';
  105.             $data  = array($found);
  106.             $fix   $phpcsFile->addFixableError($error$calledClassName'SpaceAfter'$data);
  107.  
  108.             if ($fix === true{
  109.                 $phpcsFile->fixer->replaceToken(($stackPtr + 1)'');
  110.             }
  111.         }
  112.  
  113.     }//end processTokenWithinScope()
  114.  
  115.  
  116.     /**
  117.      * Processes a token that is found within the scope that this test is
  118.      * listening to.
  119.      *
  120.      * @param PHP_CodeSniffer_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.      */
  126.     protected function processTokenOutsideScope(File $phpcsFile$stackPtr)
  127.     {
  128.  
  129.     }//end processTokenOutsideScope()
  130.  
  131.  
  132.     /**
  133.      * Returns the declaration names for classes/interfaces/functions with a namespace.
  134.      *
  135.      * @param array $tokens   Token stack for this file
  136.      * @param int   $stackPtr The position where the namespace building will start.
  137.      *
  138.      * @return string 
  139.      */
  140.     protected function getDeclarationNameWithNamespace(array $tokens$stackPtr)
  141.     {
  142.         $nameParts      = array();
  143.         $currentPointer $stackPtr;
  144.         while ($tokens[$currentPointer]['code'=== T_NS_SEPARATOR
  145.             || $tokens[$currentPointer]['code'=== T_STRING
  146.         {
  147.             $nameParts[$tokens[$currentPointer]['content'];
  148.             $currentPointer--;
  149.         }
  150.  
  151.         $nameParts = array_reverse($nameParts);
  152.         return implode(''$nameParts);
  153.  
  154.     }//end getDeclarationNameWithNamespace()
  155.  
  156.  
  157.     /**
  158.      * Returns the namespace declaration of a file.
  159.      *
  160.      * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
  161.      * @param int                  $stackPtr  The position where the search for the
  162.      *                                         namespace declaration will start.
  163.      *
  164.      * @return string 
  165.      */
  166.     protected function getNamespaceOfScope(File $phpcsFile$stackPtr)
  167.     {
  168.         $namespace            '\\';
  169.         $namespaceDeclaration $phpcsFile->findPrevious(T_NAMESPACE$stackPtr);
  170.  
  171.         if ($namespaceDeclaration !== false{
  172.             $endOfNamespaceDeclaration $phpcsFile->findNext(T_SEMICOLON$namespaceDeclaration);
  173.             $namespace $this->getDeclarationNameWithNamespace(
  174.                 $phpcsFile->getTokens(),
  175.                 ($endOfNamespaceDeclaration - 1)
  176.             );
  177.         }
  178.  
  179.         return $namespace;
  180.  
  181.     }//end getNamespaceOfScope()
  182.  
  183.  
  184. }//end class

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