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

Source for file ConstructorNameSniff.php

Documentation is available at ConstructorNameSniff.php

  1. <?php
  2. /**
  3.  * Bans PHP 4 style constructors.
  4.  *
  5.  * Favor PHP 5 constructor syntax, which uses "function __construct()".
  6.  * Avoid PHP 4 constructor syntax, which uses "function ClassName()".
  7.  *
  8.  * @author    Greg Sherwood <gsherwood@squiz.net>
  9.  * @author    Leif Wickland <lwickland@rightnow.com>
  10.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  11.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12.  */
  13.  
  14. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions;
  15.  
  16. use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
  17. use PHP_CodeSniffer\Files\File;
  18.  
  19. class ConstructorNameSniff extends AbstractScopeSniff
  20. {
  21.  
  22.     /**
  23.      * The name of the class we are currently checking.
  24.      *
  25.      * @var string 
  26.      */
  27.     private $currentClass = '';
  28.  
  29.     /**
  30.      * A list of functions in the current class.
  31.      *
  32.      * @var string[] 
  33.      */
  34.     private $functionList = array();
  35.  
  36.  
  37.     /**
  38.      * Constructs the test with the tokens it wishes to listen for.
  39.      */
  40.     public function __construct()
  41.     {
  42.         parent::__construct(array(T_CLASS, T_ANON_CLASS, T_INTERFACE), array(T_FUNCTION), true);
  43.  
  44.     }//end __construct()
  45.  
  46.  
  47.     /**
  48.      * Processes this test when one of its tokens is encountered.
  49.      *
  50.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
  51.      * @param int                         $stackPtr  The position of the current token
  52.      *                                                in the stack passed in $tokens.
  53.      * @param int                         $currScope A pointer to the start of the scope.
  54.      *
  55.      * @return void 
  56.      */
  57.     protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
  58.     {
  59.         $className = $phpcsFile->getDeclarationName($currScope);
  60.         if ($className !== $this->currentClass) {
  61.             $this->loadFunctionNamesInScope($phpcsFile, $currScope);
  62.             $this->currentClass = $className;
  63.         }
  64.  
  65.         $methodName = $phpcsFile->getDeclarationName($stackPtr);
  66.  
  67.         if (strcasecmp($methodName, $className) === 0) {
  68.             if (in_array('__construct', $this->functionList) === false) {
  69.                 $error = 'PHP4 style constructors are not allowed; use "__construct()" instead';
  70.                 $phpcsFile->addError($error, $stackPtr, 'OldStyle');
  71.             }
  72.         } else if (strcasecmp($methodName, '__construct') !== 0) {
  73.             // Not a constructor.
  74.             return;
  75.         }
  76.  
  77.         $tokens = $phpcsFile->getTokens();
  78.  
  79.         $parentClassName = $phpcsFile->findExtendedClassName($currScope);
  80.         if ($parentClassName === false) {
  81.             return;
  82.         }
  83.  
  84.         // Stop if the constructor doesn't have a body, like when it is abstract.
  85.         if (isset($tokens[$stackPtr]['scope_closer']) === false) {
  86.             return;
  87.         }
  88.  
  89.         $endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
  90.         $startIndex       = $stackPtr;
  91.         while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex, $endFunctionIndex)) !== false) {
  92.             if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
  93.                 && $tokens[($doubleColonIndex + 1)]['content'] === $parentClassName
  94.             ) {
  95.                 $error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead';
  96.                 $phpcsFile->addError($error, ($doubleColonIndex + 1), 'OldStyleCall');
  97.             }
  98.  
  99.             $startIndex = ($doubleColonIndex + 1);
  100.         }
  101.  
  102.     }//end processTokenWithinScope()
  103.  
  104.  
  105.     /**
  106.      * Processes a token that is found within the scope that this test is
  107.      * listening to.
  108.      *
  109.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  110.      * @param int                         $stackPtr  The position in the stack where this
  111.      *                                                token was found.
  112.      *
  113.      * @return void 
  114.      */
  115.     protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
  116.     {
  117.  
  118.     }//end processTokenOutsideScope()
  119.  
  120.  
  121.     /**
  122.      * Extracts all the function names found in the given scope.
  123.      *
  124.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
  125.      * @param int                         $currScope A pointer to the start of the scope.
  126.      *
  127.      * @return void 
  128.      */
  129.     protected function loadFunctionNamesInScope(File $phpcsFile, $currScope)
  130.     {
  131.         $this->functionList = array();
  132.         $tokens = $phpcsFile->getTokens();
  133.  
  134.         for ($i = ($tokens[$currScope]['scope_opener'] + 1); $i < $tokens[$currScope]['scope_closer']; $i++) {
  135.             if ($tokens[$i]['code'] !== T_FUNCTION) {
  136.                 continue;
  137.             }
  138.  
  139.             $next = $phpcsFile->findNext(T_STRING, $i);
  140.             $this->functionList[] = trim($tokens[$next]['content']);
  141.         }
  142.  
  143.     }//end loadFunctionNamesInScope()
  144.  
  145.  
  146. }//end class

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