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

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