]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for the use of `parse_str`
authorJoshua Spence <josh@joshuaspence.com>
Thu, 13 Aug 2015 21:44:19 +0000 (07:44 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Thu, 13 Aug 2015 21:45:27 +0000 (07:45 +1000)
Summary: The use of the [[http://php.net/manual/en/function.parse-str.php | parse_str]] method (when called without sepcifying a second parameter) hinders static analysis. Specifically, the `parse_str('...')` behaves similarly to [[http://php.net/manual/en/function.extract.php | extract]].

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D13857

src/__phutil_library_map__.php
src/lint/linter/__tests__/xhpast/extract-use.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/xhpast/parse_str-use.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/ArcanistParseStrUseXHPASTLinterRule.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php

index 97a653f0c107f9a6a43552a2089015f9a989f8c0..ca5061e4d658bf1929667effe1e7cd9fdacc22d4 100644 (file)
@@ -185,6 +185,7 @@ phutil_register_library_map(array(
     'ArcanistPHPOpenTagXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistPHPOpenTagXHPASTLinterRule.php',
     'ArcanistPHPShortTagXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistPHPShortTagXHPASTLinterRule.php',
     'ArcanistParenthesesSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistParenthesesSpacingXHPASTLinterRule.php',
+    'ArcanistParseStrUseXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistParseStrUseXHPASTLinterRule.php',
     'ArcanistPasteWorkflow' => 'workflow/ArcanistPasteWorkflow.php',
     'ArcanistPatchWorkflow' => 'workflow/ArcanistPatchWorkflow.php',
     'ArcanistPhpLinter' => 'lint/linter/ArcanistPhpLinter.php',
@@ -467,6 +468,7 @@ phutil_register_library_map(array(
     'ArcanistPHPOpenTagXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistPHPShortTagXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistParenthesesSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistParseStrUseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistPasteWorkflow' => 'ArcanistWorkflow',
     'ArcanistPatchWorkflow' => 'ArcanistWorkflow',
     'ArcanistPhpLinter' => 'ArcanistExternalLinter',
diff --git a/src/lint/linter/__tests__/xhpast/extract-use.lint-test b/src/lint/linter/__tests__/xhpast/extract-use.lint-test
new file mode 100644 (file)
index 0000000..43f13e6
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+
+extract();
+~~~~~~~~~~
+error:3:1
diff --git a/src/lint/linter/__tests__/xhpast/parse_str-use.lint-test b/src/lint/linter/__tests__/xhpast/parse_str-use.lint-test
new file mode 100644 (file)
index 0000000..21afe52
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+
+$params = array();
+parse_str('foo=bar', $params);
+parse_str('foo=bar');
+~~~~~~~~~~
+error:5:1
diff --git a/src/lint/linter/xhpast/rules/ArcanistParseStrUseXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistParseStrUseXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..9656e4a
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+final class ArcanistParseStrUseXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 80;
+
+  public function getLintName() {
+    return pht('Questionable Use of %s', 'parse_str()');
+  }
+
+  public function process(XHPASTNode $root) {
+    $calls = $this->getFunctionCalls($root, array('parse_str'));
+
+    foreach ($calls as $call) {
+      $call_params = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
+
+      if (count($call_params->getChildren()) < 2) {
+        $this->raiseLintAtNode(
+          $call,
+          pht(
+            'Avoid %s unless the second parameter is specified. '.
+            'It is confusing and hinders static analysis.',
+            'parse_str()'));
+      }
+    }
+  }
+
+}
index c60b216ced98bc2e4b831258c477bd15a55acbf6..05464d41b8f88a0170060ae74e44314510b04a08 100644 (file)
@@ -48,6 +48,8 @@ final class ArcanistUndeclaredVariableXHPASTLinterRule
     //
     // TODO: Support functions defined inside other functions which is commonly
     // used with anonymous functions.
+    //
+    // TODO: parse_str() also makes lexical scope unknowable, see D13857.
 
     $defs = $root->selectDescendantsOfTypes(array(
       'n_FUNCTION_DECLARATION',