Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15429340
D13426.id33583.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D13426.id33583.diff
View Options
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
@@ -83,6 +83,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',
@@ -366,6 +367,7 @@
'ArcanistDifferentialDependencyGraph' => 'AbstractDirectedGraph',
'ArcanistDifferentialRevisionHash' => 'Phobject',
'ArcanistDifferentialRevisionStatus' => 'Phobject',
+ 'ArcanistDirMagicConstantXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistDoubleQuoteXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistDownloadWorkflow' => 'ArcanistWorkflow',
'ArcanistDuplicateKeysInArrayXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
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/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,57 @@
+<?php
+
+final class ArcanistDirMagicConstantXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 81;
+
+ 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 (version_compare('5.4.0', $this->version, '<')) {
+ continue;
+ }
+
+ $function_calls = $this->getFunctionCalls($root, array('dirname'));
+
+ foreach ($function_calls as $function_call) {
+ $args = $function_call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
+
+ if (count($args->getChildren()) != 1) {
+ continue;
+ }
+
+ $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__');
+ }
+ }
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Mar 25, 1:44 AM (1 w, 6 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7721104
Default Alt Text
D13426.id33583.diff (3 KB)
Attached To
Mode
D13426: Add a linter rule for the use of `dirname(__FILE__)`
Attached
Detach File
Event Timeline
Log In to Comment