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

Source for file ValidClassNameSniff.php

Documentation is available at ValidClassNameSniff.php

  1. <?php
  2. /**
  3.  * Ensures class and interface names start with a capital letter and use _ separators.
  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\PEAR\Sniffs\NamingConventions;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ValidClassNameSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(
  27.                 T_CLASS,
  28.                 T_INTERFACE,
  29.                );
  30.  
  31.     }//end register()
  32.  
  33.  
  34.     /**
  35.      * Processes this test, when one of its tokens is encountered.
  36.      *
  37.      * @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
  38.      * @param int                  $stackPtr  The position of the current token
  39.      *                                         in the stack passed in $tokens.
  40.      *
  41.      * @return void 
  42.      */
  43.     public function process(File $phpcsFile$stackPtr)
  44.     {
  45.         $tokens $phpcsFile->getTokens();
  46.  
  47.         $className $phpcsFile->findNext(T_STRING$stackPtr);
  48.         $name      trim($tokens[$className]['content']);
  49.         $errorData = array(ucfirst($tokens[$stackPtr]['content']));
  50.  
  51.         // Make sure the first letter is a capital.
  52.         if (preg_match('|^[A-Z]|'$name=== 0{
  53.             $error '%s name must begin with a capital letter';
  54.             $phpcsFile->addError($error$stackPtr'StartWithCapital'$errorData);
  55.         }
  56.  
  57.         // Check that each new word starts with a capital as well, but don't
  58.         // check the first word, as it is checked above.
  59.         $validName = true;
  60.         $nameBits  explode('_'$name);
  61.         $firstBit  array_shift($nameBits);
  62.         foreach ($nameBits as $bit{
  63.             if ($bit === '' || $bit{0!== strtoupper($bit{0})) {
  64.                 $validName = false;
  65.                 break;
  66.             }
  67.         }
  68.  
  69.         if ($validName === false{
  70.             // Strip underscores because they cause the suggested name
  71.             // to be incorrect.
  72.             $nameBits explode('_'trim($name'_'));
  73.             $firstBit array_shift($nameBits);
  74.             if ($firstBit === ''{
  75.                 $error '%s name is not valid';
  76.                 $phpcsFile->addError($error$stackPtr'Invalid'$errorData);
  77.             else {
  78.                 $newName strtoupper($firstBit{0}).substr($firstBit1).'_';
  79.                 foreach ($nameBits as $bit{
  80.                     if ($bit !== ''{
  81.                         $newName .= strtoupper($bit{0}).substr($bit1).'_';
  82.                     }
  83.                 }
  84.  
  85.                 $newName rtrim($newName'_');
  86.                 $error   '%s name is not valid; consider %s instead';
  87.                 $data    $errorData;
  88.                 $data[]  $newName;
  89.                 $phpcsFile->addError($error$stackPtr'Invalid'$data);
  90.             }
  91.         }//end if
  92.  
  93.     }//end process()
  94.  
  95.  
  96. }//end class

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