1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-10 00:42:40 +01:00

Add a linter rule for lowercase function names

Summary: Ref T7409. Adds `ArcanistXHPASTLinter::LINT_LOWERCASE_FUNCTIONS` to ensure that builtin PHP functions are lowercased. Based on [[https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/PHP/LowercasePHPFunctionsSniff.php | Squiz_Sniffs_PHP_LowercasePHPFunctionsSniff]].

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin, epriestley

Maniphest Tasks: T7409

Differential Revision: https://secure.phabricator.com/D12377
This commit is contained in:
Joshua Spence 2015-04-22 22:02:09 +10:00
parent a9ebe1cb4f
commit ab4eac6f31
2 changed files with 48 additions and 1 deletions

View file

@ -60,6 +60,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
const LINT_LOGICAL_OPERATORS = 58; const LINT_LOGICAL_OPERATORS = 58;
const LINT_INNER_FUNCTION = 59; const LINT_INNER_FUNCTION = 59;
const LINT_DEFAULT_PARAMETERS = 60; const LINT_DEFAULT_PARAMETERS = 60;
const LINT_LOWERCASE_FUNCTIONS = 61;
private $blacklistedFunctions = array(); private $blacklistedFunctions = array();
private $naminghook; private $naminghook;
@ -188,6 +189,8 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
=> pht('Inner Functions'), => pht('Inner Functions'),
self::LINT_DEFAULT_PARAMETERS self::LINT_DEFAULT_PARAMETERS
=> pht('Default Parameters'), => pht('Default Parameters'),
self::LINT_LOWERCASE_FUNCTIONS
=> pht('Lowercase Functions'),
); );
} }
@ -236,6 +239,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
self::LINT_LOGICAL_OPERATORS => $advice, self::LINT_LOGICAL_OPERATORS => $advice,
self::LINT_INNER_FUNCTION => $warning, self::LINT_INNER_FUNCTION => $warning,
self::LINT_DEFAULT_PARAMETERS => $warning, self::LINT_DEFAULT_PARAMETERS => $warning,
self::LINT_LOWERCASE_FUNCTIONS => $advice,
); );
} }
@ -303,7 +307,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
public function getVersion() { public function getVersion() {
// The version number should be incremented whenever a new rule is added. // The version number should be incremented whenever a new rule is added.
return '23'; return '24';
} }
protected function resolveFuture($path, Future $future) { protected function resolveFuture($path, Future $future) {
@ -390,6 +394,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
'lintLogicalOperators' => self::LINT_LOGICAL_OPERATORS, 'lintLogicalOperators' => self::LINT_LOGICAL_OPERATORS,
'lintInnerFunctions' => self::LINT_INNER_FUNCTION, 'lintInnerFunctions' => self::LINT_INNER_FUNCTION,
'lintDefaultParameters' => self::LINT_DEFAULT_PARAMETERS, 'lintDefaultParameters' => self::LINT_DEFAULT_PARAMETERS,
'lintLowercaseFunctions' => self::LINT_LOWERCASE_FUNCTIONS,
); );
foreach ($method_codes as $method => $codes) { foreach ($method_codes as $method => $codes) {
@ -3681,6 +3686,39 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
} }
} }
private function lintLowercaseFunctions(XHPASTNode $root) {
static $builtin_functions = null;
if ($builtin_functions === null) {
$builtin_functions = array_fuse(
idx(get_defined_functions(), 'internal', array()));
}
$function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
foreach ($function_calls as $function_call) {
$function = $function_call->getChildByIndex(0);
if ($function->getTypeName() != 'n_SYMBOL_NAME') {
continue;
}
$function_name = $function->getConcreteString();
if (!idx($builtin_functions, strtolower($function_name))) {
continue;
}
if ($function_name != strtolower($function_name)) {
$this->raiseLintAtNode(
$function,
self::LINT_LOWERCASE_FUNCTIONS,
pht('Calls to built-in PHP functions should be lowercase.'),
strtolower($function_name));
}
}
}
/** /**
* Retrieve all calls to some specified function(s). * Retrieve all calls to some specified function(s).
* *

View file

@ -0,0 +1,9 @@
<?php
var_dump('');
Var_Dump('');
~~~~~~~~~~
advice:3:1
~~~~~~~~~~
<?php
var_dump('');
var_dump('');