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

Source for file CamelCapsFunctionNameSniff.php

Documentation is available at CamelCapsFunctionNameSniff.php

  1. <?php
  2. /**
  3.  * Ensures method and functions are named correctly.
  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\Generic\Sniffs\NamingConventions;
  11.  
  12. use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
  13. use PHP_CodeSniffer\Util\Common;
  14. use PHP_CodeSniffer\Files\File;
  15.  
  16. class CamelCapsFunctionNameSniff extends AbstractScopeSniff
  17. {
  18.  
  19.     /**
  20.      * A list of all PHP magic methods.
  21.      *
  22.      * @var array 
  23.      */
  24.     protected $magicMethods = array(
  25.                                'construct'  => true,
  26.                                'destruct'   => true,
  27.                                'call'       => true,
  28.                                'callstatic' => true,
  29.                                'get'        => true,
  30.                                'set'        => true,
  31.                                'isset'      => true,
  32.                                'unset'      => true,
  33.                                'sleep'      => true,
  34.                                'wakeup'     => true,
  35.                                'tostring'   => true,
  36.                                'set_state'  => true,
  37.                                'clone'      => true,
  38.                                'invoke'     => true,
  39.                                'debuginfo'  => true,
  40.                               );
  41.  
  42.     /**
  43.      * A list of all PHP non-magic methods starting with a double underscore.
  44.      *
  45.      * These come from PHP modules such as SOAPClient.
  46.      *
  47.      * @var array 
  48.      */
  49.     protected $methodsDoubleUnderscore = array(
  50.                                           'soapcall'               => true,
  51.                                           'getlastrequest'         => true,
  52.                                           'getlastresponse'        => true,
  53.                                           'getlastrequestheaders'  => true,
  54.                                           'getlastresponseheaders' => true,
  55.                                           'getfunctions'           => true,
  56.                                           'gettypes'               => true,
  57.                                           'dorequest'              => true,
  58.                                           'setcookie'              => true,
  59.                                           'setlocation'            => true,
  60.                                           'setsoapheaders'         => true,
  61.                                          );
  62.  
  63.     /**
  64.      * A list of all PHP magic functions.
  65.      *
  66.      * @var array 
  67.      */
  68.     protected $magicFunctions = array('autoload' => true);
  69.  
  70.     /**
  71.      * If TRUE, the string must not have two capital letters next to each other.
  72.      *
  73.      * @var boolean 
  74.      */
  75.     public $strict = true;
  76.  
  77.  
  78.     /**
  79.      * Constructs a Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff.
  80.      */
  81.     public function __construct()
  82.     {
  83.         parent::__construct(array(T_CLASST_INTERFACET_TRAIT)array(T_FUNCTION)true);
  84.  
  85.     }//end __construct()
  86.  
  87.  
  88.     /**
  89.      * Processes the tokens within the scope.
  90.      *
  91.      * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  92.      * @param int                  $stackPtr  The position where this token was
  93.      *                                         found.
  94.      * @param int                  $currScope The position of the current scope.
  95.      *
  96.      * @return void 
  97.      */
  98.     protected function processTokenWithinScope(File $phpcsFile$stackPtr$currScope)
  99.     {
  100.         $methodName $phpcsFile->getDeclarationName($stackPtr);
  101.         if ($methodName === null{
  102.             // Ignore closures.
  103.             return;
  104.         }
  105.  
  106.         $className $phpcsFile->getDeclarationName($currScope);
  107.         $errorData = array($className.'::'.$methodName);
  108.  
  109.         // Is this a magic method. i.e., is prefixed with "__" ?
  110.         if (preg_match('|^__|'$methodName!== 0{
  111.             $magicPart strtolower(substr($methodName2));
  112.             if (isset($this->magicMethods[$magicPart]=== false
  113.                 && isset($this->methodsDoubleUnderscore[$magicPart]=== false
  114.             {
  115.                 $error 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
  116.                 $phpcsFile->addError($error$stackPtr'MethodDoubleUnderscore'$errorData);
  117.             }
  118.  
  119.             return;
  120.         }
  121.  
  122.         // PHP4 constructors are allowed to break our rules.
  123.         if ($methodName === $className{
  124.             return;
  125.         }
  126.  
  127.         // PHP4 destructors are allowed to break our rules.
  128.         if ($methodName === '_'.$className{
  129.             return;
  130.         }
  131.  
  132.         // Ignore first underscore in methods prefixed with "_".
  133.         $methodName ltrim($methodName'_');
  134.  
  135.         $methodProps $phpcsFile->getMethodProperties($stackPtr);
  136.         if (Common::isCamelCaps($methodNamefalsetrue$this->strict=== false{
  137.             if ($methodProps['scope_specified'=== true{
  138.                 $error '%s method name "%s" is not in camel caps format';
  139.                 $data  = array(
  140.                           ucfirst($methodProps['scope']),
  141.                           $errorData[0],
  142.                          );
  143.                 $phpcsFile->addError($error$stackPtr'ScopeNotCamelCaps'$data);
  144.             else {
  145.                 $error 'Method name "%s" is not in camel caps format';
  146.                 $phpcsFile->addError($error$stackPtr'NotCamelCaps'$errorData);
  147.             }
  148.  
  149.             $phpcsFile->recordMetric($stackPtr'CamelCase method name''no');
  150.             return;
  151.         else {
  152.             $phpcsFile->recordMetric($stackPtr'CamelCase method name''yes');
  153.         }
  154.  
  155.     }//end processTokenWithinScope()
  156.  
  157.  
  158.     /**
  159.      * Processes the tokens outside the scope.
  160.      *
  161.      * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  162.      * @param int                  $stackPtr  The position where this token was
  163.      *                                         found.
  164.      *
  165.      * @return void 
  166.      */
  167.     protected function processTokenOutsideScope(File $phpcsFile$stackPtr)
  168.     {
  169.         $functionName $phpcsFile->getDeclarationName($stackPtr);
  170.         if ($functionName === null{
  171.             // Ignore closures.
  172.             return;
  173.         }
  174.  
  175.         $errorData = array($functionName);
  176.  
  177.         // Is this a magic function. i.e., it is prefixed with "__".
  178.         if (preg_match('|^__|'$functionName!== 0{
  179.             $magicPart strtolower(substr($functionName2));
  180.             if (isset($this->magicFunctions[$magicPart]=== false{
  181.                  $error 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
  182.                  $phpcsFile->addError($error$stackPtr'FunctionDoubleUnderscore'$errorData);
  183.             }
  184.  
  185.             return;
  186.         }
  187.  
  188.         // Ignore first underscore in functions prefixed with "_".
  189.         $functionName ltrim($functionName'_');
  190.  
  191.         if (Common::isCamelCaps($functionNamefalsetrue$this->strict=== false{
  192.             $error 'Function name "%s" is not in camel caps format';
  193.             $phpcsFile->addError($error$stackPtr'NotCamelCaps'$errorData);
  194.             $phpcsFile->recordMetric($stackPtr'CamelCase function name''no');
  195.         else {
  196.             $phpcsFile->recordMetric($stackPtr'CamelCase method name''yes');
  197.         }
  198.  
  199.     }//end processTokenOutsideScope()
  200.  
  201.  
  202. }//end class

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