Page MenuHomePhabricator

D14612.diff
No OneTemporary

D14612.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
@@ -163,6 +163,8 @@
'ArcanistHLintLinter' => 'lint/linter/ArcanistHLintLinter.php',
'ArcanistHLintLinterTestCase' => 'lint/linter/__tests__/ArcanistHLintLinterTestCase.php',
'ArcanistHelpWorkflow' => 'workflow/ArcanistHelpWorkflow.php',
+ 'ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule.php',
+ 'ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase.php',
'ArcanistHgClientChannel' => 'hgdaemon/ArcanistHgClientChannel.php',
'ArcanistHgProxyClient' => 'hgdaemon/ArcanistHgProxyClient.php',
'ArcanistHgProxyServer' => 'hgdaemon/ArcanistHgProxyServer.php',
@@ -561,6 +563,8 @@
'ArcanistHLintLinter' => 'ArcanistExternalLinter',
'ArcanistHLintLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistHelpWorkflow' => 'ArcanistWorkflow',
+ 'ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistHgClientChannel' => 'PhutilProtocolChannel',
'ArcanistHgProxyClient' => 'Phobject',
'ArcanistHgProxyServer' => 'Phobject',
diff --git a/src/lint/linter/xhpast/rules/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule.php
@@ -0,0 +1,50 @@
+<?php
+
+final class ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 127;
+
+ public function getLintName() {
+ return pht('Hexadecimal Casing');
+ }
+
+ 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'.strtoupper($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__/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase.php
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(
+ dirname(__FILE__).'/hexadecimal-numeric-scalar-casing/');
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/hexadecimal-numeric-scalar-casing/hexadecimal.lint-test b/src/lint/linter/xhpast/rules/__tests__/hexadecimal-numeric-scalar-casing/hexadecimal.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/hexadecimal-numeric-scalar-casing/hexadecimal.lint-test
@@ -0,0 +1,12 @@
+<?php
+0xFF;
+0xff;
+0XFF;
+~~~~~~~~~~
+warning:3:1
+warning:4:1
+~~~~~~~~~~
+<?php
+0xFF;
+0xFF;
+0xFF;

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 11, 2:45 PM (2 d, 13 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6717226
Default Alt Text
D14612.diff (4 KB)

Event Timeline