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

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