Page MenuHomePhabricator

D14604.id35422.diff
No OneTemporary

D14604.id35422.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
@@ -190,6 +190,8 @@
'ArcanistInvalidDefaultParameterXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistInvalidDefaultParameterXHPASTLinterRuleTestCase.php',
'ArcanistInvalidModifiersXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistInvalidModifiersXHPASTLinterRule.php',
'ArcanistInvalidModifiersXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistInvalidModifiersXHPASTLinterRuleTestCase.php',
+ 'ArcanistInvalidOctalNumericScalarXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistInvalidOctalNumericScalarXHPASTLinterRule.php',
+ 'ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase.php',
'ArcanistJSHintLinter' => 'lint/linter/ArcanistJSHintLinter.php',
'ArcanistJSHintLinterTestCase' => 'lint/linter/__tests__/ArcanistJSHintLinterTestCase.php',
'ArcanistJSONLintLinter' => 'lint/linter/ArcanistJSONLintLinter.php',
@@ -590,6 +592,8 @@
'ArcanistInvalidDefaultParameterXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistInvalidModifiersXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistInvalidModifiersXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
+ 'ArcanistInvalidOctalNumericScalarXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistJSHintLinter' => 'ArcanistExternalLinter',
'ArcanistJSHintLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistJSONLintLinter' => 'ArcanistExternalLinter',
diff --git a/src/lint/linter/xhpast/rules/ArcanistInvalidOctalNumericScalarXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistInvalidOctalNumericScalarXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistInvalidOctalNumericScalarXHPASTLinterRule.php
@@ -0,0 +1,44 @@
+<?php
+
+final class ArcanistInvalidOctalNumericScalarXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 125;
+
+ public function getLintName() {
+ return pht('Invalid Octal Numeric Scalar');
+ }
+
+ public function process(XHPASTNode $root) {
+ $octals = $this->getOctalNumericScalars($root);
+
+ foreach ($octals as $octal) {
+ if (!preg_match('/^0[0-7]*$/', $octal->getConcreteString())) {
+ $this->raiseLintAtNode(
+ $octal,
+ pht(
+ 'Invalid octal numeric scalar. `%s` is not a '.
+ 'valid octal and will be interpreted as `%d`.',
+ $octal->getConcreteString(),
+ intval($octal->getConcreteString(), 8)));
+ }
+ }
+ }
+
+ private function getOctalNumericScalars(XHPASTNode $root) {
+ $numeric_scalars = $root->selectDescendantsOfType('n_NUMERIC_SCALAR');
+ $octal_numeric_scalars = array();
+
+ foreach ($numeric_scalars as $numeric_scalar) {
+ $number = $numeric_scalar->getConcreteString();
+
+ if (preg_match('/^0\d+$/', $number)) {
+ $octal_numeric_scalars[] = $numeric_scalar;
+ }
+ }
+
+ return $octal_numeric_scalars;
+
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase.php
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(
+ dirname(__FILE__).'/invalid-octal-numeric-scalar/');
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/binary.lint-test b/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/binary.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/binary.lint-test
@@ -0,0 +1,3 @@
+<?php
+0b01;
+~~~~~~~~
diff --git a/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/decimal.lint-test b/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/decimal.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/decimal.lint-test
@@ -0,0 +1,5 @@
+<?php
+0;
+123;
+0.9;
+~~~~~~~~~~
diff --git a/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/hexadecimal.lint-test b/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/hexadecimal.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/hexadecimal.lint-test
@@ -0,0 +1,4 @@
+<?php
+0xFf;
+0XFf;
+~~~~~~~~
diff --git a/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/octal.lint-test b/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/octal.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/invalid-octal-numeric-scalar/octal.lint-test
@@ -0,0 +1,7 @@
+<?php
+01234567;
+08;
+-08;
+~~~~~~~~
+error:3:1
+error:4:2

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 9, 2:00 AM (3 d, 21 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6724077
Default Alt Text
D14604.id35422.diff (5 KB)

Event Timeline