mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-25 16:22:42 +01:00
Add a linter rule for uppercase constant definitions
Summary: Ref T7409. Adds a linter rule to ensure that constant are defined as being uppercase. Based on [[https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php | Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff]]. Test Plan: Added test cases. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: Korvin, epriestley Maniphest Tasks: T7409 Differential Revision: https://secure.phabricator.com/D12366
This commit is contained in:
parent
bf1b30ae26
commit
f86f4f24e6
3 changed files with 93 additions and 3 deletions
|
@ -236,7 +236,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
|||
|
||||
public function getVersion() {
|
||||
// The version number should be incremented whenever a new rule is added.
|
||||
return '18';
|
||||
return '19';
|
||||
}
|
||||
|
||||
protected function resolveFuture($path, Future $future) {
|
||||
|
@ -316,6 +316,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
|||
'lintFormattedString' => self::LINT_FORMATTED_STRING,
|
||||
'lintUnnecessaryFinalModifier' => self::LINT_UNNECESSARY_FINAL_MODIFIER,
|
||||
'lintUnnecessarySemicolons' => self::LINT_UNNECESSARY_SEMICOLON,
|
||||
'lintConstantDefinitions' => self::LINT_NAMING_CONVENTIONS,
|
||||
);
|
||||
|
||||
foreach ($method_codes as $method => $codes) {
|
||||
|
@ -3263,6 +3264,83 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
|||
}
|
||||
}
|
||||
|
||||
private function lintConstantDefinitions(XHPASTNode $root) {
|
||||
$defines = $this
|
||||
->getFunctionCalls($root, array('define'))
|
||||
->add($root->selectDescendantsOfTypes(array(
|
||||
'n_CLASS_CONSTANT_DECLARATION',
|
||||
'n_CONSTANT_DECLARATION',
|
||||
)));
|
||||
|
||||
foreach ($defines as $define) {
|
||||
switch ($define->getTypeName()) {
|
||||
case 'n_CLASS_CONSTANT_DECLARATION':
|
||||
case 'n_CONSTANT_DECLARATION':
|
||||
$constant = $define->getChildByIndex(0);
|
||||
|
||||
if ($constant->getTypeName() !== 'n_STRING') {
|
||||
$constant = null;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'n_FUNCTION_CALL':
|
||||
$constant = $define
|
||||
->getChildOfType(1, 'n_CALL_PARAMETER_LIST')
|
||||
->getChildByIndex(0);
|
||||
|
||||
if ($constant->getTypeName() !== 'n_STRING_SCALAR') {
|
||||
$constant = null;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$constant = null;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$constant) {
|
||||
continue;
|
||||
}
|
||||
$constant_name = $constant->getConcreteString();
|
||||
|
||||
if ($constant_name !== strtoupper($constant_name)) {
|
||||
$this->raiseLintAtNode(
|
||||
$constant,
|
||||
self::LINT_NAMING_CONVENTIONS,
|
||||
pht('Constants should be uppercase.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve all calls to some specified function(s).
|
||||
*
|
||||
* Returns all descendant nodes which represent a function call to one of the
|
||||
* specified functions.
|
||||
*
|
||||
* @param XHPASTNode Root node.
|
||||
* @param list<string> Function names.
|
||||
* @return AASTNodeList
|
||||
*/
|
||||
protected function getFunctionCalls(XHPASTNode $root, array $function_names) {
|
||||
$calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
|
||||
$nodes = array();
|
||||
|
||||
foreach ($calls as $call) {
|
||||
$node = $call->getChildByIndex(0);
|
||||
$name = strtolower($node->getConcreteString());
|
||||
|
||||
if (in_array($name, $function_names)) {
|
||||
$nodes[] = $call;
|
||||
}
|
||||
}
|
||||
|
||||
return AASTNodeList::newFromTreeAndNodes($root->getTree(), $nodes);
|
||||
}
|
||||
|
||||
public function getSuperGlobalNames() {
|
||||
return array(
|
||||
'$GLOBALS',
|
||||
|
|
12
src/lint/linter/__tests__/xhpast/constant-case.lint-test
Normal file
12
src/lint/linter/__tests__/xhpast/constant-case.lint-test
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
define('foo', 'bar');
|
||||
const bar = 'baz';
|
||||
|
||||
class Foo {
|
||||
const bar = 'baz';
|
||||
}
|
||||
~~~~~~~~~~
|
||||
warning:2:8
|
||||
warning:3:7
|
||||
error:5:7
|
||||
warning:6:9
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
define('pony', 'cute');
|
||||
define('PONY', 'cute');
|
||||
define($pony, 'cute');
|
||||
define('pony', $cute);
|
||||
define('PONY', $cute);
|
||||
define($pony, $cute);
|
||||
~~~~~~~~~~
|
||||
error:3:8 dynamic define
|
||||
|
|
Loading…
Reference in a new issue