Page MenuHomePhabricator

D12366.id29694.diff
No OneTemporary

D12366.id29694.diff

diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php
--- a/src/lint/linter/ArcanistXHPASTLinter.php
+++ b/src/lint/linter/ArcanistXHPASTLinter.php
@@ -56,6 +56,7 @@
const LINT_FORMATTED_STRING = 54;
const LINT_UNNECESSARY_FINAL_MODIFIER = 55;
const LINT_UNNECESSARY_SEMICOLON = 56;
+ const LINT_CONSTANT_CASE = 57;
private $blacklistedFunctions = array();
private $naminghook;
@@ -125,6 +126,7 @@
self::LINT_FORMATTED_STRING => 'Formatted String',
self::LINT_UNNECESSARY_FINAL_MODIFIER => 'Unnecessary Final Modifier',
self::LINT_UNNECESSARY_SEMICOLON => 'Unnecessary Semicolon',
+ self::LINT_CONSTANT_CASE => 'Constant Name Case',
);
}
@@ -169,6 +171,7 @@
self::LINT_IMPLICIT_VISIBILITY => $advice,
self::LINT_UNNECESSARY_FINAL_MODIFIER => $advice,
self::LINT_UNNECESSARY_SEMICOLON => $advice,
+ self::LINT_CONSTANT_CASE => $warning,
);
}
@@ -236,7 +239,7 @@
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 +319,7 @@
'lintFormattedString' => self::LINT_FORMATTED_STRING,
'lintUnnecessaryFinalModifier' => self::LINT_UNNECESSARY_FINAL_MODIFIER,
'lintUnnecessarySemicolons' => self::LINT_UNNECESSARY_SEMICOLON,
+ 'lintConstantDefinitions' => self::LINT_CONSTANT_CASE,
);
foreach ($method_codes as $method => $codes) {
@@ -3263,6 +3267,83 @@
}
}
+ 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_CONSTANT_CASE,
+ 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',
diff --git a/src/lint/linter/__tests__/xhpast/constant-case.lint-test b/src/lint/linter/__tests__/xhpast/constant-case.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/constant-case.lint-test
@@ -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
diff --git a/src/lint/linter/__tests__/xhpast/dynamic-define.lint-test b/src/lint/linter/__tests__/xhpast/dynamic-define.lint-test
--- a/src/lint/linter/__tests__/xhpast/dynamic-define.lint-test
+++ b/src/lint/linter/__tests__/xhpast/dynamic-define.lint-test
@@ -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
diff --git a/src/lint/linter/__tests__/xhpast/naming-conventions.lint-test b/src/lint/linter/__tests__/xhpast/naming-conventions.lint-test
--- a/src/lint/linter/__tests__/xhpast/naming-conventions.lint-test
+++ b/src/lint/linter/__tests__/xhpast/naming-conventions.lint-test
@@ -59,6 +59,7 @@
warning:2:13
warning:3:9
warning:3:16
+warning:3:16
warning:4:13
warning:4:17
warning:5:19

File Metadata

Mime Type
text/plain
Expires
Sun, Mar 16, 7:45 AM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7706563
Default Alt Text
D12366.id29694.diff (5 KB)

Event Timeline