Page MenuHomePhabricator

D13426.id32509.diff
No OneTemporary

D13426.id32509.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
@@ -81,6 +81,7 @@
'ArcanistDifferentialDependencyGraph' => 'differential/ArcanistDifferentialDependencyGraph.php',
'ArcanistDifferentialRevisionHash' => 'differential/constants/ArcanistDifferentialRevisionHash.php',
'ArcanistDifferentialRevisionStatus' => 'differential/constants/ArcanistDifferentialRevisionStatus.php',
+ 'ArcanistDirMagicConstantXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDirMagicConstantXHPASTLinterRule.php',
'ArcanistDoubleQuoteXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDoubleQuoteXHPASTLinterRule.php',
'ArcanistDownloadWorkflow' => 'workflow/ArcanistDownloadWorkflow.php',
'ArcanistDuplicateKeysInArrayXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDuplicateKeysInArrayXHPASTLinterRule.php',
@@ -354,6 +355,7 @@
'ArcanistDifferentialDependencyGraph' => 'AbstractDirectedGraph',
'ArcanistDifferentialRevisionHash' => 'Phobject',
'ArcanistDifferentialRevisionStatus' => 'Phobject',
+ 'ArcanistDirMagicConstantXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistDoubleQuoteXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistDownloadWorkflow' => 'ArcanistWorkflow',
'ArcanistDuplicateKeysInArrayXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
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
@@ -50,14 +50,21 @@
}
public function setLinterConfigurationValue($key, $value) {
+ $found_match = false;
+
foreach ($this->rules as $rule) {
foreach ($rule->getLinterConfigurationOptions() as $k => $spec) {
if ($k == $key) {
- return $rule->setLinterConfigurationValue($key, $value);
+ $found_match = true;
+ $rule->setLinterConfigurationValue($key, $value);
}
}
}
+ if ($found_match) {
+ return;
+ }
+
return parent::setLinterConfigurationValue($key, $value);
}
diff --git a/src/lint/linter/__tests__/xhpast/dirname-file.lint-test b/src/lint/linter/__tests__/xhpast/dirname-file.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/dirname-file.lint-test
@@ -0,0 +1,20 @@
+<?php
+echo dirname(__FILE__);
+echo dirname(dirname(__FILE__));
+echo __FILE__;
+echo __DIR__;
+echo dirname(__file__);
+~~~~~~~~~~
+advice:2:6
+advice:3:14
+advice:6:6
+warning:6:14
+~~~~~~~~~~
+<?php
+echo __DIR__;
+echo dirname(__DIR__);
+echo __FILE__;
+echo __DIR__;
+echo __DIR__;
+~~~~~~~~~~
+{"config": {"xhpast.php-version": "5.4.0"}}
diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLinterRule.php b/src/lint/linter/xhpast/ArcanistXHPASTLinterRule.php
--- a/src/lint/linter/xhpast/ArcanistXHPASTLinterRule.php
+++ b/src/lint/linter/xhpast/ArcanistXHPASTLinterRule.php
@@ -4,6 +4,9 @@
private $linter = null;
+ protected $version;
+ protected $windowsVersion;
+
final public static function loadAllRules() {
$rules = array();
@@ -60,10 +63,29 @@
}
public function getLinterConfigurationOptions() {
- return array();
+ return array(
+ 'xhpast.php-version' => array(
+ 'type' => 'optional string',
+ 'help' => pht('PHP version to target.'),
+ ),
+ 'xhpast.php-version.windows' => array(
+ 'type' => 'optional string',
+ 'help' => pht('PHP version to target on Windows.'),
+ ),
+ );
}
- public function setLinterConfigurationValue($key, $value) {}
+ public function setLinterConfigurationValue($key, $value) {
+ switch ($key) {
+ case 'xhpast.php-version':
+ $this->version = $value;
+ return;
+
+ case 'xhpast.php-version.windows':
+ $this->windowsVersion = $value;
+ return;
+ }
+ }
abstract public function process(XHPASTNode $root);
diff --git a/src/lint/linter/xhpast/rules/ArcanistDirMagicConstantXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistDirMagicConstantXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistDirMagicConstantXHPASTLinterRule.php
@@ -0,0 +1,56 @@
+<?php
+
+final class ArcanistDirMagicConstantXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 73;
+
+ public function getLintName() {
+ return pht('%s Usage', 'dirname(__FILE__)');
+ }
+
+ public function getLintSeverity() {
+ return ArcanistLintSeverity::SEVERITY_ADVICE;
+ }
+
+ /**
+ * Lints for the usage of `dirname(__FILE__)`.
+ *
+ * The `__DIR__` magic constant has been available since PHP 5.3. Prior to
+ * this PHP version, `dirname(__FILE__)` was used to refer to the directory
+ * of the current file. In modern versions of PHP, `__DIR__` should always be
+ * used instead of `dirname(__FILE__)`.
+ *
+ * @param XHPASTNode The root node.
+ * @return void
+ */
+ public function process(XHPASTNode $root) {
+ if ($this->version && version_compare('5.4.0', $this->version, '<')) {
+ continue;
+ }
+
+ $function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
+
+ foreach ($function_calls as $function_call) {
+ $name = $function_call->getChildByIndex(0)->getConcreteString();
+ $args = $function_call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
+
+ if ($name == 'dirname' && count($args->getChildren()) == 1) {
+ $arg = $args->getChildByIndex(0);
+
+ if ($arg->getTypeName() == 'n_MAGIC_SCALAR' &&
+ strtoupper($arg->getSemanticString()) == '__FILE__') {
+
+ $this->raiseLintAtNode(
+ $function_call,
+ pht(
+ 'Use `%s` instead of `%s`.',
+ '__DIR__',
+ 'dirname(__FILE__)'),
+ '__DIR__');
+ }
+ }
+ }
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php
--- a/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php
+++ b/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php
@@ -5,41 +5,10 @@
const ID = 45;
- private $version;
- private $windowsVersion;
-
public function getLintName() {
return pht('PHP Compatibility');
}
- public function getLinterConfigurationOptions() {
- return parent::getLinterConfigurationOptions() + array(
- 'xhpast.php-version' => array(
- 'type' => 'optional string',
- 'help' => pht('PHP version to target.'),
- ),
- 'xhpast.php-version.windows' => array(
- 'type' => 'optional string',
- 'help' => pht('PHP version to target on Windows.'),
- ),
- );
- }
-
- public function setLinterConfigurationValue($key, $value) {
- switch ($key) {
- case 'xhpast.php-version':
- $this->version = $value;
- return;
-
- case 'xhpast.php-version.windows':
- $this->windowsVersion = $value;
- return;
-
- default:
- return parent::setLinterConfigurationValue($key, $value);
- }
- }
-
public function process(XHPASTNode $root) {
static $compat_info;

File Metadata

Mime Type
text/plain
Expires
Thu, Mar 20, 3:50 AM (2 w, 5 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7712487
Default Alt Text
D13426.id32509.diff (7 KB)

Event Timeline