[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_cs/DokuWiki/Sniffs/NamingConventions/ -> ConstructorNameSniff.php (source)

   1  <?php
   2  /**
   3   * Generic_Sniffs_NamingConventions_ConstructorNameSniff.
   4   *
   5   * PHP version 5
   6   *
   7   * @category  PHP
   8   * @package   PHP_CodeSniffer
   9   * @author    Greg Sherwood <gsherwood@squiz.net>
  10   * @author    Leif Wickland <lwickland@rightnow.com>
  11   * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  12   * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13   * @link      http://pear.php.net/package/PHP_CodeSniffer
  14   */
  15  
  16  if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
  17      $error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found';
  18      throw new PHP_CodeSniffer_Exception($error);
  19  }
  20  
  21  /**
  22   * Generic_Sniffs_NamingConventions_ConstructorNameSniff.
  23   *
  24   * Favor PHP 5 constructor syntax, which uses "function __construct()".
  25   * Avoid PHP 4 constructor syntax, which uses "function ClassName()".
  26   *
  27   * @category PHP
  28   * @package  PHP_CodeSniffer
  29   * @author   Leif Wickland <lwickland@rightnow.com>
  30   * @license  http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  31   * @version  Release: 1.3.3
  32   * @link     http://pear.php.net/package/PHP_CodeSniffer
  33   */
  34  class DokuWiki_Sniffs_NamingConventions_ConstructorNameSniff extends Generic_Sniffs_NamingConventions_ConstructorNameSniff
  35  {
  36      /**
  37       * Processes this test when one of its tokens is encountered.
  38       *
  39       * @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
  40       * @param int                  $stackPtr  The position of the current token
  41       *                                        in the stack passed in $tokens.
  42       * @param int                  $currScope A pointer to the start of the scope.
  43       *
  44       * @return void
  45       */
  46      protected function processTokenWithinScope(
  47          PHP_CodeSniffer_File $phpcsFile,
  48          $stackPtr,
  49          $currScope
  50      ) {
  51          $className  = $phpcsFile->getDeclarationName($currScope);
  52          $methodName = $phpcsFile->getDeclarationName($stackPtr);
  53  
  54          if (strcasecmp($methodName, $className) === 0) {
  55              $error = 'PHP4 style constructors are discouraged; use "__construct()" instead';
  56              $phpcsFile->addWarning($error, $stackPtr, 'OldStyle');
  57          } else if (strcasecmp($methodName, '__construct') !== 0) {
  58              // Not a constructor.
  59              return;
  60          }
  61  
  62          $tokens = $phpcsFile->getTokens();
  63  
  64          $parentClassName = $phpcsFile->findExtendedClassName($currScope);
  65          if ($parentClassName === false) {
  66              return;
  67          }
  68  
  69          $endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
  70          $startIndex       = $stackPtr;
  71          while ($doubleColonIndex = $phpcsFile->findNext(array(T_DOUBLE_COLON), $startIndex, $endFunctionIndex)) {
  72              if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
  73                  && $tokens[($doubleColonIndex + 1)]['content'] === $parentClassName
  74              ) {
  75                  $error = 'PHP4 style calls to parent constructors are discouraged; use "parent::__construct()" instead';
  76                  $phpcsFile->addWarning($error, ($doubleColonIndex + 1), 'OldStyleCall');
  77              }
  78  
  79              $startIndex = ($doubleColonIndex + 1);
  80          }
  81  
  82      }//end processTokenWithinScope()
  83  
  84  
  85  }//end class