Page MenuHomePhabricator

D14612.id35348.diff
No OneTemporary

D14612.id35348.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -373,6 +373,8 @@
'ArcanistUnsafeDynamicStringXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUnsafeDynamicStringXHPASTLinterRuleTestCase.php',
'ArcanistUpgradeWorkflow' => 'workflow/ArcanistUpgradeWorkflow.php',
'ArcanistUploadWorkflow' => 'workflow/ArcanistUploadWorkflow.php',
+ 'ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRule.php',
+ 'ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRuleTestCase.php',
'ArcanistUsageException' => 'exception/ArcanistUsageException.php',
'ArcanistUseStatementNamespacePrefixXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUseStatementNamespacePrefixXHPASTLinterRule.php',
'ArcanistUseStatementNamespacePrefixXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUseStatementNamespacePrefixXHPASTLinterRuleTestCase.php',
@@ -775,6 +777,8 @@
'ArcanistUnsafeDynamicStringXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistUpgradeWorkflow' => 'ArcanistWorkflow',
'ArcanistUploadWorkflow' => 'ArcanistWorkflow',
+ 'ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistUsageException' => 'Exception',
'ArcanistUseStatementNamespacePrefixXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUseStatementNamespacePrefixXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
diff --git a/src/lint/linter/xhpast/rules/ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRule.php
@@ -0,0 +1,50 @@
+<?php
+
+final class ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 127;
+
+ public function getLintName() {
+ return pht('Uppercase Hexadecimal');
+ }
+
+ public function getLintSeverity() {
+ return ArcanistLintSeverity::SEVERITY_WARNING;
+ }
+
+ public function process(XHPASTNode $root) {
+ $hexadecimals = $this->getHexadecimalNumericScalars($root);
+
+ foreach ($hexadecimals as $hexadecimal) {
+ $value = substr($hexadecimal->getConcreteString(), 2);
+
+ if (!preg_match('/^0x[0-9A-F]+$/', $hexadecimal->getConcreteString())) {
+ $this->raiseLintAtNode(
+ $hexadecimal,
+ pht(
+ 'For consistency, write hexadecimals in uppercase '.
+ 'with a leading `%s`.',
+ '0x'),
+ '0x'.$value);
+ }
+ }
+ }
+
+ private function getHexadecimalNumericScalars(XHPASTNode $root) {
+ $numeric_scalars = $root->selectDescendantsOfType('n_NUMERIC_SCALAR');
+ $hexadecimal_numeric_scalars = array();
+
+ foreach ($numeric_scalars as $numeric_scalar) {
+ $number = $numeric_scalar->getConcreteString();
+
+ if (preg_match('/^0x[0-9A-F]+$/i', $number)) {
+ $hexadecimal_numeric_scalars[] = $numeric_scalar;
+ }
+ }
+
+ return $hexadecimal_numeric_scalars;
+
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRuleTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRuleTestCase.php
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistUppercaseHexadecimalNumericScalarXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(
+ dirname(__FILE__).'/uppercase-hexadecimal-numeric-scalar/');
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/uppercase-hexadecimal-numeric-scalar/hexadecimal.lint-test b/src/lint/linter/xhpast/rules/__tests__/uppercase-hexadecimal-numeric-scalar/hexadecimal.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/uppercase-hexadecimal-numeric-scalar/hexadecimal.lint-test
@@ -0,0 +1,7 @@
+<?php
+0xFF;
+0xff;
+0XFF;
+~~~~~~~~~~
+warning:3:1
+warning:4:1

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 9, 7:59 PM (3 d, 4 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6763369
Default Alt Text
D14612.id35348.diff (4 KB)

Event Timeline